class Liquid::Increment

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

Public Class Methods

new(tag_name, markup, options) click to toggle source
Calls superclass method Liquid::Tag::new
# File lib/liquid/tags/increment.rb, line 20
def initialize(tag_name, markup, options)
  super
  @variable = markup.strip
end

Public Instance Methods

render_to_output_buffer(context, output) click to toggle source
# 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