Sha256: c7106b2e57ca895f9f5ac7c44142a914970ba5c53f2236866c4ad53cb4403129

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Liquid
  # @liquid_public_docs
  # @liquid_type tag
  # @liquid_category variable
  # @liquid_name increment
  # @liquid_summary
  #   Creates a new variable, with a default value of 0, that's increased by 1 with each subsequent call.
  # @liquid_description
  #   Variables that are declared with `increment` are unique to the [layout](/themes/architecture/layouts), [template](/themes/architecture/templates),
  #   or [section](/themes/architecture/sections) file that they're created in. However, the variable is shared across
  #   [snippets](/themes/architecture#snippets) included in the file.
  #
  #   Similarly, variables that are created with `increment` are independent from those created with [`assign`](/docs/api/liquid/tags/assign)
  #   and [`capture`](/docs/api/liquid/tags/capture). However, `increment` and [`decrement`](/docs/api/liquid/tags/decrement) share
  #   variables.
  # @liquid_syntax
  #   {% increment variable_name %}
  # @liquid_syntax_keyword variable_name The name of the variable being incremented.
  class Increment < Tag
    attr_reader :variable_name

    def initialize(tag_name, markup, options)
      super
      @variable_name = markup.strip
    end

    def render_to_output_buffer(context, output)
      counter_environment = context.environments.first
      value = counter_environment[@variable_name] || 0
      counter_environment[@variable_name] = value + 1

      output << value.to_s
      output
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
liquid-5.7.0 lib/liquid/tags/increment.rb
liquid-5.6.4 lib/liquid/tags/increment.rb
liquid-5.6.3 lib/liquid/tags/increment.rb
liquid-5.6.2 lib/liquid/tags/increment.rb
liquid-5.6.1 lib/liquid/tags/increment.rb
liquid-5.6.0 lib/liquid/tags/increment.rb
liquid-5.6.0.rc3 lib/liquid/tags/increment.rb
liquid-5.6.0.rc2 lib/liquid/tags/increment.rb
liquid-5.6.0.rc1 lib/liquid/tags/increment.rb