The parser transforms a string input into a template AST.

Template language description

The template language uses <% and %> as the tag delimiters. For outputting, the start delimiter <%= is used.

Comments can be inserted with <%!-- Comment --%>, they are not emitted to the AST.

Assigns can be referenced using their name (key in the assigns map). After an assign name, the contents of the assign can be accessed using field or index based access:

The accesses can be chained, e.g. These access methods work in any place where an assign reference is expected.

Any other content in the template is emitted as text nodes.



<%= foo %>

Emits an output node of the assign foo. The contents are encoded using the encoder given in the render call.

Raw output

<%= raw foo %>

Emits an output node of the assign foo. The contents are not encoded.


<% if foo %>
<% else %>
<% end %>

Emits an if node. If the assign foo is truthy (not False), the first nodes are rendered, otherwise the nodes in the else block. The else block is optional.


<% for bar in foo %>
<% end %>

Emits a for node. Each item of foo is looped through, making it available as the assign bar inside the block. The contents of the block are rendered for each item.


<% render tpl.txt.glemp %>
<% render tpl.txt.glemp name:, level: user.level %>

Emits a render node. This will render another template in this place. The listed assigns will be the assigns given to the template. The name of the assign in the child template is on the left, the value is on the right.


pub fn parse(template: String) -> Result(

Parse the string into a node list.

pub fn parse_to_template(template: String, name: String) -> Result(

Parse the string into a template with the given name.

Search Document