Class: Liquid::Increment

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/tags/increment.rb

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_number, #nodelist, #parse_context, #tag_name

Instance Method Summary collapse

Methods inherited from Tag

#blank?, disable_tags, #name, #parse, parse, #raw, #render

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, options) ⇒ Increment

Returns a new instance of Increment.


20
21
22
23
# File 'lib/liquid/tags/increment.rb', line 20

def initialize(tag_name, markup, options)
  super
  @variable = markup.strip
end

Instance Method Details

#render_to_output_buffer(context, output) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/liquid/tags/increment.rb', line 25

def render_to_output_buffer(context, output)
  value = context.environments.first[@variable] ||= 0
  context.environments.first[@variable] = value + 1

  output << value.to_s
  output
end