Sha256: 5f90e5885a1ebf5fb80f6ab3f5a430939bb115d6e0d9a04c5e4101665c08d2d6

Contents?: true

Size: 810 Bytes

Versions: 23

Compression:

Stored size: 810 Bytes

Contents

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(context)
      value = context.environments.first[@variable] ||= 0
      context.environments.first[@variable] = value + 1
      value.to_s
    end
  end

  Template.register_tag('increment'.freeze, Increment)
end

Version data entries

23 entries across 23 versions & 4 rubygems

Version Path
liquid-4.0.4 lib/liquid/tags/increment.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/liquid-4.0.3/lib/liquid/tags/increment.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/liquid-4.0.3/lib/liquid/tags/increment.rb
liquid-4.0.3 lib/liquid/tags/increment.rb
liquid-4.0.2 lib/liquid/tags/increment.rb
liquid-4-0-2-4.0.2 lib/liquid/tags/increment.rb
liquid-4.0.1 lib/liquid/tags/increment.rb
liquid-4.0.0 lib/liquid/tags/increment.rb
liquid-4.0.0.rc3 lib/liquid/tags/increment.rb
liquid-4.0.0.rc2 lib/liquid/tags/increment.rb
liquid-4.0.0.rc1 lib/liquid/tags/increment.rb
locomotivecms-liquid-4.0.0 lib/liquid/tags/increment.rb
liquid-3.0.6 lib/liquid/tags/increment.rb
liquid-3.0.5 lib/liquid/tags/increment.rb
liquid-3.0.4 lib/liquid/tags/increment.rb
liquid-3.0.3 lib/liquid/tags/increment.rb
liquid-3.0.2 lib/liquid/tags/increment.rb
locomotivecms-liquid-4.0.0.alpha2 lib/liquid/tags/increment.rb
locomotivecms-liquid-4.0.0.alpha1 lib/liquid/tags/increment.rb
locomotivecms-liquid-4.0.0.alpha lib/liquid/tags/increment.rb