SG Dev Corner

SG Dev Corner

On programming, erlang, science, web design, business and politics.

 
 

2 July 2007 - How to use Yatsy

This article explains how to use Yatsy.

Yatsy (Yet Another Test Server + Yaws) is a test framework for erlang applications that collects results of test cases and presents them either via a web interface or as HTML files on disk.

This is the first part of the article explaining how to setup all the required packages and how to write a simple test case.
In the second part of the article more advanced features will be explained.

Comments (0) - Leave a Comment


26 April 2007 - sgte 0.3.0

I have completed the parser rewrite of sgte.
The new version (0.3.0) is available for download on the google project page:
http://code.google.com/p/sgte/

Changes

join of items

In the previous version a join template was written like this:
$join ", " columns$ 

In the new version the same template is written:

$join:{, } columns$

The new syntax for join reflects the syntax used for the inline map.

map feature

Map has undergone the major changes. There was a bug in the management of multiple inline templates (thanks to tobbe for finding the bug and for his suggestions). I.e. when you had a template like:
$map:{
<li class="odd">$item$</li>,
<li class="even">$item$</li>} itemList$
Where itemList was [{item, "Item1"}, {}item, "Item2", {item, "Item3"}]. You got:
<li class="odd">Item1</li>
<li class="even">Item2</li>
<li class="odd">Item3</li>

The problem was the templates separator "," if found both as a separator and as text to be rendered was parsed incorrectly. Moreover the use of the comma as a separator was really a bad choice since it can easily be found in javascript code and in normal text to render.

I've removed this feature for now.
I don't think anyone uses it at the moment. If someone needs it let me know and I'll put it back in place choosing a better separator.

The non inline map instead now can assume one of the following forms:

$map tmpl items$
$mmap tmpl1 tmpl2 tmpl3 items$
$mapl tmpl items$

The first one is the map of compiled template tmpl over a list of items as before.

If you have multiple templates you want to alternate (e.g. for even and odd rows in a table) you can use mmap.

Finally mapl is a small facility that can be usefull in some cases. I illustrate it with a small example:

{ok, Row} = sgte:compile("- $attr$").
{ok, MapTmpl} = sgte:compile("$map row names$").
sgte:render(MapTmpl, [{row, Row}, {names, ["foo", "bar", "baz"]}]).

Here names is a list of values instead of a list of {key,value} pairs. each name is passed to the row template using attr as key. names is converted during rendering to [{attr, "foo"}, {attr, "bar"}, {attr, "baz"}]. The result is:

- foo
- bar
- baz

Other changes

Other changes include:

  • line numbers added in error messages
  • better management of blanks in templates. Multiple spaces and newlines are properly handled inside template statements
  • the make test now works
  • code has been refactored in a parse module and a render module


Comments (0) - Leave a Comment


20 April 2007 - sgte parser rewrite

After some testing and the first experiences using sgte I'm doing a complete rewrite of the parser.
The code in the first version after some changes was quickly becoming unmantainable, so I decided to rewrite it from scratch.
The interface and the language is mainly the same as the previous version. There are a couple of new features and some smaller incompatible changes I will document before merging the new version in the main trunk.

The rewrite can be found in the TRY-parser-rewrite branch on the project page on Google.

You can checkout the branch with the command:
svn checkout http://sgte.googlecode.com/svn/branches/TRY-parser-rewrite sgte

The rewrite is almost finished. I have to fix some bugs in the if templates, and write documentation for the new features.

The new version should be available in the next week.

Comments (0) - Leave a Comment


11 March 2007 - sgte Google project

I created a Google project for sgte development and a Google group for discussion.

I uploaded the files to the project:
source code and a test module.

The tests use a small library (sgeunit) I wrote when I started learning Erlang. The source of the library is included.

Here are the links:
sgte project home page
sgte google group


Comments (0) - Leave a Comment


5 March 2007 - Update to the map code

