How to use XF's built in Template Parser?

Jaxel

Well-known member
So I am writing an addong, that takes a string, and parses it.
I would like to use XF's built in template parser, so that I can use code like:
Code:
{xen:if '$var>10', 'true', 'false'}
So if I have:
Code:
$values = array(
    'var' => '11'
);
$template = '<div style="color: {xen:if '{$var}>10', 'red', 'blue'}">test</div>';

How would I run this through the parser?
 
Last edited:
It will be hacky, but you could write a class that extends XenForo_Template_Public, overwrites the __construct() method that sets the params ($this->setParams($params); ), templateName (can be something random), and then takes the $templateName variable and sets Your_Class::$_templateCache['templateName'].

Then you should be able to do this:

PHP:
$rendered = Your_Class($template, $values)->render();

Very hacky and hasn't been tested, but theory is sound.
 
Code:
            $compiler = new XenForo_Template_Compiler($source);
             $compiled = $compiler->compile('sourceName', $style_id, $lang_id);
             eval($compiled);
             $readyToUseSource = $__output;
Source

It might makes the code faster slower to load though.
 
Last edited:
Top Bottom