Sha256: f694ef643cff4da395b8e8230afa94000e7d72425f8e41fb761cb79c821d5140

Contents?: true

Size: 948 Bytes

Versions: 2

Compression:

Stored size: 948 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, context)
      @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

2 entries across 2 versions & 1 rubygems

Version Path
locomotive_liquid-2.4.2 lib/liquid/tags/decrement.rb
locomotive_liquid-2.4.1 lib/liquid/tags/decrement.rb