As in other languages, control structures are statements that dynamically determine the flow of rendering based on the evaluation of content and variables. Automad knows four different control structures: for, foreach, if and with. In contrast to function calls, control structures always wrap a block of code and can therefore also be nested.
Syntax
A control structure starts with an opening statement and is closed by an end statement. Some control structures also accept an options object:
<@ foreach in filelist { width: 400 } @>
<# Code to be executed for every file goes here. #>
<@ end @>
All control structures except for can also have an optional else block that is processed in case a list is empty, a condition is not met or a page or file doesn't exist:
<@ foreach in pagelist @>
<# Code to be executed for every page. #>
<@ else @>
<# Code to be executed in case the pagelist is empty. #>
<@ end @>
Available Control Structures
| Statement | Description |
|---|---|
| for | Iterates a range of integer values. |
| foreach | Iterates collections and lists of files and changes the context while iterating pages. |
| if | Processes a block of code conditionally. |
| with | Processes a block of code within the context of a given page or file. |