how to compile template in model

Jake Bunce

XenForo moderator
Staff member
#2
Normally you would use template syntax to include the contents of another template:

Rich (BB code):
<xen:include template="template_name" />
No variable assignment is necessary.
 

marioman

Active member
#3
not in template
i have function in MyAddOn_Model_thread
i need to compile template inside php and return its contents to vatiable
some thing like $html = eval(template)
 

Robbo

Well-known member
#4
You shouldn't be doing it in a model. You should be doing it in a view or hook that is at the view stage. You can use the template hooks to get an instance of XenForo_Template_Abstract and the use create() to make your template. Or you can do it in a view and use $this->createTemplateObject().
 

Jake Bunce

XenForo moderator
Staff member
#5
not in template
i have function in MyAddOn_Model_thread
i need to compile template inside php and return its contents to vatiable
some thing like $html = eval(template)
Yeah. That kind of eval is not necessary in XenForo. The "include" syntax has replaced it.

Edit the template in question... the template where you would normally include $html. Instead of $html use this template syntax to include the template you want to eval:

Rich (BB code):
<xen:include template="template_name" />
 

Jake Bunce

XenForo moderator
Staff member
#7
Why does he want it in a variable? I assume to display the contents by referencing the variable in another template.
 

Robbo

Well-known member
#8
There are many reasons you might want to store a rendered template in a varible. I recently did it because of a system I made had different types. So you called $handlerInstance->render(). In render it create an instance of the template for that handler. This is called from within the template hook listener and will match certain hooks.
 

marioman

Active member
#9
i make add-on to me
when click stick in thread i get title, 5 lines from contents and first image in attachment ans store it in notice to appear only in forum have this thread
but i need to style this contents because notices accept html content so i need to compile template and store its html in my notice

so how can i do something like that
PHP:
$notice_html = compile('template_name', $viewParams);
i don't need to write html inside php
 
#10
i think this will work
i will test it

PHP:
$templateVariables = array( );

$template = new XenForo_Template_Public(Template_name, $templateVariables);
$html = $template->render();
 
F

Floris

Guest
#12
You're extending on &$contents

public static function templateHook($name, &$contents, array $params, XenForo_Template_Abstract $template)
{
// whatever
$temp_contents = $template->create('my_own_template', $template->getParams());
/* now that we have our custom template into $temp_contents, let's append it to $contents */
$contents = $contents . $temp_contents;
// whatever
}

And create a listener for the class where this function is in ..
 
Top