Comment on page
Using Mod Variables
To assign a Mod Variable, use the Assign Mod Variable Brick in the “Store/Retrieve” Category:
In the Brick Configuration, provide a Variable Name and the Value. The Value can be of any type, and you can provide a variable.
To Read a Mod Variable, use the
@modvariable in a variable or template expression.
PixieBrix fills in the variable with the current value at the time the brick is run.
If the mod variable has not been assigned yet, it’s value will be
Referencing a Mod Variable in a Text Template
Reading Multiple Mod Variables at Once (Advanced)
In certain situations, you need to use multiple related mod variables across more than one brick. If you access the mod variables via
@mod, the values may change between bricks.
To read a single snapshot of all variables, use the Get Shared Page State brick described below in the Using Page State section. That brick can read all Mod Variables and assigns them to a local variable.
To view the Mod Variables at the time the brick was run, use the “Context” tab in the Data Panel:
Viewing the Mod Variables when the brick was run
When editing the Starter Brick or Assign Mod Variable brick, the Data Panel will also include a “Page State” tab that shows the current Mod Variable Values.
Other bricks in PixieBrix can take advantage of Mod Variables:
- Trigger Starter Brick: can listen for changes to Mod Variables
- Sidebar Starter Brick: can re-render on changes to Mod Variables
- Custom Form: fields can be bound to a Mod Variable
- Display Temporary Information: can be set to re-render on changes to Mod Variable
- With Async Page State: runs one or more bricks asynchronously and keeps track of the result in a Mod Variable. Great when showing the result of API calls in a Sidebar
To run an mod at most once, use a Mod Variable brick to track if the mod has already been run.
Add a “Cancel Current Action” brick and specify the condition as
Using a mod variable. PixieBrix shows a variable warning because there’s no brick that has set the mod variable yet.
Then, add a “Assign Mod Variable” brick that sets the value of the Mod Variable to true when the mod runs:
To append to an array, use the JQ - JSON Processor Brick to append the item, and the Assign Mod Variable Brick to assign the result to a Mod Variable:
Note the use of JQ’s default operator
//to default to a blank array if the array is not already set:
Then, assign the value to a mod variable using the Assign Mod Variable brick: