Rewrote avatar element contruction as Twig macro.

This commit is contained in:
flash 2023-01-03 00:10:28 +00:00
parent 47121897b9
commit ebabb19998
3 changed files with 12 additions and 49 deletions

View file

@ -32,7 +32,6 @@ final class TwigMisuzu extends AbstractExtension {
return [
new TwigFunction('url_construct', 'url_construct'),
new TwigFunction('url', 'url'),
new TwigFunction('html_avatar', 'html_avatar'),
new TwigFunction('forum_may_have_children', 'forum_may_have_children'),
new TwigFunction('forum_may_have_topics', 'forum_may_have_topics'),
new TwigFunction('csrf_token', fn() => CSRF::token()),

View file

@ -85,6 +85,16 @@
</div>
{% endmacro %}
{% macro avatar(user_id, resolution, alt_text, attributes) %}
{{ html_avatar(user_id, resolution, alt_text|default(''), attributes|default([]))|raw }}
{% macro avatar(user_id, resolution, alt_text, attrs) %}
{% apply spaceless %}
<img src="{{ url('user-avatar', { 'user': user_id, 'res': resolution * 2 }) }}"
alt="{{ alt_text|default('') }}"
width="{{ attrs.width|default(resolution) }}"
height="{{ attrs.height|default(resolution) }}"
class="avatar{% if attrs.class is defined %} {{ attrs.class }}{% endif %}"
{% if attrs.id is defined %}
id="{{ attrs.id }}"
{% endif %}
/>
{% endapply %}
{% endmacro %}

View file

@ -33,14 +33,6 @@ function clamp($num, int $min, int $max): int {
return max($min, min($max, (int)$num));
}
function starts_with(string $string, string $text): bool {
return str_starts_with($string, $text);
}
function ends_with(string $string, string $text): bool {
return str_ends_with($string, $text);
}
function first_paragraph(string $text, string $delimiter = "\n"): string {
$index = mb_strpos($text, $delimiter);
return $index === false ? $text : mb_substr($text, 0, $index);
@ -110,10 +102,6 @@ function render_info(?string $message, int $httpCode, string $template = 'errors
$template = sprintf($template, $httpCode);
/*if(!tpl_exists($template)) {
$template = 'errors.master';
}*/
return \Misuzu\Template::renderRaw(sprintf($template, $httpCode));
}
@ -136,40 +124,6 @@ function html_colour(?int $colour, $attribs = '--user-colour'): string {
return $css;
}
function html_avatar(?int $userId, int $resolution, string $altText = '', array $attributes = []): string {
$attributes['src'] = url('user-avatar', ['user' => $userId ?? 0, 'res' => $resolution * 2]);
$attributes['alt'] = $altText;
$attributes['class'] = trim('avatar ' . ($attributes['class'] ?? ''));
if(!isset($attributes['width']))
$attributes['width'] = $resolution;
if(!isset($attributes['height']))
$attributes['height'] = $resolution;
return html_tag('img', $attributes);
}
function html_tag(string $name, array $atrributes = [], ?bool $close = null, string $content = ''): string {
$html = '<' . $name;
foreach($atrributes as $key => $value) {
$html .= ' ' . $key;
if(!empty($value))
$html .= '="' . $value . '"';
}
if($close === false)
$html .= '/';
$html .= '>';
if($close === true)
$html .= $content . '</' . $name . '>';
return $html;
}
function msz_server_timing(\Index\Performance\Timings $timings): string {
$laps = $timings->getLaps();
$timings = [];