set

Lets you set and read your own variables using b@se tags

Example

Setting the tag:

[%set [@unixdate@] %]
    [%format type:'date' format:'#U'%][@date_created@][%/format%]
[%/set%]

Using the tag:

[@unixdate@]

Usage

The set function allows you to generate your own tags using B@se. Once set, this tag allows you to treat it just as any other tag in Maropost Commerce Cloud, you can use it in parameters, inside any other tag on the page, perform logic on it, etc.

In the example above, we use a format tag to turn an existing tag, [@date_created@] into a unix time code. Then we store the new timecode as it's own new tag, [@unixdate@] which we can then use on the page.

Scoping

By default, a variable's scope is limited to the function param it was defined in - see the example below:

[%thumb_list type:'products' limit:'10'%]
    [%param *header%]
        [%set [@my_variable@] %]true[%/set%]
        <!-- my_variable will output "true" here -->
        [@my_variable@]
    [%/param%]
    [%param *footer%]
        <!-- my_variable will output nothing here -->
        [@my_variable@]
    [%/param%]
[%/thumb_list%]
<!-- my_variable will output nothing here -->
[@my_variable@]

Caret to the rescue!

The caret character (^) can be used to hoist a variable's scope up one or more levels - see the updated example below:

[%thumb_list type:'products' limit:'10'%]
    [%param *header%]
        <!-- Here we use the ^ char in front of the variable name to set the scope up 1 level -->
        [%set [@^my_variable@] %]true[%/set%]
        <!-- my_variable will output "true" here -->
        [@^my_variable@]
    [%/param%]
    [%param *footer%]
        <!-- my_variable will output "true" here -->
        [@my_variable@]
        <!--
        Notice how we didn't need to specify the ^ char this time? There is no my_variable declared
        in this scope, so it is inherited from the parent scope.
        -->
    [%/param%]
[%/thumb_list%]
<!-- my_variable will output "true" here -->
[@my_variable@]
<!--
Because my_variable was declared up 1 scope level, we can now access it outside of the thumb_list
function!
-->

Multiple caret characters can be used to hoist the variable up many levels of scope if needed. For example, if you were writing code 3 functions deep and wanted to make a variable available at the full page level, you could declare it as [@^^^my_variable@].

As seen in the example above, if a variable is not defined in the current function param scope, it will be inherited from the parent scope. If you just want to read an inherited variable, the carets can be left off for cleaner syntax. If you want to set the variable you will need to explicitly specify the scope using the caret characters though.

The 'session' function

Note: Session tags do not work with varnish caching enabled on your site. Most sites have caching enabled to speed up page access.

Using the special value tag session, you can use set tag to store data to a variable that is then accessible site wide.

To do this, we use the session short hand like so:

[%set [@session:globalExample@]%]
    Example data
[%/set%]

Then you can load this new session value tag on any other page or template and it will return your value, so setting the above on the home page, then loading the following on the about us page:

[@session:globalExample@]

Will print:

Example data

Was this article useful?

Be notified when this page is updated. Optional.