Comment on page

Using Mod Variables

Adding a Mod Variable

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.

Reading a Mod Variable

To Read a Mod Variable, use the @mod variable 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 null:
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.

Viewing Mod Variables in Data Panel

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.
You can read more about Page State in the “Using Page State (Advanced)” Section.

Other Bricks That Can Use Mod Variables

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

Examples and Uses

Example: Running a Mod Only Once

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 @mod.modHasRun
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:

Example: Appending to an Array

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: