mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
86 lines
1.9 KiB
Ruby
86 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Liquid
|
|
# @liquid_public_docs
|
|
# @liquid_type object
|
|
# @liquid_name forloop
|
|
# @liquid_summary
|
|
# The `forloop` object contains information about a parent [`for` loop](/api/liquid/tags#for).
|
|
class ForloopDrop < Drop
|
|
def initialize(name, length, parentloop)
|
|
@name = name
|
|
@length = length
|
|
@parentloop = parentloop
|
|
@index = 0
|
|
end
|
|
|
|
# @liquid_public_docs
|
|
# @liquid_name forloop.length
|
|
# @liquid_summary
|
|
# The number of iterations.
|
|
# @liquid_return [number]
|
|
attr_reader :length
|
|
|
|
attr_reader :parentloop
|
|
|
|
def name
|
|
Usage.increment('forloop_drop_name')
|
|
@name
|
|
end
|
|
|
|
# @liquid_public_docs
|
|
# @liquid_summary
|
|
# The 1-based index of the current iteration.
|
|
# @liquid_return [number]
|
|
def index
|
|
@index + 1
|
|
end
|
|
|
|
# @liquid_public_docs
|
|
# @liquid_summary
|
|
# The 0-based index of the current iteration.
|
|
# @liquid_return [number]
|
|
def index0
|
|
@index
|
|
end
|
|
|
|
# @liquid_public_docs
|
|
# @liquid_summary
|
|
# The 1-based index of the current iteration, in reverse order.
|
|
# @liquid_return [number]
|
|
def rindex
|
|
@length - @index
|
|
end
|
|
|
|
# @liquid_public_docs
|
|
# @liquid_summary
|
|
# The 0-based index of the current iteration, in reverse order.
|
|
# @liquid_return [number]
|
|
def rindex0
|
|
@length - @index - 1
|
|
end
|
|
|
|
# @liquid_public_docs
|
|
# @liquid_summary
|
|
# Returns `true` if the current iteration is the first. Returns `false` if not.
|
|
# @liquid_return [boolean]
|
|
def first
|
|
@index == 0
|
|
end
|
|
|
|
# @liquid_public_docs
|
|
# @liquid_summary
|
|
# Returns `true` if the current iteration is the last. Returns `false` if not.
|
|
# @liquid_return [boolean]
|
|
def last
|
|
@index == @length - 1
|
|
end
|
|
|
|
protected
|
|
|
|
def increment!
|
|
@index += 1
|
|
end
|
|
end
|
|
end
|