Migrated from liquid-for-programmers v10

RichMorin
2010-08-12 13:45:54 -07:00
parent ff06aeea32
commit c61c2b725e
+18 -18
@@ -4,10 +4,10 @@ It's very simple to get started with Liquid.
A Liquid template is rendered in two steps: Parse and Render. A Liquid template is rendered in two steps: Parse and Render.
For an overview of the Liquid syntax, please read [[Liquid for Designers]]. For an overview of the Liquid syntax, please read [[Liquid for Designers]].
<pre><code> <pre>
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template @template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
@template.render( 'name' => 'tobi' ) # Renders the output => "hi tobi" @template.render( 'name' => 'tobi' ) # Renders the output => "hi tobi"
</code></pre> </pre>
The @parse@ step creates a fully compiled template which can be re-used as often as you like. 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. You can store it in memory or in a cache for faster rendering later.
@@ -28,22 +28,22 @@ Basically, they are just methods which take one parameter and return a modified
You can use your own filters by passing an array of modules to the render call like this: 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>. <code>@template.render(assigns, [MyTextFilters, MyDateFilters])</code>.
<pre><code> <pre>
module TextFilter module TextFilter
def textilize(input) def textilize(input)
RedCloth.new(input).to_html RedCloth.new(input).to_html
end end
end end
</code></pre> </pre>
<pre><code> <pre>
@template = Liquid::Template.parse(" {{ '*hi*' | textilize }} ") @template = Liquid::Template.parse(" {{ '*hi*' | textilize }} ")
@template.render({}, :filters => [TextFilter]) # => "<b>hi</b>" @template.render({}, :filters => [TextFilter]) # => "<b>hi</b>"
</code></pre> </pre>
Alternatively, you can register your filters globally: Alternatively, you can register your filters globally:
<pre><code> <pre>
module TextFilter module TextFilter
def textilize(input) def textilize(input)
RedCloth.new(input).to_html RedCloth.new(input).to_html
@@ -51,20 +51,20 @@ module TextFilter
end end
Liquid::Template.register_filter(TextFilter) Liquid::Template.register_filter(TextFilter)
</code></pre> </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> <pre>
@template = Liquid::Template.parse(" {{ '*hi*' | textilize }} ") @template = Liquid::Template.parse(" {{ '*hi*' | textilize }} ")
@template.render # => "<b>hi</b>" @template.render # => "<b>hi</b>"
</code></pre> </pre>
h3. Create your own tags 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 @Liquid::Tag@ and register your block with @Liquid::Template@.
<pre><code> <pre>
class Random < Liquid::Tag class Random < Liquid::Tag
def initialize(tag_name, max, tokens) def initialize(tag_name, max, tokens)
super super
@@ -77,12 +77,12 @@ To create a new tag, simply inherit from @Liquid::Tag@ and register your block w
end end
Liquid::Template.register_tag('random', Random) Liquid::Template.register_tag('random', Random)
</code></pre> </pre>
<pre><code> <pre>
@template = Liquid::Template.parse(" {% random 5 %}") @template = Liquid::Template.parse(" {% random 5 %}")
@template.render # => "3" @template.render # => "3"
</code></pre> </pre>
h3. Create your own tag blocks h3. Create your own tag blocks
@@ -90,7 +90,7 @@ All tag blocks are parsed by Liquid.
To create a new block, To create a new block,
you just have to inherit from @Liquid::Block@ and register your block with @Liquid::Template@. you just have to inherit from @Liquid::Block@ and register your block with @Liquid::Template@.
<pre><code> <pre>
class Random < Liquid::Block class Random < Liquid::Block
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
super super
@@ -107,10 +107,10 @@ you just have to inherit from @Liquid::Block@ and register your block with @Liqu
end end
Liquid::Template.register_tag('random', Random) Liquid::Template.register_tag('random', Random)
</code></pre> </pre>
<pre><code> <pre>
text = " {% random 5 %} wanna hear a joke? {% endrandom %} " text = " {% random 5 %} wanna hear a joke? {% endrandom %} "
@template = Liquid::Template.parse(text) @template = Liquid::Template.parse(text)
@template.render # => In 20% of the cases, this will output "wanna hear a joke?" @template.render # => In 20% of the cases, this will output "wanna hear a joke?"
</code></pre> </pre>