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
based access: foo.wibble
accesses field wibble
in assign foo
.
The accesses can be chained, e.g. foo.wibble.wobble
. 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
, 0, or empty),
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.
Types
pub type ParseError {
LexerError(String)
ParserError(List(String))
}
Constructors
-
LexerError(String)
-
ParserError(List(String))
Functions
pub fn new() -> Parser
Create a new parser.
This same parser should be used for multiple operations to avoid creating the parser over and over again.
pub fn parse(
input: String,
parser: Parser,
) -> Result(List(Node), ParseError)
Parse the tokens into a node list.
pub fn parse_to_template(
input: String,
name: String,
parser: Parser,
) -> Result(Template, ParseError)
Parse the input into a template with the given name.