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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
Tilt Templates
==============
(See <https://github.com/rtomayko/tilt/blob/master/TEMPLATES.md> for a rendered,
HTML-version of this file).
While all Tilt templates use the same basic interface for template loading and
evaluation, each varies in its capabilities and available options. Detailed
documentation on each supported template engine is provided below.
There are also some file extensions that have several implementations
(currently ERB and Markdown). These template classes have certain features
which are guaranteed to work across all the implementations. If you wish to be
compatible with all of these template classes, you should only depend on the
cross-implementation features.
* [ERB](#erb) - Generic ERB implementation (backed by erb.rb or Erubis)
* [erb.rb](#erbrb) - `Tilt::ERBTemplate`
* [Erubis](#erubis) - `Tilt::ErubisTemplate`
* [Haml](#haml) - `Tilt::HamlTemplate`
* [Liquid](#liquid) - `Tilt::LiquidTemplate`
* Nokogiri - `Tilt::NokogiriTemplate`
* Builder - `Tilt::BuilderTemplate`
* Markaby - `Tilt::MarkabyTemplate`
* [Radius](#radius) - `Tilt::RadiusTemplate`
Tilt also includes support for CSS processors like [LessCSS][lesscss] and
[Sass][sass], [CoffeeScript][coffee-script] and some simple text formats.
* Less - `Tilt::LessTemplate`
* Sass - `Tilt::SassTemplate`
* Scss - `Tilt::ScssTemplate`
* CoffeeScript - `Tilt::CoffeeScriptTemplate`
* [Textile](#redcloth) - `Tilt::RedClothTemplate`
* Creole - `Tilt::CreoleTemplate`
* [RDoc](#rdoc) - `Tilt::RDocTemplate`
Tilt has extensive support for Markdown, backed by one of four different
implementations (depending on which are available on your system):
* [Markdown](#markdown) - Generic Markdown implementation
* [RDiscount](#rdiscount) - `Tilt::RDiscountTemplate`
* Redcarpet - `Tilt::RedcarpetTemplate`
* BlueCloth - `Tilt::BlueClothTemplate`
* Kramdown - `Tilt::KramdownTemplate`
* Maruku - `Tilt::MarukuTemplate`
<a name='erb'></a>
ERB (`erb`, `rhtml`)
--------------------
ERB is a simple but powerful template languge for Ruby. In Tilt it's backed by
[Erubis](#erubis) (if installed on your system) or by [erb.rb](#erbrb) (which
is included in Ruby's standard library). This documentation applies to both
implementations.
### Example
Hello <%= world %>!
### Usage
ERB templates support custom evaluation scopes and locals:
>> require 'erb'
>> template = Tilt.new('hello.html.erb')
>> template.render(self, :world => 'World!')
=> "Hello World!"
Or, use `Tilt['erb']` directly to process strings:
template = Tilt['erb'].new { "Hello <%= world %>!" }
template.render(self, :world => 'World!')
### Options
#### `:trim => trim`
Omits newlines and spaces around certain lines (usually those that starts with
`<%` and ends with `%>`). There isn't a specification for how trimming in ERB
should work, so if you need more control over the whitespace, you should use
[erb.rb](#erbrb) or [Erubis](#erubis) directly.
#### `:outvar => '_erbout'`
The name of the variable used to accumulate template output. This can be
any valid Ruby expression but must be assignable. By default a local
variable named `_erbout` is used.
<a name='erbrb'></a>
erb.rb (`erb`, `rhtml`)
-----------------------
[ERB](#erb) implementation available in Ruby's standard library.
All the documentation of [ERB](#erb) applies in addition to the following:
### Usage
The `Tilt::ERBTemplate` class is registered for all files ending in `.erb` or
`.rhtml` by default, but with a *lower* priority than ErubisTemplate. If you
specifically want to use ERB, it's recommended to use `#prefer`:
Tilt.prefer Tilt::ERBTemplate
__NOTE:__ It's suggested that your program `require 'erb'` at load time when
using this template engine within a threaded environment.
### Options
#### `:trim => true`
The ERB trim mode flags. This is a string consisting of any combination of the
following characters:
* `'>'` omits newlines for lines ending in `>`
* `'<>'` omits newlines for lines starting with `<%` and ending in `%>`
* `'%'` enables processing of lines beginning with `%`
* `true` is an alias of `<>`
#### `:safe => nil`
The `$SAFE` level; when set, ERB code will be run in a
separate thread with `$SAFE` set to the provided level.
#### `:outvar => '_erbout'`
The name of the variable used to accumulate template output. This can be
any valid Ruby expression but must be assignable. By default a local
variable named `_erbout` is used.
### See also
* [ERB documentation](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html)
<a name='erubis'></a>
Erubis (`erb`, `rhtml`, `erubis`)
---------------------------------
[Erubis][erubis] is a fast, secure, and very extensible implementation of [ERB](#erb).
All the documentation of [ERB](#erb) applies in addition to the following:
### Usage
The `Tilt::ErubisTemplate` class is registered for all files ending in `.erb` or
`.rhtml` by default, but with a *higher* priority than `ERBTemplate`. If you
specifically want to use Erubis, it's recommended to use `#prefer`:
Tilt.prefer Tilt::ErubisTemplate
__NOTE:__ It's suggested that your program `require 'erubis'` at load time when
using this template engine within a threaded environment.
### Options
#### `:engine_class => Erubis::Eruby`
Allows you to specify a custom engine class to use instead of the
default which is `Erubis::Eruby`.
#### `:escape_html => false`
When `true`, `Erubis::EscapedEruby` will be used as the engine class
instead of the default. All content within `<%= %>` blocks will be
automatically html escaped.
#### `:outvar => '_erbout'`
The name of the variable used to accumulate template output. This can be
any valid Ruby expression but must be assignable. By default a local
variable named `_erbout` is used.
#### `:pattern => '<% %>'`
Set pattern for embedded Ruby code.
#### `:trim => true`
Delete spaces around `<% %>`. (But, spaces around `<%= %>` are preserved.)
### See also
* [Erubis Home][erubis]
* [Erubis User's Guide](http://www.kuwata-lab.com/erubis/users-guide.html)
<a name='haml'></a>
Haml (`haml`)
-------------
[Haml][haml] is a markup language that’s used to cleanly and simply describe
the HTML of any web document without the use of inline code. Haml functions as
a replacement for inline page templating systems such as PHP, ASP, and ERB, the
templating language used in most Ruby on Rails applications. However, Haml
avoids the need for explicitly coding HTML into the template, because it itself
is a description of the HTML, with some code to generate dynamic content.
([more](http://haml-lang.com/about.html))
### Example
%html
%head
%title= @title
%body
%h1
Hello
= world + '!'
### Usage
The `Tilt::HamlTemplate` class is registered for all files ending in `.haml`
by default. Haml templates support custom evaluation scopes and locals:
>> require 'haml'
>> template = Tilt.new('hello.haml')
=> #<Tilt::HamlTemplate @file='hello.haml'>
>> @title = "Hello Haml!"
>> template.render(self, :world => 'Haml!')
=> "
<html>
<head>
<title>Hello Haml!</title>
</head>
<body>
<h1>Hello Haml!</h1>
</body>
</html>"
Or, use the `Tilt::HamlTemplate` class directly to process strings:
>> require 'haml'
>> template = Tilt::HamlTemplate.new { "%h1= 'Hello Haml!'" }
=> #<Tilt::HamlTemplate @file=nil ...>
>> template.render
=> "<h1>Hello Haml!</h1>"
__NOTE:__ It's suggested that your program `require 'haml'` at load time when
using this template engine within a threaded environment.
### Options
Please see the [Haml Reference](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options) for all available options.
### See also
* [#haml.docs](http://haml-lang.com/docs.html)
* [Haml Tutorial](http://haml-lang.com/tutorial.html)
* [Haml Reference](http://haml-lang.com/docs/yardoc/HAML_REFERENCE.md.html)
<a name='liquid'></a>
Liquid (`liquid`)
-----------------
[Liquid][liquid] is for rendering safe templates which cannot affect the
security of the server they are rendered on.
### Example
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Hello {{ world }}!</h1>
</body>
</html>
### Usage
`Tilt::LiquidTemplate` is registered for all files ending in `.liquid` by
default. Liquid templates support locals and objects that respond to
`#to_h` as scopes:
>> require 'liquid'
>> require 'tilt'
>> template = Tilt.new('hello.liquid')
=> #<Tilt::LiquidTemplate @file='hello.liquid'>
>> scope = { :title => "Hello Liquid Templates" }
>> template.render(nil, :world => "Liquid")
=> "
<html>
<head>
<title>Hello Liquid Templates</title>
</head>
<body>
<h1>Hello Liquid!</h1>
</body>
</html>"
Or, use `Tilt::LiquidTemplate` directly to process strings:
>> require 'haml'
>> template = Tilt::LiquidTemplate.new { "<h1>Hello Liquid!</h1>" }
=> #<Tilt::LiquidTemplate @file=nil ...>
>> template.render
=> "<h1>Hello Liquid!</h1>"
__NOTE:__ It's suggested that your program `require 'liquid'` at load
time when using this template engine within a threaded environment.
### See also
* [Liquid for Programmers](http://wiki.github.com/tobi/liquid/liquid-for-programmers)
* [Liquid Docs](http://liquid.rubyforge.org/)
* GitHub: [tobi/liquid](http://github.com/tobi/liquid/)
<a name='radius'></a>
Radius (`radius`)
-----------------
[Radius][radius] is the template language used by [Radiant CMS][radiant]. It is
a tag language designed to be valid XML/HTML.
### Example
<html>
<body>
<h1><r:title /></h1>
<ul class="<r:type />">
<r:repeat times="3">
<li><r:hello />!</li>
</r:repeat>
</ul>
<r:yield />
</body>
</html>
### Usage
To render a template such as the one above.
scope = OpenStruct.new
scope.title = "Radius Example"
scope.hello = "Hello, World!"
require 'radius'
template = Tilt::RadiusTemplate.new('example.radius', :tag_prefix=>'r')
template.render(scope, :type=>'hlist'){ "Jackpot!" }
The result will be:
<html>
<body>
<h1>Radius Example</h1>
<ul class="hlist">
<li>Hello, World!</li>
<li>Hello, World!</li>
<li>Hello, World!</li>
</ul>
Jackpot!
</body>
</html>
### See also
* [Radius][radius]
* [Radiant CMS][radiant]
<a name='textile'></a>
Textile (`textile`)
-------------------
Textile is a lightweight markup language originally developed by Dean Allen and
billed as a "humane Web text generator". Textile converts its marked-up text
input to valid, well-formed XHTML and also inserts character entity references
for apostrophes, opening and closing single and double quotation marks,
ellipses and em dashes.
Textile formatted texts are converted to HTML with the [RedCloth][redcloth]
engine, which is a Ruby extension written in C.
### Example
h1. Hello Textile Templates
Hello World. This is a paragraph.
### Usage
__NOTE:__ It's suggested that your program `require 'redcloth'` at load time
when using this template engine in a threaded environment.
### See Also
* [RedCloth][redcloth]
<a name='rdoc'></a>
RDoc (`rdoc`)
-------------
[RDoc][rdoc] is the simple text markup system that comes with Ruby's standard
library.
### Example
= Hello RDoc Templates
Hello World. This is a paragraph.
### Usage
__NOTE:__ It's suggested that your program `require 'rdoc/markup'` and
`require 'rdoc/markup/to_html'` at load time when using this template
engine in a threaded environment.
### See also
* [RDoc][rdoc]
<a name='markdown'></a>
Markdown (`markdown`, `md`, `mkd`)
----------------------------------
[Markdown][markdown] is a lightweight markup language, created by John Gruber
and Aaron Swartz. For any markup that is not covered by Markdown’s syntax, HTML
is used. Marking up plain text with Markdown markup is easy and Markdown
formatted texts are readable.
Markdown formatted texts are converted to HTML with one of these libraries:
* [RDiscount](#rdiscount) - `Tilt::RDiscountTemplate`
* Redcarpet - `Tilt::RedcarpetTemplate`
* BlueCloth - `Tilt::BlueClothTemplate`
* Kramdown - `Tilt::KramdownTemplate`
* Maruku - `Tilt::MarukuTemplate`
Tilt will use fallback mode (as documented in the README) for determining which
library to use. RDiscount has highest priority - Maruku has lowest.
### Example
Hello Markdown Templates
========================
Hello World. This is a paragraph.
### Usage
To wrap a Markdown formatted document with a layout:
layout = Tilt['erb'].new do
"<!doctype html><title></title><%= yield %>"
end
data = Tilt['md'].new { "# hello tilt" }
layout.render { data.render }
# => "<!doctype html><title></title><h1>hello tilt</h1>\n"
### Options
Every implementation of Markdown *should* support these options, but there are
some known problems with the Kramdown and Maruku engines.
#### `:smartypants => true|false`
Set `true` to enable [Smarty Pants][smartypants]
style punctuation replacement.
#### `:escape_html => true|false`
Set `true` disallow raw HTML in Markdown contents. HTML is converted to
literal text by escaping `<` characters.
### See also
* [Markdown Syntax Documentation](http://daringfireball.net/projects/markdown/syntax/)
<a name='rdiscount'></a>
RDiscount (`markdown`, `md`, `mkd`)
-----------------------------------
[Discount][discount] is an implementation of the Markdown markup language in C.
[RDiscount][rdiscount] is a Ruby wrapper around Discount.
All the documentation of [Markdown](#markdown) applies in addition to the following:
### Usage
The `Tilt::RDiscountTemplate` class is registered for all files ending in
`.markdown`, `.md` or `.mkd` by default with the highest priority. If you
specifically want to use RDiscount, it's recommended to use `#prefer`:
Tilt.prefer Tilt::RDiscountTemplate
__NOTE:__ It's suggested that your program `require 'erubis'` at load time when
using this template engine within a threaded environment.
### See also
* [Discount][discount]
* [RDiscount][rdiscount]
* GitHub: [rtomayko/rdiscount][rdiscount]
[lesscss]: http://lesscss.org/ "Less CSS"
[sass]: http://sass-lang.com/ "Sass"
[coffee-script]: http://jashkenas.github.com/coffee-script/ "Coffee Script"
[erubis]: http://www.kuwata-lab.com/erubis/ "Erubis"
[haml]: http://haml-lang.org/ "Haml"
[liquid]: http://www.liquidmarkup.org/ "Liquid"
[radius]: http://radius.rubyforge.org/ "Radius"
[radiant]: http://radiantcms.org/ "Radiant CMS"
[redcloth]: http://redcloth.org/ "RedCloth"
[rdoc]: http://rdoc.rubyforge.org/ "RDoc"
[discount]: http://www.pell.portland.or.us/~orc/Code/discount/ "Discount"
[rdiscount]: http://github.com/rtomayko/rdiscount/ "RDiscount"
[smartypants]: http://daringfireball.net/projects/smartypants/ "Smarty Pants"
|