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

Custom variables in my templates

Discussion in 'XenForo Development Discussions' started by ceribik, Jan 29, 2012.

  1. ceribik

    ceribik Active Member

    Say I have my own template named "MyTemplate". How can I give my template access to a custom variable no matter where the template is included from?

    From searching, I've found that most people do something similar to this: http://xenforo.com/community/threads/php-in-templates.7424/#post-104328
    However, I've found that this only works when my template is included from template such as sidebar_*, etc. My template can also be called from other templates like
    "ad_below_bottom_breadcrumb", but in this case, the variable isn't passed.

    I tried using a "template_create" listener, but that doesn't seem to be called when "MyTemplate" is made/included. Furthermore, I don't want to pass the variable onto every page using something like "PAGE_CONTAINER" for performance reasons.

    Any ideas?
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    When a template is employed using xen:include syntax (called inside of another template) then it shares the scope of the parent template. You can't add new parameters unless the template is called using the template object (in the PHP code).

    If a template hook is available then you can use it to render your template using the template object, specify your own parameters, and then append the output to the contents of the hook. Here is an example:


    What is your template used for? And what variables do you need?
    ceribik likes this.
  3. ceribik

    ceribik Active Member

    Hmm that method should work. Thanks! :)

    The template is used to display an array of users (which I'm storing in the DataRegistry.

    Edit2: I was actually planning on using a custom template hook and adding the variable to the params array via a template_hook listener, but that had the same issue as in the OP.

Share This Page