• ♡ Sponsor

    Control Structures

    #Condition #Context #Loop #Statement

    As in other languages, control structures in Automad are statements that dynamically determine the flow of an application or website based on evaluating content or variables. Automad knows four different statements to control the flow of the application: for, foreach, if and with. In contrast to function calls, control structures are always wrapping blocks of code and can therefore also be nested.

    Usage

    Control structure have the following syntax:

    <@ foreach in filelist {options} @>
        <# Code to be executed for every file goes here. #>
    <@ end @>
    

    Some statements can also have an else block:

    <@ foreach in pagelist @>
        <# Code block to be executed for every page. #>
    <@ else @>
        <# Code to be executed in case the pagelist is empty. #>
    <@ end @>
    

    More in Control Structures

    • for
    • foreach
    • if
    • with

    Related

    Template Language  ⁄
    Variables — Template Language

    The actual value of a variable is depending on the current context. In most cases the context is the currently requested page. However, control structures can change the context to any other page during runtime, for example when iterating over a list of pages. In case a variable ...

    Variables  ⁄
    Runtime Variables — Variables

    There are some variables defined by Automad during runtime. The names of those runtime variables are always preceded with a colon as follows: @{ :basename } Runtime variables can be subdivided into two types - context related variables and variables created within control ...

    Template Language  ⁄
    Toolbox — Template Language

    The Toolbox provides a collection of essential functions for templates. While some are just convenient shorthand functions for more complex statements, others provide the main interface to interact with objects, create redirects and work with query strings.

    Control Structures  ⁄
    for — Control Structures

    The for loop iterates over a range of numeric values of type integer.

    Control Structures  ⁄
    foreach — Control Structures

    The foreach loop iterates over objects. Depending of the type of the object in use, Automad provides different runtime variables within the code block of the loop to refer to items of the object. The :i variable refers to the index of the current iteration in the loop.

    Control Structures  ⁄
    if — Control Structures

    The if statement allows for conditional processing of code blocks. In case the given expression is evaluated to false, an optional else block is processed instead.

    Control Structures  ⁄
    with — Control Structures

    The with statement allows for processing code associated with a given page or file. When using a page's URL or one of the keywords prev and next as an argument, the context within the block of code changes to that page. When using a file path, it is possible to access related ...

    Template Language  ⁄
    Snippets — Template Language

    The snippet statement allows for defining reusable code snippets and is therefore also the basis for any kind of recursion in Automad. A snippet always inherits the context of the calling code. To use a snippet, it is enough to put the snippet's name in statement delimiters.

    Template Language  ⁄
    Includes — Template Language

    In many cases, different templates across a theme use recurring components such as a header, a navbar or a footer. Those elements can be stored in separate files and included whenever needed. To include such an external snippet, simply wrap its path relative to the current file ...

    Template Language  ⁄
    Using Extensions — Template Language

    Extensions provide a convenient way of turning custom PHP code into Automad statements. Besides the fact that extensions have to be called by their names including their namespace, the syntax is the same as for Toolbox or pipe functions.

    • Automad
    • Developer Guide
    • Building Themes
    • Template Language
    • Control Structures
    Discuss
    Packages
    Release Notes
    Support
    Terms of Use
      Become a Sponsor
      GitHub
      Twitter
      Facebook
      Instagram
      2013-2022 by Marc Anton Dahmen
    Released under the MIT license

    A faceless machine, creating one's portrait in the internet.
  • Automad
  • Getting Started
  • System
  • User Guide
  • Developer Guide
  • Headless Mode
  • Discuss
  • Packages
  • Developer Guide ←
  • Building Themes ←
  • Template Language ←
  • Control Structures
  • for
  • foreach
  • if
  • with