Sha256: 7f157048ada9d2215e30deda7fe79417f3d0d82ecb4737abf3ad3367f4ccb47d

Contents?: true

Size: 848 Bytes

Versions: 1

Compression:

Stored size: 848 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, tokens, options)
      @variable = markup.strip
      super
    end

    def render(context)
      value = context.environments.first[@variable] ||= 0
      context.environments.first[@variable] = value + 1
      value.to_s
    end


    def blank?
      false
    end
  end

  Template.register_tag('increment', Increment)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locomotivecms-liquid-2.6.0 lib/liquid/tags/increment.rb