Sha256: 0d7c659e14ed641ff47a0d61679722c4917a5f5f23a3a46545dcc582ea300be6

Contents?: true

Size: 939 Bytes

Versions: 6

Compression:

Stored size: 939 Bytes

Contents

module Liquid

  # decrement 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.
  #     NOTE: decrement is a pre-decrement, --i,
  #           while increment is post:      i++.
  #
  #     (To achieve the survival, the application must keep the context)
  #
  #     if the variable does not exist, it is created with value 0.

  #   Hello: {% decrement variable %}
  #
  # gives you:
  #
  #    Hello: -1
  #    Hello: -2
  #    Hello: -3
  #
  class Decrement < Tag
    def initialize(tag_name, markup, tokens)
      @variable = markup.strip

      super
    end

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

    private
  end

  Template.register_tag('decrement', Decrement)
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
liquid-2.6.3 lib/liquid/tags/decrement.rb
liquid-2.6.2 lib/liquid/tags/decrement.rb
liquid-2.6.1 lib/liquid/tags/decrement.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/liquid-2.6.0/lib/liquid/tags/decrement.rb
liquid-2.6.0 lib/liquid/tags/decrement.rb
liquid-2.6.0.rc1 lib/liquid/tags/decrement.rb