Rewrote avatar element contruction as Twig macro.
This commit is contained in:
parent
47121897b9
commit
ebabb19998
3 changed files with 12 additions and 49 deletions
|
@ -32,7 +32,6 @@ final class TwigMisuzu extends AbstractExtension {
|
||||||
return [
|
return [
|
||||||
new TwigFunction('url_construct', 'url_construct'),
|
new TwigFunction('url_construct', 'url_construct'),
|
||||||
new TwigFunction('url', 'url'),
|
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_children', 'forum_may_have_children'),
|
||||||
new TwigFunction('forum_may_have_topics', 'forum_may_have_topics'),
|
new TwigFunction('forum_may_have_topics', 'forum_may_have_topics'),
|
||||||
new TwigFunction('csrf_token', fn() => CSRF::token()),
|
new TwigFunction('csrf_token', fn() => CSRF::token()),
|
||||||
|
|
|
@ -85,6 +85,16 @@
|
||||||
</div>
|
</div>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro avatar(user_id, resolution, alt_text, attributes) %}
|
{% macro avatar(user_id, resolution, alt_text, attrs) %}
|
||||||
{{ html_avatar(user_id, resolution, alt_text|default(''), attributes|default([]))|raw }}
|
{% 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 %}
|
{% endmacro %}
|
||||||
|
|
46
utility.php
46
utility.php
|
@ -33,14 +33,6 @@ function clamp($num, int $min, int $max): int {
|
||||||
return max($min, min($max, (int)$num));
|
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 {
|
function first_paragraph(string $text, string $delimiter = "\n"): string {
|
||||||
$index = mb_strpos($text, $delimiter);
|
$index = mb_strpos($text, $delimiter);
|
||||||
return $index === false ? $text : mb_substr($text, 0, $index);
|
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);
|
$template = sprintf($template, $httpCode);
|
||||||
|
|
||||||
/*if(!tpl_exists($template)) {
|
|
||||||
$template = 'errors.master';
|
|
||||||
}*/
|
|
||||||
|
|
||||||
return \Misuzu\Template::renderRaw(sprintf($template, $httpCode));
|
return \Misuzu\Template::renderRaw(sprintf($template, $httpCode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,40 +124,6 @@ function html_colour(?int $colour, $attribs = '--user-colour'): string {
|
||||||
return $css;
|
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 {
|
function msz_server_timing(\Index\Performance\Timings $timings): string {
|
||||||
$laps = $timings->getLaps();
|
$laps = $timings->getLaps();
|
||||||
$timings = [];
|
$timings = [];
|
||||||
|
|
Loading…
Reference in a new issue