A template engine for Pony
Templates is an alpha-level package.
As templates gets used in more projects, we expect to find and fix bugs. We also expect that we will make breaking changes.
- Install corral
corral add github.com/ponylang/templates.git --version 0.2.4
corral fetch
to fetch your dependenciesuse "templates"
to include this packagecorral run -- ponyc
to compile your application
https://ponylang.github.io/templates
-
Add a line
use "templates"
to your source file -
Load a template with
Template.parse("Hello {{ name }}")?
. Note that this method is partial. In other words, it can fail. -
Create a
TemplateValues
object and fill it with the values you want to see in the rendered template:let values = TemplateValues values("name") = "world"
-
Finally, render the template with
template.render(values)?
(assumingtemplate
references your template). Note that this method is partial as well.
-
Variables:
{{ some_var }}
will be replaced with the variablesome_var
's value. A value can either be aString
or aTemplateValue
. ATemplateValue
is either aString
or aSeq[TemplateValue]]
. -
Properties:
{{ some_var.prop }}
will be replaced with valuesome_var
's propertyprop
. Properties are part ofTemplateValue
: its constructor takes aMap[String, TemplateValue]
that defines the value's properties. -
For loops:
{{ for x in xs }}{{ x }} {{ end }}
will iterate through the sequencexs
and adds each element plus a space to the output. -
Conditional output:
{{ if spam }}Eggs{{ end }}
only addsEggs
to the output if the variablespam
exists. Can also check for the presence of a property.{{ ifnotempty seq }}
ignores everything until the next{{ end }}
if sequenceseq
is empty. -
Calls:
{{ escape(var) }}
callsescape
with argumentvar
and adds the function result to the output. All known functions must be passed as part of aTemplateContext
value to the template's constructor.