XF 2.2 Can I Use A Macro To Store A Template Variable?

Pawn Studios

Active member
There is an array that I use throughout templates and it would seem reasonable to use a macro to store it as the documentation states: "use a macro any place you want to produce the same thing multiple times in multiple different files".

I would like to then be able to pass it to a callback.

Here's what I tried:

A template called my_macro

<xf:macro name="get_static_data">
    <xf:set var="$static_data" value="{{ [1, 2, 4, 4] }}"></xf:set>

Another template:

<xf:macro template="my_macro" name="get_static_data" />

<xf:callback class="Addon\MyCallback" method="getHTML" params="{$static_data}"></xf:callback>

But I get an error logged: Argument 4 passed to XF\Template\Templater::callback() must be of the type array, null given
So I supposed $static_data isn't set and it's null.

I kind of realize macros are probably used to emit HTML. But I can't say I've grasped template processing enough to conclude that content emitted by macros doesn't get a pass by the template syntax parser. But again, my code would have to then be modified to emit xf:set var instead of it being processed within the macro.

I think the better question is, can xf:set create a global variable.
Last edited:

Jeremy P

XenForo developer
Staff member
Template and macro variables are locally scoped (though included templates inherit variables from their parent, and macros can opt-in to a similar behavior). Think of macros as functions, but for templates. They are good for creating reusable components with variable arguments that you can call from other templates and macros, but they do not modify the state of the caller (beyond the outputted HTML).

I'm not sure of your exact use case, but there is probably a better way to accomplish what you're after (perhaps using code events to set the params in the pertinent templates, or hard-coding it in the callback if that's the only place you use it).