• ♡ Sponsor

    with

    #Context #File

    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 runtime variables and, in case the file is an image, you can directly apply options for resizing and cropping. If there is no matching page of file, an optional else block is processed instead.

    Changing Context

    Using URLs

    When using a page URL, either by string or variable value, the context within the statement will change to the given page, in case the page exists. All variables used in the statement's code are then associated with that page automatically. The else block is optional.

    <@ with "/page/url" @>
        @{ title }
    <@ else @>
        Page doesn't exist!
    <@ end @>
    

    Using a variable as argument:

    <@ with @{ page } @>
        @{ title }
    <@ end @>
    

    Previous and Next Pages

    Instead of a page URL, it is also possible to use the keywords prev and next to change the context to the previous or next item in the pagelist.

    <@ newPagelist { type: 'siblings' } @>
    <@ with prev @>     
        <a href="@{ url }">
            Previous: @{ title }
        </a>
    <@ end @>
    <@ with next @>
        <a href="@{ url }">
            Next: @{ title }
        </a>
    <@ end @>
    

    Using Files

    Passing a file path as argument allows for using the file's metadata and runtime variables in the statement's code block. In case the file is an image, resizing options can be passed as well. The file path can be either a full path or a glob pattern. When using glob patterns, the first match will be used.

    <@ with @{ logo | def('/shared/*logo*') } {
        width: 100,
        height: 100,
        crop: true
    } @>
        <img
        src="@{ :fileResized }"
        alt="@{ :basename }"
        width="@{ :widthResized }"
        height="@{ :heightResized }"
        title="@{ :caption }"
        />
    <@ else @>
        <# Use sitename in case ther is no logo. #>
        @{ sitename }
    <@ end @>
    

    Related runtime variables:

    :basename
    The basename (filename) of the file.
    :caption
    A caption associated with the current file in a with statement.
    :file
    The resolved file path of the current file.
    :fileResized
    The file path of the resized version of the file.
    :height
    The height of the file.
    :heightResized
    The height of the resized version of the file.
    :width
    The width of the file.
    :widthResized
    The width of the resized version of the file.

    Related

    User Guide  ⁄
    Linking to Files and Pages — User Guide

    There are multiple places where you need to specify URLs to pages or paths to media files within your content. That may be a link within your markdown or a parameter for a tool or extension represented by a variable. Most of the times you would just use the buttons in the editor ...

    User Guide  ⁄
    Resizing Images — User Guide

    To optimize the speed of your website it is required to use images of an appropriate size. Images can be resized inline or by using the file manager. When using the image selection dialog, it is therefore possible and recommended to add the appropriate dimensions in pixels.

    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 ...

    Toolbox  ⁄
    filelist — Toolbox

    Updates the configuration of the filelist object. A foreach loop can be used to iterate over the files in the list. This method has no output.

    Toolbox  ⁄
    img — Toolbox

    Creates an image tag for the first image file matching the specified glob pattern. The image can also be cropped by defining a maximum width and height.

    Objects  ⁄
    Filelist — Objects

    The filelist contains by default all image files of the current page. The selection of files can be modified by using the filelist function. Check out this detailed example of how to use a filelist. Similar to the pagelist object, it is possible to get the number of files in the ...

    Template Language  ⁄
    Control Structures — Template Language

    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 ...

    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.

    Template Language  ⁄
    Working with Images — Template Language

    Images are the most important type of media used in the internet and are essential for any kind of blog or portfolio. Automad's template language provides handy methods for basic image processing like resizing and cropping. When using a single image or iterating a set of images, ...

    • Automad
    • Developer Guide
    • Building Themes
    • Template Language
    • Control Structures
    • with
    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