24 November 2006 - SGTE - bug fixed
I've fixed a bug with embedded if tokens in templates and uploaded the fixed version on our website.
You can download it
here.
Comments (0) -
Leave a Comment
15 November 2006 - SGTE update
I've uploaded an updated version of sgte.
Below are the changes from the previous version and the template reference feature I forgot to mention in my previous post.
Changes
- sgte is no longer a gen_server behaviour. It's a simple library. OTP behaviour, as well as caching of the compiled templates will be part of the web framework I'm building on sgte. So you don't need the initial call to start_link to use the library.
- error reporting: if compilation fails now you get an tuple explaining where the error occured.
- I rewrote the code documentation using edoc. You can
download the generated html.
Template reference
In the previous post I also forgot to mention the template reference feature.
In a template we can refer to other templates.
First we define the template to be included:
{ok, C1} = sgte:compile("bar").
Then we define the template including it, and we call the render passing the previous compiled template in the data:
{ok, C2} = sgte:compile("foo $call tmpl$ baz").
sgte:render(C2, [{tmpl, C1}]).
And so we get:
"foo bar baz"
Note that in the template after call we can use any atom (e.g. $call page_header$). Then when we call the render we pass the compiled template to include (e.g. {page_header, CompiledHeader}).
[edited] the token for template reference is changed. Use include instead of call [edited]
Comments (0) -
Leave a Comment
7 November 2006 - SGTE - a simple Erlang Template Engine
SGTE is an Erlang template Engine for generating structured output (code, html web pages, xml, emails, csv files, etc...).
It's based on the StringTemplate template language. And follows the principle of strict model-view separation. The template language tries to remain as simple as possible, moreover the approach used should work very well for designers working with WYSIWYG editors.
Let's see quickly how to use it from the command line and all it's features.
How to use it
SGTE usage is very simple. To compile a template and render it we call:
{ok, Compiled} = sgte:compile(Template).
sgte:render(compiled, Data).
Where template can be either a string or a tuple {file, FileName}, and Data can be a Dict or a list of tuple (e.g. [{attribute1, Val1}, {attribute2, Val2}]).
Values can be simple values or a function/1. In this case the function is
called with Data as an argument.
The render function returns a list containing the rendered template.
Features
1- attribute reference:
{ok, C1} = sgte:compile("foo $attr_ref$ baz").
sgte:render(C1, [{attr_ref, "bar"}]).
and you get: "foo bar baz"
2- conditional evaluation:
T2 = "$if test$
test passed
$else$
test failed
$end if$".
{ok, C2} = sgte:compile(T2).
sgte:render(C2, [{test, true}]).
to get: "test passed" (newlines are not stripped)
and
sgte:render(C2, [{test, false}]).
to get: "test failed"
3- template application to a list of elements: first we create a template to call on each element in a list:
{ok, LI} = sgte:compile("<li><b>$attr$</b></li>").
then we build the map template and call the render:
{ok, C3} = sgte:compile("$map li names$").
sgte:render(C3, [{li, LI}, {names, ["foo", "bar", "baz"]}]).
and the result is: "<li><b>foo</b></li><li><b>bar</b></li><li><b>baz</b></li>"
An alternative syntax to get the same result inlining the li in the template is this:
{ok, C4} = sgte:compile("$map:{<li><b>$attr$</b></li>} names$").
sgte:render(C4, [{names, ["foo", "bar", "baz"]}]).
You can also alternate several templates on an a list of elements building the template in one of the following ways:
MultiRows = "$map row1 row2 row3 names$".
MultiRowsInline = "$map:{<li class="row1">$attr$</li>,
<li class="row2">$attr$</li>,
<li class="row3">$attr$</li>} names$".
4- join of items using a separator:
{ok, C5} = sgte:compile("SELECT $join "," columns$ FROM $table$").
sgte:render(C5, [{columns, ["col1", "col2", "col3"]}, {table, "mytable"}]).
gives the result: "SELECT col1,col2,col3 FROM mytable".
A usefull tip for designers using WYSIWYG editors like Dreamweaver is to enclose template instructions in html comments. In this way designers and programmers can easily share templates.
For example a template like the one below can be opened without problems in Dreamweaver. template directives are in the html code, but designers don't see them while they see all they need to build a well designed page.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>$title$</title>
</head>
<body>
<!-- $if printTitle$ -->
<h1>$pageTitle$</h1>
<!-- $end if$ -->
<ul>
<!-- $map:{ -->
<li class="row1">$attr$</li>,
<li class="row2">$attr$</li>
<!-- } names$ -->
</ul>
<hr>
<!-- hhmts start -->Last modified: Tue Nov 7 17:51:30 CET 2006 <!-- hhmts end -->
</body> </html>
All these features can be combined togheter to build complex templates.
Bugs and things TODO
The template engine is still in an early development stage. I'm planning to start using it soon, so in the next weeks most of the bugs should be fixed, error reporting should be improved, etc...
One of the most annoying bug you'll see is that if the template sysntax is not correct (e.g. you forget a closing $) on compilation the engine simply crashes without giving usefull information.
I'm also planning to use a cache to store compiled templates. After reading the paper Concurrent Caching I've exchanged some email with the author and I'd like to thank him for the help in the design of the cache system.
If you to try sgte, you can download the latest version here. Any comment and suggestion is welcome.
Comments (42) -
Leave a Comment