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

Named Template Reuse?

Discussion in 'XenForo Development Discussions' started by Tom Christian, Nov 2, 2014.

  1. Tom Christian

    Tom Christian Active Member

    I want to create several different SP dropdowns using a named template. Is it possible to only create one named template with the necessary code and then parse to it the necessary options/vaues from other SP's ?

    Basically, I want to save time by using a single named template rather than creating a unique one for every single dropdown menu. Is this possible?
  2. Chris D

    Chris D XenForo Developer Staff Member

    Check out this Admin Template:


    It is a style property named template and it is only used once.

    But what you will see is how variables are used in various places, those variables grab values from the SP definition therefore it should allow the template to be reused.
    Tom Christian likes this.
  3. Tom Christian

    Tom Christian Active Member

    That seems like a fairly simple example - but what about for a more complex situation such as varying number of options for an HTML select menu?

    What I don't understand is how I can define the select menu option/values inside the SP Definition and then access those with a loop or something inside the Named Template.
  4. Chris D

    Chris D XenForo Developer Staff Member

    Yeah, for that, you would likely need a separate template for each different set of options.

    You can include templates in templates, though; and you can also do foreach loops in the template. But the problem you will face, is that it isn't possible to build an array and pass it to another template.

    You might be able to have a separate template for each SP, but include a template that contains common code, if that saves time.

    Unfortunately, I think Style Properties are a little bit underpowered in this circumstance. Certainly what you want to do would be more likely to be possible with add-on options, but styles and add-ons are completely separate. e.g. You can assign options to add-ons and you can assign style properties to add-ons, but you can't assign options to styles.

    That's where this suggestion comes in:

    There's one more thought I'm having but I haven't actually had a chance to test it, yet.

    You can include PHP in templates via the xen:callback tag. In theory, you could call the PHP from the template, pass some parameters to it, and return a different template or, e.g. a different array of options based on those parameters.

    Certainly would work with public templates in some fashion, haven't ever tested it in Admin templates.
  5. Tom Christian

    Tom Christian Active Member

    That's a shame. I'll just have multiple templates for now. Hopefully they'll consider your suggestion as it seems very strange to me also.

    Thanks for the help.

    Oh, one last question actually - IPB has a very simple way of returning the list of categories & forums (nodes) into a multi-select HTML element. This then outputs an array of node ID's for the selected options. Do you know if anything like this exists within XenForo. Presumably, it would need a named template and then some template code to output the node list. Is that possible?
  6. Chris D

    Chris D XenForo Developer Staff Member

    Same story, unfortunately.

    If you are creating an add-on, you can do this very easily with options using a PHP callback.

    Also, it's worth noting, Style Properties can only save and output scalar values.
    Steve F likes this.
  7. Tom Christian

    Tom Christian Active Member

    The multi-select would be for an addon actually. Is there any documentation in regards to outputting the node list or perhaps an addon you know of that I can look at to see how it's done ?
  8. Chris D

    Chris D XenForo Developer Staff Member

    Tom Christian likes this.
  9. Tom Christian

    Tom Christian Active Member

    Awesome, thanks so much for the help :)
    Chris D likes this.

Share This Page