Class: Liquid::Increment
Overview
increment is used in a place where one needs to insert a counter
into a template, and needs the counter to survive across
multiple instantiations of the template.
(To achieve the survival, the application must keep the context)
if the variable does not exist, it is created with value 0.
Hello: {% increment variable %}
gives you:
Hello: 0
Hello: 1
Hello: 2
Instance Attribute Summary
Attributes inherited from Tag
#line, #nodelist, #options, #warnings
Instance Method Summary collapse
- #blank? ⇒ Boolean
-
#initialize(tag_name, markup, tokens, options) ⇒ Increment
constructor
A new instance of Increment.
- #render(context) ⇒ Object
Methods inherited from Tag
#name, new_with_options, #parse, #parse_with_selected_parser
Constructor Details
#initialize(tag_name, markup, tokens, options) ⇒ Increment
Returns a new instance of Increment.
18 19 20 21 |
# File 'lib/liquid/tags/increment.rb', line 18 def initialize(tag_name, markup, tokens, ) @variable = markup.strip super end |
Instance Method Details
#blank? ⇒ Boolean
30 31 32 |
# File 'lib/liquid/tags/increment.rb', line 30 def blank? false end |
#render(context) ⇒ Object
23 24 25 26 27 |
# File 'lib/liquid/tags/increment.rb', line 23 def render(context) value = context.environments.first[@variable] ||= 0 context.environments.first[@variable] = value + 1 value.to_s end |