Intermediate variables
In complex recipes, you often create sub-components (like a dough, a sauce, or a frosting) that are then used in later steps. Gram allows you to declare these sub-components as Intermediate Variables.
Once declared, an intermediate &variable acts exactly like an @ingredient, allowing you to reference it, measure it, or calculate percentages from it.
Declaring a variable
Section titled “Declaring a variable”You can declare a &variable using the ->& syntax. There are two places you can declare them: at the end of a Step, or at the end of a Section Title.
Step-level declaration
Section titled “Step-level declaration”When placed at the very end of a step (a paragraph), the &variable captures everything produced in that specific step.
Section-level declaration
Section titled “Section-level declaration”When placed at the end of a ## Section title, the &variable captures the output of the entire section. This is perfect for components that take several steps to prepare.
Using a variable
Section titled “Using a variable”To use a previously declared &variable, simply reference it with &.
You can also assign a quantity to it for display purposes in the rendered recipe:
Advanced mechanics
Section titled “Advanced mechanics”Global scoping
Section titled “Global scoping”When a &variable is declared at the Section-level, it is registered in the Global Scope. This means you can declare a &variable in the first section of your recipe and safely reference it in the last section.
Relative quantities
Section titled “Relative quantities”As detailed in the Relative Quantities documentation, you can calculate the mass of an ingredient based on the total mass of an intermediate variable.
Error handling
Section titled “Error handling”The compiler helps you maintain clean and conflict-free code by emitting specific warnings:
- Scope Conflict: If you accidentally declare the same
&variablename twice in different## Sectionblocks, the compiler will issue aSCOPE_CONFLICTwarning. - Unused Variable: Any
&variablethat is declared using->&must be used later in the recipe. If you declare a&variablebut never reference it, the compiler will trigger a warning to help you keep your recipe code clean.