I do not know how to program in PHP, but I am trying to understand how I can set dynamic variables in templates. For example, if I use the query tag, how can I create a few dynamic variables that i can place the value of in a few different areas on the template? I have been looking at low variables and the standard template variables, but don't see how to make them work dynamically. Can this be done?

1 Answer 1


There are two ways that you can do this, one built in to EE's core function, the second using a widely used add-on.

Almost all dynamic variable use cases can be met using layout variables; a system that allows you to create and store single variable values and simple arrays during the evaluation of a template. You can store anything that can be generated as output by an EE template within a layout variable.

The quirk with layout variables is that the variables you define only become available for use after parsing of the template is complete, so the only way to use them within your page is to concurrently use EE's template layout system - which is not a bad thing, the template layout system is helpful and powerful. It lets you break the parsing of a template into the accummulation of the processing of two or more component templates: layout variables defined in a component template become available to the next (and all subsequent) templates in the sequence.

The alternative solution is to use an add-on called stash. This widely used add-on provides (amongst other things) a very similar mechanism for storing local / temporary values to that provided by layout variables, the primary difference being that stash variables become available for re-use upon definition (no need to wait until the template evaluation is complete).

Stash is a complicated add-on and has complicated documentation to go with it that at times can be confusing to follow, though using it for local variable storage is clearly explained.

By comparison layout variables do less (but usually enough) but are simpler to work with, though to get the best from them you need to learn also to structure your template designs to use the template layout system.


  • First, thank you for such a detailed explanation and it helps quite a bit. I can see where both will help. I am trying both and was able to get stash to work with one of their basic examples so I know the add-on is working. I am trying to do something like this, but it does not seem to be working. Can this work? {!-- set a variable --} {exp:query sql="select distinct category_primary from product_category where cat_url_slug = '{segment_2}'"} {exp:stash:set name="foo"}{category_primary}{/exp:stash:set} {/exp:query} {!-- get a variable (prints "bar") --} {exp:stash:get name="foo"}
    – Jeff
    Dec 28, 2021 at 16:51
  • I'm not 100% sure how you solve it with stash: if it is not working it probably is something to do with parse order - a universal thing to try in such circumstances is adding parse="inward" to your stash tag. Stash several options for messing with parse order - see the wiki. An easier way: put your query tag into an empty template and save the values as a layout variable(s) there, link from that template to a second (add {layout='...2nd template..'} as first line of first template) then retrieve the layout variable(s) on the linked layout template: can be sure of parse order this way. HTH Dec 28, 2021 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.