Variable Tags

Shortcut "Setters" and "Getters"

"Setters" is nerd speak for code that sets a variable. Likewise, "getters" is nerd speak for code that retrieves, or gets, a variable.

The simplest way to set a variable, is to use an opening and closing tag with content inside it. The variable name is the second tagpart (foo in the below example):

{!-- set the variable 'foo' to the inside content --}
{exp:ce_vars:foo}
<p>Bar</p>
{/exp:ce_vars:foo}

And a single tag to retrieve the variable:

{!-- get the variable 'foo' --}
{exp:ce_vars:foo}
The above would simply return <p>Bar</p>

The setting and getting of variables in the examples above, are shortcuts of the Set Content and Get tags, respectively. Take a look at those tags for more details on their available parameters.

Block

Returns the variable's content if it is set. Otherwise, it returns the tagdata as the default content.

Parameters

  • name= (required): The name of the global variable.

Example

{!-- This is our "layouts/_master" template --}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{exp:ce_vars:page-title default="My Site"}</title>
</head>
<body>
    <div id="content">
    {exp:ce_vars:content}
    <p>If you see this paragraph, then the content variable was not set!</p>
    {/exp:ce_vars:content}
    </div><!-- #content -->
</body>
</html>

This tag is particularly useful when you want default content to appear, unless it is intentionally overwritten when the template is extended.

Is Empty

Returns the tagdata if the specified variable is empty ('') or does not exist.

Parameters

  • name= (required): The name of the global variable.
  • trim=: Trim the leading and trailing whitespace from the passed in content? Can be "yes" or "no". The default is "yes".

Example

{exp:ce_vars:is_empty name="my_list}
    <p>There are no items in this list!</p>
{/exp:ce_vars:is_empty}

Not Empty

Returns the tagdata if the specified variable exists and is not empty.

Parameters

  • name= (required): The name of the global variable.
  • trim=: Trim the leading and trailing whitespace from the passed in content? Can be "yes" or "no". The default is "yes".

Example

{exp:ce_vars:not_empty name="profile"}
    <div id="profile">
    {exp:ce_vars:profile}
    </div><!-- #profile -->
{/exp:ce_vars:not_empty}

In the above example, the div with the id of "profile" is only created if the profile variable is set and is not empty (meaning it contains characters other than whitespace characters).

Is Set

Returns the tagdata if the specified variable is set.

Parameters

  • name= (required): The name of the global variable.

Example

{exp:ce_vars:is_set name="my_list}
    <ul id="my-list">
        {exp:ce_vars:my_list}
    </ul>
{/exp:ce_vars:is_set}

Just because a variable has been set, does not mean that it is not empty. A variable could be set to an empty string (''). Take a look Not Empty if you want to make sure the variable has been set and is not empty.

Set Content

Sets the content of a global variable.

You can also prefix the variable name with an asterisks to prepend the value to the variable, or append the asterisks to the variable name to append the value to the variable.

Parameters

  • name= (required): The name of the global variable.
  • default=: The default value to use in case the value that is passed in is empty.
  • trim=: Trim the leading and trailing whitespace from the passed in content? Can be "yes" or "no". The default is "yes".

Example

{!-- set the variable 'bar' to the tag contents --}
{exp:ce_vars:set_content name="bar"}
<p>Content</p>
{/exp:ce_vars:set_content}

{!-- append more content to the 'bar' variable --}
{exp:ce_vars:set_content name="*bar"}
<p>More content</p>
{/exp:ce_vars:set_content}

The above code would result in the value of the 'bar' variable being set to:

<p>Content</p>
<p>More content</p>

Set

Sets the tag parameters as global variables. Simplified version of the Set Content tag.

By default, ExpressionEngine® trims the whitespace from before and after parameter values. You can use asterisks inside the parameter value to preserve whitespace. Asterisks will be removed from values starting and ending with *.

You can also prefix the variable name with an asterisks to prepend the value to the variable, or append the asterisks to append the value to the variable.

Example

{!-- set the fruit and foo variables --}
{exp:ce_vars:set fruit="apple" foo="am"}

{!-- prepend 'I ' --}
{exp:ce_vars:set *foo="*I *"}

{!-- append ' happy' --}
{exp:ce_vars:set foo*="* happy*"}

The above would result in I am happy for the global variable 'foo', and apple for the global variable 'fruit'.

Get

Outputs the contents of a global variable, including variables set with the Set Content and Set tags.

Parameters

  • name= (required): The name of the global variable.
  • default= (optional): Allows a default value to be returned if the content that is passed in is empty.
  • trim=: Trim the leading and trailing whitespace from the passed in content? Can be "yes" or "no". The default is "yes".

Example

{!-- get the "foo" variable, and default to "My default text" if it doesn't exist --}
{exp:ce_vars:get name="foo" default="My default text"}

Parse

Parse on-the-fly variables and global variables. It will also parse variables with the var prefix of 'late:' (eg, {late:my_snippet}). That way, you can prevent your snippets from being parsed right away, and parse them when you’re ready to.

After parsing all of the other variables, the 'now:' variables will be parsed. The advantage of the now variables is that they will immediately return '' if a global by that name is not found.

Parameters

  • save_globals=: Set the variables as globals? Can be "yes" or "no", defaults to "yes".
  • default= (optional): Allows a default value to be returned if the content that is passed in is empty.
  • trim=: Trim the leading and trailing whitespace from the passed in content? Can be "yes" or "no". The default is "no".
  • order= (required): You can optionally define what you would like to parse, and in what order. Note that you can use any combination of options and you can use them multiple times, just make sure the options are pipe (|) delimited. The default order for this tag is: vars. The options to choose from are in the list below:
    • vars - Parses all of the variables in the 'default' order.
    • params - Parse variables passed in as parameters.
    • globals - Parse global variables.
    • segs - Parse the segment variables.
    • now - Now variables in the form {now:variable_name}. By default, EE will not parse a variable if its value is unknown. The now: variables are advantageous, as they will immediately return '' if a global variable by that name is not found, or the value of the variable if it is found.
    • conditionals - Parses all of the conditionals in the 'default' order.
    • simple - Parse simple segment conditionals and simple conditionals.
    • advanced - Parse advanced conditionals.
    • tags - Parse any exp tags.
    • exp - Parse any exp tags.
    • lists - Parse all or part of a list. The list variables should be in the format: {list_name:start_row,end_row} (if no end_row is specified, only the start_row will be returned) or {list_name|start_row,length} (if no length is specified, the entire list from start_row on will be returned)

Example

The following example would replace {title}, {styles}, and {scripts} in the {header_snippet} with the variables set in the tag’s parameters.

{exp:ce_vars:parse
   title="Home Page"
   styles='<link href="/css/styles.css" rel="stylesheet" type="text/css">'
   scripts='<script href="/scripts/custom_script.js" type="text/javascript"></script>'
}
{header_snippet}
{/exp:ce_vars:parse}