XF 2.1 Add additional macros via template include?

Nulumia

Well-known member
I'm pretty sure I've seen this done before, but can't seem to track down where :unsure:..

Let's say you have a typical macro template, such as:
Template: some_template_macros
HTML:
<xf:macro name="version_one">
    // stuff
</xf:macro>

<xf:macro name="version_two">
    // stuff
</xf:macro>

Then you call it with:
HTML:
<xf:macro template="some_template_macros" name="version_one" />
Next, I use regex modification to insert an include to end of file, which will contain additional macros (to hold code outside of the modification and keep things clean):
HTML:
 ..
 </macro>
 <xf:include template="my_additional_macros" />

Then in the template: my_additional_macros
HTML:
<xf:macro name="version_three">
    // stuff
</xf:macro>

<xf:macro name="version_four">
    // stuff
</xf:macro>

In all cases where I do something like <xf:macro template="some_template_macros" name="version_four" />, this turns up blank. I figured setting the modification which inserts the template include to Execution: 1 would ensure the macros are present would help, but doesn't.

Is this possible?
 
Not really, including the my_additional_macros template doesn't cause its macros to be compiled into the some_template_macros template. If you didn't want to include the macro definitions in the template modification itself, you would have to call the macros on the my_additional_macros template instead.
 
Not really, including the my_additional_macros template doesn't cause its macros to be compiled into the some_template_macros template.
That was my rough suspicion, but so weird as I could've sworn I chanced upon this in another addon's modifications o_O. I suppose there's no downside to having hefty sized modifications? I was hoping for a more ideal method of keeping bulky code in viewable templates.
 
Unless it's unavoidable I try to limit template modifications to including other templates or calling macros, but that's really just a personal preference. I mostly like the convenience of being able to view/edit the templates/macros in a proper editor.

In your case I would probably create vendor_some_template_macros and then call the macros directly on that template instead, but there's no real downside to placing them directly in template modifications either.
 
Unless it's unavoidable I try to limit template modifications to including other templates or calling macros, but that's really just a personal preference. I mostly like the convenience of being able to view/edit the templates/macros in a proper editor.
This! (y)
In your case I would probably create vendor_some_template_macros and then call the macros directly on that template instead, but there's no real downside to placing them directly in template modifications either.
Originally my idea was that it would be easier/cleaner to simply swap out the name="" part of a macro call as a conditional variable, instead of conditionally calling an entire different macro (looks messy in the template or modification). However this is still better than handling large modifications as you mentioned - thanks for the input :)

Edit: *Goes to try conditionally swapping out the template="" segment..
 
Top Bottom