TenPlates


Heavy lifting templating engine and the backbone of this website.

View on Github

View raw file


Alright, fine. I can't stack ten plates, but I'll stack templated text files like it's nobody's business.

Tags

Assert

Verifies that a condition is truthy before continuing, will throw at compile-time otherwise.

{% assert "1" /%}

Call

Processes an external file inline, modifying the existing context along the way.

{# ./functions/header.tenplate #}\
{% fn header(lvl, txt) %}\
    <h{{ lvl }}>{{ txt }}</h{{ lvl }}>\
{% /fn %}\
{% call "./functions/header.tenplate" /%}\
{% exec header("2", "Hello") /%}
<h2>Hello</h2>

The following other tag(s) were used in this example.

Comment

Instructs the compiler to skip all content contained within the open/close tags.

{# this is a comment #}

Compile

Processes an external file inline without modifying the existing context.

{# ./set/name.tenplate #}\
{% set name %}Frankie{% /set %}\
{% set name %}Matthew{% /set %}\
{% compile "./set/name.tenplate" /%}{{ name }}
Matthew

The following other tag(s) were used in this example.

Exec

Executes a function already read into the current context. Arguments can be excluded from right-to-left.

{% fn commas(a, b, c) %}\
    {{ a }}, {{ b }}{% if c %}, {{ c }}{% /if %}\
{% /fn %}\

{% set d %}foo{% /set %}\
{% set e %}bar{% /set %}\

{% exec commas(d, e, "baz") /%}
{% exec commas(d, e) /%}
foo, bar, baz
foo, bar

The following other tag(s) were used in this example.

Extend

Sets a single file as an outer template to process with the result of the current file. The context will be passed along and the content will be assigned to the special context variable CONTENT. If the extend tag is used multiple times within the same template, the last tag used wins.

{# ../papa.tenplate #}\

{% assert name /%}\
{% assert paragraph /%}\

<h1>{{ name }}</h1>
<p>{{ paragraph }}</p>\

{% if CONTENT %}
<hr>
<p>{{ CONTENT }}</p>\
{% /if %}
{% extend "../papa.tenplate" /%}\

{% set name %}Frankie{% /set %}\
{% set paragraph %}This is a paragraph.{% /set %}\

And here is some output content.
<h1>Frankie</h1>
<p>This is a paragraph.</p>
<hr>
<p>And here is some output content.</p>

The following other tag(s) were used in this example.

Fn

Registers a function in context which can be called using the exec tag. A function can have anywhere from 0 to n arguments.

{% fn commas(one, two, three) %}\
    {{ one }}, {{ two }}{% if three %}, {{ three }}{% /if %}.\
{% /fn %}\
{% exec commas("First", "Second", "Third") /%}
{% exec commas("First", "Second") /%}
First, Second, Third.
First, Second.

The following other tag(s) were used in this example.

Fordir / Else

Loops through each directory within a given directory. The element variable will contain the path of the directory. If a name is given for the loop variable (as dir_loop in the example below), the loop context will be stored with the given variable as a prefix. The else condition is triggered when no elements are found for the loop.

Assume the following file stucture for the next example.

./
 \
  a-dir/
       \
        First/
        Second/
        Third/
{% fordir d in "./a-dir" as dir_loop %}\
    {% if dir_loop.isfirst %}{% else %}, {% /if %}\
    "{{ d }}"\
{% else %}\
    {# no directories in "./a-dir" #}\
{% /fordir %}
"./a-dir/First", "./a-dir/Second", "./a-dir/Third"

The following other tag(s) were used in this example.

Foreach / Else

Loops through each value in a given variable in context. See set for info on how a variable can have multiple values. The optional loop context definition behaves identically to the fordir tag.

The siblings are \
{% set names %}Matthew{% /set %}\
{% set names %}Frankie{% /set %}\
{% set names %}Karina{% /set %}\
{% foreach name in items as name_loop %}\
    {% if name_loop.isfirst %}{% else %}, {% /if %}\
        {% if name_loop.islast %}and {% /if %}\
        {{ name }}\
    {% /if %}\
    {% if name_loop.islast %}.{% /if %}\
{% else %}\
    {# no items #}
{% /foreach %}
The siblings are Matthew, Frankie, and Karina.

The following other tag(s) were used in this example.

Forfile / Else

Loops through each file in a given directory. The element variable will contain the path. The optional loop context definition behaves identically to the fordir tag.

Assume the following file stucture and contents for the next example.

./
 \
  sibling.tenplate
  siblings/
          \
           first.tenplate
           second.tenplate
           third.tenplate
{# ./sibling.tenplate #}\
{% assert sibling.filepath /%}\
{% call sibling.filepath /%}\
{% assert sibling.name /%}\
{% assert sibling.description /%}\
<tr><td>{{ sibling.name }}</td><td>{{ sibling.description }}.</td></tr>\
{# ./siblings/first.tenplate #}\
{% set sibling.name %}Matthew{% /set %}\
{% set sibling.description %}The elder{% /set %}\
{# ./siblings/second.tenplate #}\
{% set sibling.name %}Frankie{% /set %}\
{% set sibling.description %}The poor middle-child{% /set %}
{# ./siblings/third.tenplate #}\
{% set sibling.name %}Karina{% /set %}\
{% set sibling.description %}Da baby{% /set %}\
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>\
        {% set sibsdir %}{% path "./siblings" /%}{% /set %}\
        {% set sibtemplate %}{% path "./sibling.tenplate" /%}{% /set %}\
        {% forfile sibling.filepath in sibsdir %}
        {% compile sibtemplate /%}\
        {% else %}\
            {# no files in "./a-dir" #}\
        {% /fordir %}
    </tbody>
</table>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr><td>Matthew</td><td>The elder.</td></tr>
        <tr><td>Frankie</td><td>The poor middle-child.</td></tr>
        <tr><td>Karina</td><td>Da baby.</td></tr>
    </tbody>
</table>

The following other tag(s) were used in this example.

Get

Gets a value from a variable in context.

{% set msg %}Hi{% /set %}\
{{ msg }}
Hi

The following other tag(s) were used in this example.

If / Else

Compiles one of two code-paths depending on whether the condition evaluates to true or false. The else tag is an optional inclusion.

{% if "1" %}\
    True\
{% else %}\
    False\
{% /if %}
True

Include

Includes a file inline with compilation. Useful for including files which contain Ten Plates syntax.

{# ./includes/file.tenplate #}\
{% set name %}Frankie{% /set %}\
{% include "./includes/file.tenplate" /%}
{# ./includes/file.tenplate#}\
{% set name %}Frankie{% /set %}\

Set

Sets a value for a variable in context. When multiple values are set for a given variable, the previous value is not overwritten, but is masked by the new value. These values can then be iterated over in the order in which they were set using the for-each tag.

{% set v %}1{% /set %}

Conditions

A set of one or more of logical assertions evaluating to true or false. These can be nested using parenthetical notation or conjoined using the short-circuiting and or or operators. The values contained within conditions are evaluated in their string form so Ten Plates performs boolean casting on all values.

{# true #}{% assert "1" /%}
{# true #}{% assert "Hello, World!" /%}

{# false #}{% assert "0" /%}
{# false #}{% assert "" /%}
{# false #}{% assert a /%}

{# true #}{% set a %}1{% /set %}
{# true #}{% assert "1" == a /%}

{# true #}{% set b %}0{% /set %}
{# true #}{% assert a || b /%}

{# true #}{% assert (a && b) || "1" /%}

{# true #}{% set d %}500{% /set %}
{# true #}{% assert d > a /%}

{# true #}{% assert "501" > d /%}

{# true #}{% assert "501" >= d /%}

{# true #}{% assert "501" != d /%}

{# true #}{% assert "501" <= d /%}
{# true #}{% assert "501" < d /%}

Glossary

Content: The final output of a tenplate.

Context: Functions, values, and other data currently in-scope and usable.

Function: A block of tenplate keyed with a given name for future retrieval and compilation against an optional set of named arguments.

Variable: A value in context keyed with a given name for future retrieval.