XF 2.0 XenForo 2 Helper Function

Hi everyone,

I'm looking to be able to use the XenForo 2 equivalent of "{xen:helper (function), $var}". I can't seem to find any documentation on this topic. Can anyone guide me in the right direction to creating my own helper (I already have my old code, just need to port it)

Thanks!
 
Listener.php

Code:
public static function templater_setup(\XF\Container $container, \XF\Template\Templater &$templater)
{
        $templater->addFunction('YOUR_FUNCTION', function(\XF\Template\Templater $templater, &$escape)
        {
           // CODE
        });
}

admin.php?code-events/listeners/

Add code event listener -> templater_setup

Template Code: {{ YOUR_FUNCTION($vars) }}


Something like that :)
 
Listener.php

Code:
public static function templater_setup(\XF\Container $container, \XF\Template\Templater &$templater)
{
        $templater->addFunction('YOUR_FUNCTION', function(\XF\Template\Templater $templater, &$escape)
        {
           // CODE
        });
}

admin.php?code-events/listeners/

Add code event listener -> templater_setup

Template Code: {{ YOUR_FUNCTION($vars) }}


Something like that :)
Thanks, I tried this but had some problems. Here is what I input
NbRFapy.png

UHTVrVC.png
[/SP
krrfocT.png
OILER]

I get this error:
  • Template public:tkgmod_servers: Function steamid64fromuserid is unknown (src/XF/Template/Templater.php:861)
 
Yep

Code:
public static function templater_setup(\XF\Container $container, \XF\Template\Templater &$templater)
{
        $templater->addFunction('YOUR_FUNCTION', function(\XF\Template\Templater $templater, &$escape, $var1, $var2 etc.)
        {
           // CODE
        });
}


{{ YOUR_FUNCTION($vars1, $var2 etc.) }}
 
@Arty: you linked to me talking about not using custom template tags because there are situations where listeners aren't enabled when rebuilding templates. That doesn't apply to templater functions though, as they're purely run-time evaluated.

Do you have an example you're referring to? The RM and MG both use custom templater functions without issues. (Certainly there could be situations if you edit a template to include a function call and then disable the add-on, though arguably a callback may fail in that scenario, depending on the code in question.)
 
Back
Top Bottom