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

how to compile template in model

Discussion in 'XenForo Development Discussions' started by marioman, Oct 17, 2011.

  1. marioman

    marioman Active Member


    i need to compile a template and insert it's html contents in variable to use it
    how can i use that ?
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Normally you would use template syntax to include the contents of another template:

    <xen:include template="template_name" />
    No variable assignment is necessary.
  3. marioman

    marioman Active Member

    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)
  4. Robbo

    Robbo Well-Known Member

    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().
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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:

    <xen:include template="template_name" />
  6. Robbo

    Robbo Well-Known Member

    He wants it in a variable, not to show it in a template...
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Why does he want it in a variable? I assume to display the contents by referencing the variable in another template.
  8. Robbo

    Robbo Well-Known Member

    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.
  9. marioman

    marioman Active Member

    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
    $notice_html compile('template_name'$viewParams);
    i don't need to write html inside php
  10. marioman

    marioman Active Member

    i think this will work
    i will test it

    $templateVariables = array( );

    $template = new XenForo_Template_Public(Template_name$templateVariables);
    $html $template->render();
  11. marioman

    marioman Active Member

    not work
  12. Floris

    Floris Guest

    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 ..
  13. SheepCow

    SheepCow Well-Known Member

    This is how you can do this, although I don't recommend doing it in a model:

    $htmlContent $template->create('template_name',
    'param1' => 'value 1',
    'param2' => 'value 2'
    Taken from https://github.com/sheepcow/XenQuot...brary/XenQuotation/Event/TemplateHook.php#L37

    edit: it does require you to have a template object, you can always create one if you don't have it lying around

Share This Page