Dynamic Shortcodes Variables (get, set and let)
Updated over a week ago

Variables can be defined with either the for or the let Shortcode, these variables are only valid while their respective Shortcodes are being evaluated.

If a variable is redefined in a nested scope, the previous variable is hidden until the end of the nested scope (shadowing).

The for Shortcode is documented in its respective page.

Get Shortcode

Used to get the value of a variable. If the variable doesn't exist it throws an error. Example {get:x} {get:foo}

If you already are inside a shortcode you can just write $foo, which is just the same as writing {get:foo}, for example {+:$foo 1} instead of {+:{get:foo} 1}

Set Shortcode

Used to set the value of a variable. First argument is the variable name, second name is the new value. Examples: {set:x 3}, set the value of x to 3. {set:x {+: $x 1}} increment x by one.

Note: If you use the set Shortcode on an undefined variable, a variable will be created that is not bound to any scope, it will be valid also after the end of the current scope.

There is also set-default, it will set a value only if the specified variable has not already been initialized. If the variable has been initialized it does nothing.

Let Shortcode

The let Shortcode defines variables that will be valid only during the evaluation of its first argument. It can be useful if you need to use a value more than once. The variables are defined by assigning them to keyargs.

Simple Example

Here we are defining the variables greeting and name, and using them in the first argument.

{let: [{$greeting} {$name}!] @greeting='Hello' name='Joe'}

This will output "Hello Joe!"

Real usage Example

[Valid From {date:$x '-2 days'} to {date:$x '+2 days'}]

Here we are fetching the acf field mydate only once and then using it twice to print a date range from two days before it to two days after it.

Did this answer your question?