Class: Liquid::Cycle

### Home Classes Methods

### In Files

- lib/liquid/tags/cycle.rb

### Parent

Liquid::Tag

### Methods

- ::new
- #blank?
- #render

### Class/Module Index

Quicksearch

- Liquid
- Liquid::ArgumentError
- Liquid::Assign
- Liquid::BlankFileSystem
- Liquid::Block
- Liquid::Break
- Liquid::BreakInterrupt
- Liquid::Capture
- Liquid::Case
- Liquid::Comment
- Liquid::Context
- Liquid::ContextError
- Liquid::Continue
- Liquid::ContinueInterrupt
- Liquid::Cycle
- Liquid::Decrement
- Liquid::Document
- Liquid::Drop
- Liquid::ElseCondition
- Liquid::Error
- Liquid::FileSystemError
- Liquid::FilterNotFound
- Liquid::For
- Liquid::I18n
- Liquid::I18n::TranslationError
- Liquid::If
- Liquid::Ifchanged
- Liquid::Include
- Liquid::Increment
- Liquid::Interrupt
- Liquid::Lexer
- Liquid::LocalFileSystem
- Liquid::MemoryError
- Liquid::Parser
- Liquid::Raw
- Liquid::StackLevelError
- Liquid::StandardError
- Liquid::StandardFilters
- Liquid::SyntaxError
- Liquid::TableRow
- Liquid::Tag
- Liquid::Template
- Liquid::Unless
- Liquid::Utils
- Liquid::Variable
- FalseClass
- LiquidView
- Module
- NilClass
- TrueClass

No matching classes.

# Liquid::Cycle

Cycle is usually used within a loop to alternate between values, like colors or DOM classes.

    {% for item in items %}
      <div class="{% cycle 'red', 'green', 'blue' %}"> {{ item }} </div>
    {% end %}

     <div class="red"> Item one </div>
     <div class="green"> Item two </div>
     <div class="blue"> Item three </div>
     <div class="red"> Item four </div>
     <div class="green"> Item five</div>

### Constants

NamedSyntax
SimpleSyntax

### Public Class Methods

new(tag_name, markup, tokens) click to toggle source

    # File lib/liquid/tags/cycle.rb, line 18
    def initialize(tag_name, markup, tokens)
      case markup
      when NamedSyntax
        @variables = variables_from_string($2)
        @name = $1
      when SimpleSyntax
        @variables = variables_from_string(markup)
        @name = "'#{@variables.to_s}'"
      else
        raise SyntaxError.new(options[:locale].t("errors.syntax.cycle"))
      end
      super
    end

### Public Instance Methods

blank?() click to toggle source

    # File lib/liquid/tags/cycle.rb, line 46
    def blank?
      false
    end

render(context) click to toggle source

    # File lib/liquid/tags/cycle.rb, line 32
    def render(context)
      context.registers[:cycle] ||= Hash.new(0)

      context.stack do
        key = context[@name]
        iteration = context.registers[:cycle][key]
        result = context[@variables[iteration]]
        iteration += 1
        iteration  = 0  if iteration >= @variables.size
        context.registers[:cycle][key] = iteration
        result
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.