Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

251 lines (181 sloc) 7.145 kb

Temple expression documentation

Temple uses S-expressions to represent the parsed template code. The S-expressions are passed from filter to filter until the generator. The generator transforms the S-expression to a ruby code string. See the { README} for an introduction.

In this document we documented all the expressions which are used by Temple. There is also a formal grammar which can validate expressions.

The Core Abstraction

The core abstraction is what every template evetually should be compiled to. Currently it consists of six types: multi, static, dynamic, code, newline and capture.

When compiling, there's two different strings we'll have to think about. First we have the generated code. This is what your engine (from Temple's point of view) spits out. If you construct this carefully enough, you can make exceptions report correct line numbers, which is very convenient.

Then there's the result. This is what your engine (from the user's point of view) spits out. It's what happens if you evaluate the generated code.

[:multi, *sexp]

Multi is what glues everything together. It's simply a sexp which combines several others sexps:

  [:static, "Hello "],
  [:dynamic, "@world"]]

[:static, string]

Static indicates that the given string should be appended to the result.


[:static, "Hello World"]

is the same as: _buf << "Hello World"

[:static, "Hello \n World"]

is the same as _buf << "Hello\nWorld"

[:dynamic, ruby]

Dynamic indicates that the given Ruby code should be evaluated and then appended to the result.

The Ruby code must be a complete expression in the sense that you can pass it to eval() and it would not raise SyntaxError.


 [:dynamic, 'Math::PI * r**2']

[:code, ruby]

Code indicates that the given Ruby code should be evaluated, and may change the control flow. Any \n causes a newline in the generated code.


 [:code, 'area = Math::PI * r**2']


Newline causes a newline in the generated code, but not in the result.

[:capture, variable_name, sexp]

Evaluates the Sexp using the rules above, but instead of appending to the result, it sets the content to the variable given.


  [:static, "Some content"],
  [:capture, "foo", [:static, "More content"]],
  [:dynamic, "foo.downcase"]]

is the same as: _buf << "Some content" foo = "More content" _buf << foo.downcase

Control flow abstraction

Control flow abstractions can be used to write common ruby control flow constructs. These expressions are compiled to [:code, ruby] by Temple::Filters::ControlFlow

[:if, condition, if-sexp, optional-else-sexp]


 "1+1 == 2",
 [:static, "Yes"],
 [:static, "No"]]

is the same as: if 1+1 == 2 _buf << "Yes" else _buf << "No" end

[:block, ruby, sexp]


 '10.times do',
 [:static, 'Hello']]

is the same as: 10.times do _buf << 'Hello' end

[:case, argument, [condition, sexp], [condition, sexp], ...]


 ["1",   "value is 1"],
 ["2",   "value is 2"],
 [:else, "don't know"]]

is the same as: case value when 1 _buf << "value is 1" when 2 _buf << "value is 2" else _buf << "don't know" end

[:cond, [condition, sexp], [condition, sexp], ...]

 ["a",   "a is true"],
 ["b",   "b is true"],
 [:else, "a and b are false"]]

is the same as: case when a _buf << "a is true" when b _buf << "b is true" else _buf << "a and b are false" end

Escape abstraction

The Escape abstraction is processed by Temple::Filters::Escapable.

[:escape, bool, sexp]

The boolean flag switches escaping on or off for the content sexp. Dynamic and static expressions are manipulated.


[:escape, true,
  [:dynamic, "code"],
  [:static, "<"],
  [:escape, false, [:static, ">"]]]]

is transformed to [:multi, [:dynamic, 'escape_html(code)'], [:static, '<'], [:static, '>']]

HTML abstraction

The HTML abstraction is processed by the html filters (Temple::HTML::Fast and Temple::HTML::Pretty).

[:html, :doctype, string]

Example: [:html, :doctype, '5'] generates <!DOCTYPE html5>

Supported doctypes:

Name Generated doctype
1.1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
html, 5 <!DOCTYPE html>
strict <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
frameset <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "">
mobile <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "">
basic <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "">
transitional <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

[:html, :comment, sexp]

Example: [:html, :comment, [:static, 'comment']] generates: <!--comment-->

[:html, :tag, identifier, attributes, optional-sexp]

HTML tag abstraction. Identifier can be a String or a Symbol. If the optional content Sexp is omitted the tag is closed (e.g.
). The tag is also closed if the content Sexp is empty (consists only of :multi and :newline expressions) and the tag is registered as auto-closing.

Example: [:html, :tag, 'img', [:html, :attrs, [:html, :attr, 'src', 'image.png']]] [:html, :tag, 'p', [:multi], [:static, 'Content']] generates:


[:html, :attrs, attributes]

List of html attributes [:html, :attr, identifier, sexp]

[:html, :attr, identifier, sexp]

HTML attribute abstraction. Identifier can be a String or a Symbol.

Formal grammar

Validate expressions with Temple::Grammar.match? and Temple::Grammar.validate!

Temple::Grammar.match? [:multi, [:static, 'Valid Temple Expression']]
Temple::Grammar.validate! [:multi, 'Invalid Temple Expression']

The formal grammar is given in a Ruby DSL similar to EBNF and should be easy to understand if you know EBNF. Repeated tokens are given by appending ?, * or + as in regular expressions.

  • ? means zero or one occurence
  • * means zero or more occurences
  • + means one or more occurences
Jump to Line
Something went wrong with that request. Please try again.