Sha256: fced1dde160dffe4998df2c865f6a034d8872cc487e52e2f58021b59cfbc09e6

Contents?: true

Size: 883 Bytes

Versions: 5

Compression:

Stored size: 883 Bytes

Contents

# frozen_string_literal: true

module Liquid
  # 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
  #
  class Increment < Tag
    def initialize(tag_name, markup, options)
      super
      @variable = markup.strip
    end

    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
  end

  Template.register_tag('increment', Increment)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
liquid-5.3.0 lib/liquid/tags/increment.rb
liquid-5.2.0 lib/liquid/tags/increment.rb
liquid-5.1.0 lib/liquid/tags/increment.rb
liquid-5.0.1 lib/liquid/tags/increment.rb
liquid-5.0.0 lib/liquid/tags/increment.rb