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

Listen for template loading when xen:include used

Discussion in 'XenForo Development Discussions' started by Sim, Mar 25, 2015.

  1. Sim

    Sim Well-Known Member

    There is a template I want to insert additional data into whenever it is rendered, but it is never called directly - only ever via xen:include and from at least 6 different parent templates.

    Is there a way of listening for this sub-template to be loaded and then execute my code to insert the additional data (which gets added to the sub-template via a template mod) ?

    Currently I am extending each of the various view classes associated with the parent templates and inserting my data at the parent level, but it's the same code over and over again and it would be nice to only listen for the sub-template being rendered and have one set of code execute.
  2. Chris D

    Chris D XenForo Developer Staff Member

    The template_create event ought to do it.
  3. Sim

    Sim Well-Known Member

    Nope, it seems that only the parent template triggers that event.

    I put one of the parent template names as the event hint and it triggers fine.

    But when I put the xen:include template name as the hint, I get no triggering.
  4. Chris D

    Chris D XenForo Developer Staff Member

    Too late for me. I misread some of your message.

    You could look at using a xen:callback tag instead of xen:include.

    That can call a static PHP function from a template, where you can then add your additional data and render a template.

    You can see an example of its usage in XenForo Media Gallery. Search templates for <xen:callback and the file Callback.php in library/XenGallery
  5. Sim

    Sim Well-Known Member

    Okay - thanks Chris, I see how that works.

    I think it would do what I need, but I would also need to put in template mods for each of the parent templates to change the xen:include to a xen:callback.

    Although it is certainly less code using a callback, I'm not sure it's more robust (subject to unexpected breakage if core templates ever change?) or easier to manage than simply extending the view classes and decorating them with additional information like I'm already doing.

    I think it might come down to a coin toss as to which is the better way, and since I'm already 95% of the way there, I might just stick with what I've got.

    Thanks anyway Chris - I think xen:callback might come in very handy in the future.

    EDIT: although this all being said - there is the whole "extending a fake base class" issue to deal with doing things my way, which I hadn't really taken into account yet.

Share This Page