glemplate/parser

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. foo.bar.0.baz. These access methods work in any place where an assign reference is expected.

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

Tags

Output

<%= 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

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

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

<% render tpl.txt.glemp %>
<% render tpl.txt.glemp name: user.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.

Functions

pub fn parse(template: String) -> Result(
  List(Node),
  List(DeadEnd(a)),
)

Parse the string into a node list.

pub fn parse_to_template(template: String, name: String) -> Result(
  Template,
  List(DeadEnd(a)),
)

Parse the string into a template with the given name.

Search Document