Template engines

Templates are compiled to C# code at runtime, which gives you quite fast templates. They are automatically recompiled if the templates are changed on the harddrive. Template engines can easily be
created and added. The project has currently two template engines, one that parses <% and <%= style tags, and one that parses HAML (http://haml.hamptoncatlin.com/).

Processing templates

Templates are processed in three steps. First two steps are only done once, and the result are then cached in memory.
Note that the first to steps are done every time a template file is changed on disk.

Step 1 - Processing the files

The first step is to process the template files and convert them to c# code. This is done by each parser and this is the only thing you have to do when adding your own template engine.

Step 2 - Compile the templates

This is done by the framework. The code that was generated in step 1 is inserted into a predefined class structure. The compiler takes care of finding all needed assemblies and name spaces. Then the new class is compiled and a new .Net object is created in memory.

Step 3 - Run the template.

This step should be quite fast since your template is now an actual .Net object. Running the template is simply done by invoking a method in the generated template class.

Built in template engines

Tiny template

Tiny templates are quite similiar to asp.net templates, they use the <% %> and <%= %> tags

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
  <title>hello world</title>
  <body>
    <%= bodyCode %>
  </body>
</html>


HAML Templates

HAML is a markup language originally created for Ruby on Rails. It uses intendation as a part of the markup, instead of closing tags.
The engine works, but need a little love to generate more beautiful html.


!!!
%html
  %head
    %title Hello world
  %body
    = bodyCode


As you see, you need to type a lot less.

Currently implemented haml features

% Html tags

All html tags starts with "%", tags that should be closed should be prepended with /
  • %html => <html></html>
  • %br/ => <br />

- Silent code

C# code that should be invoked but do not make any output

%html
  %body
    - for (int i = 0; i < 10; ++i)
      Hello World!

= display code

c# code that should be shown in templates

%html
  %body
    = "Hello " + user.LastName

# tag id

Assign an id to a tag

%html
  %body
    %div#main


Would result in

<html>
  <body>
    <div id="main"></div>
  </body>
</html>


Since div is such a regularly used tag, you don't have to use "%div" for it, you can simply use "#myid" directly

%html
  %body
    #main

. css classes

"." are used to indicate which css class to use. Same as with id, you dont need to specify %div for divs.

%html
  %body
    .myclass This is some text.

{} attributes.

Attribute values are interpreted as c#, while attribute names are always regular text.

%html
  %body
    #myclass{ class = user.htmlClass, style="display:none"} Hello, this text is not visible.


This document is far from done.

Last edited May 11, 2008 at 4:30 PM by jgauffin, version 5

Comments

No comments yet.