Migrated from liquid-for-programmers v7

RichMorin
2010-08-12 13:45:54 -07:00
parent 0fe3cc41a1
commit beb8f54fd2
+23 -12
@@ -1,24 +1,32 @@
h2. First steps
It's very simple to get started with Liquid. A liquid template is rendered in two steps: Parse and Render.
For an overview of the liquid syntax please read [[Liquid for Designers]].
It's very simple to get started with Liquid.
A Liquid template is rendered in two steps: Parse and Render.
For an overview of the Liquid syntax, please read [[Liquid for Designers]].
<pre><code>
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
@template.render( 'name' => 'tobi' ) # Renders the output => "hi tobi"
</code></pre>
The parse step creates a fully compiled template which can be re-used as often as you like. You can store it in memory or in a cache for faster rendering later.
The <code>parse</code> step creates a fully compiled template which can be re-used as often as you like.
You can store it in memory or in a cache for faster rendering later.
All parameters you want Liquid to work with have to be passed as parameters to the render method. Liquid does not know about your ruby local, instance, and global variables.
All parameters you want Liquid to work with have to be passed as parameters to the <code>render</code> method.
Liquid does not know about your Ruby local, instance, and global variables.
h2. Extending liquid
h2. Extending Liquid
Extending liquid is very easy. However keep in mind that liquid is a young library and requires some outside help. If you create useful filters and tags please consider creating a patch and attaching it to a ticket here on this trac.
Extending Liquid is very easy.
However, keep in mind that Liquid is a young library and requires some outside help.
If you create useful filters and tags, please consider creating a patch and attaching it to a ticket here on this trac.
h3. Create your own filters
Creating filters is very easy. Basically they are just methods which take one parameter and return a modified string. You can use your own filters by passing an array of modules to the render call like this @template.render(assigns, [MyTextFilters, MyDateFilters])
Creating filters is very easy.
Basically, they are just methods which take one parameter and return a modified string.
You can use your own filters by passing an array of modules to the render call like this:
<code>@template.render(assigns, [MyTextFilters, MyDateFilters])</code>.
<pre><code>
module TextFilter
@@ -33,7 +41,7 @@ end
@template.render({}, :filters => [TextFilter]) # => "<b>hi</b>"
</code></pre>
Alternatively you can also register your filters globally
Alternatively, you can register your filters globally:
<pre><code>
module TextFilter
@@ -45,7 +53,7 @@ end
Liquid::Template.register_filter(TextFilter)
</code></pre>
Once the filter is globally registered you can simply use it:
Once the filter is globally registered, you can simply use it:
<pre><code>
@template = Liquid::Template.parse(" {{ '*hi*' | textilize }} ")
@@ -54,7 +62,7 @@ Once the filter is globally registered you can simply use it:
h3. Create your own tags
To create a new tag, simply inherit from Liquid::Tag and register your block with Liquid::Template
To create a new tag, simply inherit from <code>Liquid::Tag</code> and register your block with <code>Liquid::Template</code>.
<pre><code>
class Random < Liquid::Tag
@@ -78,7 +86,9 @@ Liquid::Template.register_tag('random', Random)
h3. Create your own tag blocks
All tag blocks are parsed by liquid. To create a new block you just have to inherit from Liquid::Block and register your block with Liquid::Template
All tag blocks are parsed by Liquid.
To create a new block,
you just have to inherit from <code>Liquid::Block</code> and register your block with <code>Liquid::Template</code>.
<pre><code>
class Random < Liquid::Block
@@ -100,6 +110,7 @@ Liquid::Template.register_tag('random', Random)
</code></pre>
<pre><code>
@template = Liquid::Template.parse(" {% random 5 %} wanna hear a joke? {% endrandom %} ")
text = " {% random 5 %} wanna hear a joke? {% endrandom %} "
@template = Liquid::Template.parse(text)
@template.render # => in 20% of the cases this will output : wanna hear a joke?
</code></pre>