Templates

Syntax

There are two types of markup in Liquid: Output and Tag.

  • Output markup

    (which may resolve to text) is surrounded by {{ matched pairs of curly brackets }}

  • Tag markup

    (which cannot resolve to text) is surrounded by {% matched pairs of curly brackets and percent signs %}

Output

An output statement is a set of double curly braces containing an expression; when the template is rendered, it gets replaced with the value of that expression.

Here is a simple example of output:

{{model.Name}}

Advanced output: Filters

Output markup can take filters, which modify the result of the output statement. You can invoke filters by following the output statement's main expression with:

  • A pipe character ( | )

  • The name of the filter

  • Optionally, a colon ( : ) and a comma-separated list of additional parameters to the filter. Each additional parameter must be a valid expression, and each filter pre-defines the parameters it accepts and the order in which they must be passed.

Filters can also be chained together by adding additional filter statements (starting with another pipe character). The output of the previous filter will be the input for the next one.

{{ model.Name | Downcase }}
{{ model.Name | RemoveLast:'Id' | Downcase }}

Tags

Tags are used for the logic in your template. Here is a list of currently supported tags:

  • assign

    Assigns some value to a variable

  • capture

    Block tag that captures text into a variable

  • case

    Block tag, its the standard case...when block

  • comment

    Block tag, comments out the text in the block

  • for

    For loop

  • if

    Standard if/else block

  • raw

    temporarily disable tag processing to avoid syntax conflicts.

Whitespace control

The tag markup is {%.....%}. You can include a hyphen in your tag syntax {%-.....-%} to strip whitespace from the left or right side of a rendered tag. If you don't want any of your tags to print whitespace, as a general rule you can add hypens to both sides of all your tags {%-.....-%}

{%- for p in model.Properties -%}
    ...
{%- endfor -%}

Comments

Any content that you put between {% comment %} and {% endcomment %} tags is turned into a comment.

In this text we have a comment {% comment %}this comment will not be printed{% endcomment %}

Raw

Raw temporarily disables tag processing. This is useful for generating content (eg, Mustache, Handlebars) which uses conflicting syntax.

{% raw %}
......
{% endraw %}

Capture

If you want to combine a number of strings into a single string and save it to a variable, you can do that with the capture tag. This tag is a block which "captures" whatever is rendered inside it, then assigns the captured value to the given variable instead of rendering it to the screen.

{% capture name_desc %}{{model.Name}}-{{model.Description}}{% endcapture %}
{{name_desc}}

© DynamoCode.