Retired Documentation:  You are using the documentation for version 1.7.3 which was retired in 2013. Go here for the latest version documentation or check here for your available upgrades to the latest version.

Dynamically Assigned Variables

ExpressionEngine allows you to declare template-level "variables" to save time typing and editing a template where certain bits of text might get used multiple times. Assigned Variables act as a straight string replacement for use later in the same template.

Variable assignment and replacement occurs instantly when the template is loaded and may not be affected by the result of another tag's output.

Creating and Using an Assigned Variable

The basic syntax for creating a variable is this:

{assign_variable:variable_name="variable_replacement"}

In the above example, "variable_name" is the name of the variable and "variable_replacement" is the content that will be used to replace all occurrences of {variable_name} in the template.

Example Usage

A common usage for dynamically assigned variables is to hold the Weblog short name(s), to save you from repeatedly typing them into different tags within the same template.

{assign_variable:weblogs="news|reviews"}

{exp:weblog:category_heading weblog="{weblogs}"}
    <h1>{category_name}</h1>
    {if category_description != ""}<p>{category_description}</p>{/if}
{/exp:weblog:category_heading}

{exp:weblog:entries weblog="{weblogs}" limit="10"}
    <h2>{title}</h2>
    {body}
{/exp:weblog:entries}

Note to developers: In this context, the word "variable" is used simply to mean the use of an ExpressionEngine style variable in the template to substitute for some other text. It is not a "variable" in the sense of being an accessible and modifiable value held in memory. It is a simple text replacement that occurs the moment the Assign Variable tag is encountered by the template parser.

Top of Page