1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
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 {file:README.md 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:
[:multi,
[:static, "Hello "],
[:dynamic, "@world"]]
### [:static, string]
Static indicates that the given string should be appended to the result.
Example:
[: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.
Example:
[: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.
Example:
[:code, 'area = Math::PI * r**2']
### [:newline]
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.
Example:
[:multi,
[: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]
Example:
[:if,
"1+1 == 2",
[:static, "Yes"],
[:static, "No"]]
is the same as:
if 1+1 == 2
_buf << "Yes"
else
_buf << "No"
end
### [:block, ruby, sexp]
Example:
[:block,
'10.times do',
[:static, 'Hello']]
is the same as:
10.times do
_buf << 'Hello'
end
### [:case, argument, [condition, sexp], [condition, sexp], ...]
Example:
[:case,
'value',
["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], ...]
[:cond,
["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.
Example:
[:escape, true,
[:multi,
[: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 html>
Supported doctypes:
<table>
<tr><td><b>Name</b></td><td><b>Generated doctype</b></td></tr>
<tr><td>1.1</td><td><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"></td></tr>
<tr><td>html, 5</td><td><!DOCTYPE html></td></tr>
<tr><td>strict</td><td><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></td></tr>
<tr><td>frameset</td><td><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"></td></tr>
<tr><td>mobile</td><td><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"></td></tr>
<tr><td>basic</td><td><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"></td></tr>
<tr><td>transitional</td><td><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></td></tr>
</table>
### [:html, :comment, sexp]
Example:
[:html, :comment, [:static, 'comment']]
generates:
<!--comment-->
### [:html, :condcomment, condition, sexp]
Example:
[:html, :condcomment, 'IE', [:static, 'comment']]
generates:
<!--[if IE]>comment<![endif]-->
### [: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. `<br/>` `<img/>`). 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:
<img src="image.png"/>
<p>Content</p>
### [: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.
### [:html, :js, code]
HTML javascript abstraction which wraps the js code in a HTML comment or CDATA depending on document format.
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
<!-- Find a better way to include the grammar -->
<script src="http://gist-it.appspot.com/github/judofyr/temple/raw/master/lib/temple/grammar.rb"></script>
|