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

Get HTML output of template?

Discussion in 'XenForo Development Discussions' started by Despair, Mar 11, 2011.

  1. Despair

    Despair Active Member

    Is there an easy way to grab the rendered HTML output of a template of for example say "page_container_js_head". This would be outside the forums if it makes a difference. I've only done the bare basics initializing the XenForo app, but I'm a little clueless as to what to do next. Since the templates have params hopefully it's not too complicated to do.

  2. Shadab

    Shadab Well-Known Member

    Wuebit and Despair like this.
  3. Despair

    Despair Active Member

    Thank you Shadab, somehow I was not able to find that thread myself. Read most of the ones regarding templates except that one. :p Probably would have had better luck if I actually used the search here instead of googling...

    Anyways, in my case, I need the $jQuerySource as a param, I had a look in the dependencies and saw it there, but it just states whether it is local or not. Do you know of a method to fetch the URL? I have another question, but I think I'll leave it for next time as it's getting a bit late here.

    Before I forget, I wanted to thank you again, you are a great help in the development forums. :)
  4. Shadab

    Shadab Well-Known Member

    Thanks for the kind words. :)

    I'll need to find a way to reuse the container parameters.
    For now, you can grab the values for jQuerySource and jQuerySourceLocal from:
    • XenForo_Dependencies_Public::getJquerySource(), and
    • XenForo_Dependencies_Public::getJquerySource(true) respectively.
    Vodkaholic likes this.
  5. Shadab

    Shadab Well-Known Member

    Found it. The dependency class has a getEffectiveContainerParams() method.
    Haven't tried, but this should work:

    // Your custom template parameters, if any
    $viewParams = array(
    'foo' => 'bar'

    // Merge the container parameters when rendering any non-content template
    $params $dependencies->getEffectiveContainerParams($viewParams$request);

    $content $viewRenderer->renderView(''$params'page_container_js_head');
    Despair likes this.
  6. Despair

    Despair Active Member

    Templates that contain <xen:require /> don't seem to get the required js/css show up. And <!--XenForo_Require:JS--> is printed as a comment rather than replaced with the scripts. Any ideas about this one? Thanks.
  7. Shadab

    Shadab Well-Known Member

    Could you try this...

    Instead of rendering the template directly via renderView(), grab a template object from the view renderer using createTemplateObject(). Render the template manually; and then pass the rendered contents + the template object to replaceRequiredExternalPlaceholders().

    Basically, that's how renderContainer() works.
    $template $viewRenderer->createTemplateObject('your_template_name'$params);
    $content $viewRenderer->replaceRequiredExternalPlaceholders($template$template->render());
    But there's one catch. This won't work if you are building your page in a typical procedural style: header first, content second and footer last. That's because the external dependencies (css and js filenames) are not known until after you render the content templates. So you'll have to render the page chrome/container after you finish evaluating all the content templates; if you are not doing this already.
    Despair likes this.
  8. Despair

    Despair Active Member

    Ah, so that is how you do it, I was doing something weird... This now seems to be working perfectly. (y)

Share This Page