mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Migrated from liquid-for-programmers v11
+22
-22
@@ -5,8 +5,8 @@ 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>
|
<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"
|
||||||
</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.
|
||||||
@@ -65,17 +65,17 @@ 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>
|
<pre>
|
||||||
class Random < Liquid::Tag
|
class Random < Liquid::Tag
|
||||||
def initialize(tag_name, max, tokens)
|
def initialize(tag_name, max, tokens)
|
||||||
super
|
super
|
||||||
@max = max.to_i
|
@max = max.to_i
|
||||||
end
|
|
||||||
|
|
||||||
def render(context)
|
|
||||||
rand(@max).to_s
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render(context)
|
||||||
|
rand(@max).to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('random', Random)
|
Liquid::Template.register_tag('random', Random)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -91,20 +91,20 @@ 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>
|
<pre>
|
||||||
class Random < Liquid::Block
|
class Random < Liquid::Block
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
super
|
super
|
||||||
@rand = markup.to_i
|
@rand = markup.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
if rand(@rand) == 0
|
if rand(@rand) == 0
|
||||||
super
|
super
|
||||||
else
|
else
|
||||||
''
|
''
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Liquid::Template.register_tag('random', Random)
|
Liquid::Template.register_tag('random', Random)
|
||||||
</pre>
|
</pre>
|
||||||
|
|||||||
Reference in New Issue
Block a user