• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Questions, template variable filters, parent param in macro

Yugensoft

Active member
#1
Hi,

Where can I find a list of the Django-style template variable filters for XF2? E.g. when you see {$someVar|number}
Also, how do I add one?

Secondly, how do I use view param in a macro? I.e. if I add a param: $reply->setParam('myParam', 'something'), how can I use it in any macro the associated template uses? This is a permission-like param that says whether a particular piece of info should be visible.

I don't want to inject it via a template modification to insert an arg-somethingSomething, as it's a global flag.

Cheers
 

Jeremy P

Well-known member
#2
Where can I find a list of the Django-style template variable filters for XF2? E.g. when you see {$someVar|number}
Taken from \XF\Template\Templater:
Code:
default
censor
currency
escape
for_attr
file_size
first
hex
host
ip
join
json
last
nl2br
nl2nl
number
number_short
pad
parens
pluck
preescaped
raw
replace
split
strip_tags
to_lower
to_upper
url
urlencode
zerofill
Also, how do I add one?
In a templater_setup event hook, you can do something like:
PHP:
$templater->addFilter('your_filter', function ($templater, $value, &$escape) {
    // ...
    return $value;
});
Secondly, how do I use view param in a macro? I.e. if I add a param: $reply->setParam('myParam', 'something'), how can I use it in any macro the associated template uses? This is a permission-like param that says whether a particular piece of info should be visible.
In XF2+, permissions should generally be implemented as methods on the entities they are associated with and called directly from the template/macro:
PHP:
<xf:if is="$content.canView()">
    ...
</xf:if>
Nevertheless, you can use a template_macro_pre_render event and add to the globals:
PHP:
public static function template_macro_pre_render(
    \XF\Template\Templater $templater,
    &$type,
    &$template,
    &$name,
    array &$arguments,
    array &$globalVars
) {
    $globalVars['foo'] = 'bar';
}
HTML:
{$__globals.foo}