Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
136 lines (120 sloc) 4.17 KB
@title sgte - A simple Erlang Template Engine
<p><em>sgte</em> is a library implementing a Template Engine.
The template system is inspired on <a href="">String Template</a>
The template source can be a string or binary or a filename.
The use of the engine is as simple as (from the command line):
> {ok, Compiled} = sgte:compile(TmplStr),
> sgte:render(Compiled, Data).
> {ok, Compiled} = sgte:compile_file(FileName),
> sgte:render(Compiled, Data).
When compile/1 or compile_file/1 are called, the template is "compiled"
to an internal representation made of mixed binaries and erlang terms.
Then one of the render functions (render, render_str, or render_bin)
can be called passing the Data to be rendered in the template.
<li>render/2 and render/3 returns a deep iolist() suitable e.g. to use in yaws</li>
<li>render_str/2 and render_str/3 returns a string()</li>
<li>render_bin/2 and render_bin/3 returns a list of binaries()</li>
In the rest of the overview the template features will be described using render_str since
the produced is easier to read.
Data can be a Dict or a proplist (e.g. [{attr1, Val1}, {attr2, Val2}])
Values can be a simple value or a function/1. In this case the function is
called with Data as an argument.
<h3>Template Features</h3>
<h4>Attribute reference</h4>
Attribute reference is written as:
</pre> Here is an example:
> {ok, C} = sgte:compile("Hello $name$!").
> sgte:render_str(C, [{name, "Filippo").
"Hello Filippo!"
You can also use attribute references to access nested structures. Eg.:
> {ok, C} = sgte:compile("$$").
> sgte:render_str(C, [{foo, [{bar, [{baz, "a string"}]}]}]).
"a string"
<h4>Template reference</h4>
$include tmpl$
</pre> Includes an external compiled template. Eg.:
> {ok, C1} = sgte:compile("foo").
> {ok, C2} = sgte:compile("$include t1$ bar").
> sgte:render_str(C2, [{t1, C1}]).
"foo bar"
<h4>Application of an attribute to another</h4>
$apply myFun aVar$
</pre> When the first attribute is callable you get the
result of myFun(aVar). Otherwhise the result is the value of
<h4>Conditional evaluation</h4>
$if title$
&lt;h1&gt;default title&lt;/h1&gt;
$end if$
<h4>Template application to a list of elements</h4>
if names is a list [{username, "foo"}, {username, "bar"}]
$map li names$
map li template to names. Each element in names is passed to the template with name attr.
If li is the template:
We get the result:
In the shell we write something like:
> {ok, LI} = sgte:compile("&lt;li&gt;&lt;b&gt;$username$&lt;/b&gt;&lt;/li&gt;").
> {ok, NamesC} = sgte:compile("$map li names$").
> sgte:render_str(NamesC, [{li, LI}, {names, [{username, "foo"}, {username, "bar"}]}]).
Another way to express the same template inline all in one step is:
$map:{&lt;li&gt;&lt;b&gt;$username$&lt;/b&gt;&lt;/li&gt;} names$
<h4>Join of items using a separator</h4>
> {ok, SEL} = sgte:compile("SELECT $join:{,} columns$ FROM $table$;").
> sgte:render_str(SEL, [{table, "myTable"}, {columns, ["col1", "col2", "col3"]}]).
"SELECT col1,col2,col3 FROM myTable;"
<h4>Internationalization support using gettext</h4>
Gettext is a package that can be found in <a href="">jungerl</a>. It
supports internationalization using the <a href="">GNU Gettext format</a>.
$txt:{Hello World}$
Here's an example supposing you already have started gettext and you have the .po files containing
translated strings:
> {ok, C} = sgte:compile("$txt:{Hello World}$"),
> sgte:render(C, [{options, [{gettext_lc, "en"}]}]).
"Hello World"
> sgte:render(C, [{options, [{gettext_lc, "it"}]}]).
"Ciao Mondo"
@author Filippo Pacini <>
@copyright 2006 - 2008 S.G. Consulting
Jump to Line
Something went wrong with that request. Please try again.