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

How to use XF's built in Template Parser?

Jaxel

Well-known member
#1
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:

Jeremy

Well-known member
#2
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.
 

cclaerhout

Well-known member
#3
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: