Dynamically appending templates

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

Thanks
 
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'.

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:

http://xenforo.com/community/threads/how-to-create-a-bridge.28515/#post-331631

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

http://xenforo.com/community/threads/adding-own-thread-search-criteria.20193/#post-261191

template_post_render also only applies to content and container templates, not inlined templates.
 
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.
 
Top Bottom