Sha256: 357ec9deb51624736ee044951d1974b04480da7c1b64c6e57a3627be89caf859

Contents?: true

Size: 997 Bytes

Versions: 5

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true

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, options)
      super
      @variable = markup.strip
    end

    def render_to_output_buffer(context, output)
      value = context.environments.first[@variable] ||= 0
      value -= 1
      context.environments.first[@variable] = value
      output << value.to_s
      output
    end
  end

  Template.register_tag('decrement', Decrement)
end

Version data entries

5 entries across 5 versions & 1 rubygems

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