A few days ago after some feedback from Eric Merritt I made a small change to the way the map works. It breaks the old code and I hadn't documented it yet since I was the only user. Now sgte is getting some attention on the yaws mailinglist, so it's better if I update the doc :-) .

The old map worked like this:

{ok, C} = sgte:compile("$map:{<li><b>$attr$</b></li>} names$").
sgte:render(C4, [{names, ["foo", "bar", "baz"]}]).
here the element taken from the list is implicitely called attr and it gets the values "foo", "bar", "baz".
I made a change and now I iter over a list of {key, value} tuples so I can do something like this.
{ok, C} = sgte:compile("$map:{<li><b>$first_name$ $last_name$</b></li>} names$").
sgte:render(C4, [{names, [
[{first_name, "filippo"}, {last_name, "pacini"}],
[{first_name, "foo"}, {last_name, "bar"}]
]
}]).

To get:

<li>filippo pacini</li>
<li>foo bar</li>

This seems much better than how I previously used it. I think in the future I could also support the old way it worked adding a mapList feature, or something like that.


Comments (0) - Leave a Comment


10 January 2007 - Building an OTP Application

In this article Martin J Logan guides you in the creation of an Erlang/OTP application.
Comments (0) - Leave a Comment


7 December 2006 - ''No Silver Bullet'' and Functional Programming

''No Silver Bullet'' and Functional Programming
Interesting blog article.

Comments (0) - Leave a Comment


5 December 2006 - SGTE - update

I've started using sgte for my web development.
Using it I've found some problems and thing thath should be improved.

First of all I've added a new feature: application of an attribute to another.
$apply myFun myAttr$
If myFun is a function/1 the result of the template rendering is
myFun(myAttr)

This can be used for example when myvar is a record and you want to extract a field from the record.

Adding the apply keyword I also changed the keyword for template reference. It's not call anymore, but include. This to avoid confusion between call and apply.

I hope also to improve error reporting soon, and to add a small site, or page project with the latest version and with documentation.

The updated package can be found at the same place :-)

Comments (7) - Leave a Comment


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


22 September 2006 - S.G. Domus online

S.G. Domus is a new project developed by S.G. Consulting
It's a web application for estate agencies to help agencies in their daily work. The application will manage real estates, customer requests, a calendar etc.

The application is written mainly in erlang using mnesia as database and yaws as application server.
I'll post details on the development of the application as well as what I learn on erlang.

We also have a blog for the application development. At the moment the site and the application as well as the blog are in italian. An english version is planned when the application will be completly functional.

A demo will be online on November 16th. A first working version on November 31st.

Comments (0) - Leave a Comment


29 August 2006 - Concurrency is easy

After a long pause due to my holidays :-), here's a link to an article on Concurrency by Joe Armstrong.
Comments (0) - Leave a Comment


19 July 2006 - Francesco Facchinetti

A lot of time since the last entry in the blog. In this time I was quite engaged working on the new site of Francesco Facchinetti (dj francesco).
I developed the dynamic pages of the site using python as usual. In the site there's also a forum (phpbb) and soon a chat.

For the chat I'm planning to use the jabber protocol. I have a ejabberd server  and I'm planning to use jwchat (a client written in html and javascript) to connect to the server.

Soon updates on the chat.

Comments (0) - Leave a Comment


21 May 2006 - Erlang 5.5/OTP R11B released

Erlang 5.5/OTP R11B has been released.
The major improvement is support for SMP (Symmetric MultiProcessing). The SMP support is totally trasparent, so Erlang processes are scheduled trasparently across multiple CPUs.
SMP is recently becoming more and more important now when dual processors, hyper-threading and multi-core systems are reality.

The latest release can be downloaded in source or windows binary format.

Comments (0) - Leave a Comment


29 July 2005 - Erlang

Recently I step into erlang. It' s a really interesting language, specially if you are into concurrent, distributed, fault tolerant, applications. Take a look at this tutorial and then you can continue with this pdf: Making reliable distributed systems in the presence of software errors.
Comments (0) - Leave a Comment