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

Dynamically appending templates

Discussion in 'XenForo Development Discussions' started by Naatan, Jul 18, 2012.

  1. Naatan

    Naatan Well-Known Member

    I'm developing a small widget for an upcoming addon that I'd like to dynamically append based on an option. I will either use template hooks or embedded templates as places where this can be inserted.

    The only problem is, I can't really figure out a way to dynamically embed a template inside another template.

    For example, say PAGE_CONTAINER is being loaded and it calls template_create event for the template 'ad_below_top_breadcrumb', now I want to append another template (let's call it 'sample_template') right after 'ad_below_top_breadcrumb'.

    Is this possible? I'd prefer to do this without template edits (be they manual or otherwise).

  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Well, the template_create event doesn't apply to inlined templates like the ad_* templates. It only applies when the template object is invoked (ie content and container templates).

    To add content inside of an existing template ideally you should use a template_hook (if one is available). Code example:


    If there is no hook available then you can use template_post_render to hack away at the finished HTML. Code example:


    template_post_render also only applies to content and container templates, not inlined templates.
  3. Naatan

    Naatan Well-Known Member

    Thanks Jake, for my particular use-case I went with hooks as they do the job well enough, sometimes hooks are not available though.

    I did come up with a solution, so for anyone looking into this in the future and reading this post; here's how you do it -

    Simply hook into template_create, and "extend" the template you want to append / prepend to by returning your own custom template by renaming $templateName, then simply programmatically append / prepend the original template that was being called to your custom template.

    Of course it would be preferable that you do the prepend / append to your custom template beforehand so that when the relevant template is accessed it does not have to do any extra db queries.

    Hope that helps someone else out there.

Share This Page