Sha256: 1e43c48f203f591c62789c63bb743ad4a93ae031da4abf68e14ece7ab1318658

Contents?: true

Size: 1.62 KB

Versions: 46

Compression:

Stored size: 1.62 KB

Contents

module Liquid
  
  # 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>
  #
  class Cycle < Tag
    SimpleSyntax = /^#{QuotedFragment}+/        
    NamedSyntax  = /^(#{QuotedFragment})\s*\:\s*(.*)/
  
    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("Syntax Error in 'cycle' - Valid syntax: cycle [name :] var [, var2, var3 ...]")
      end
      super    
    end    
  
    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
  
    private
  
    def variables_from_string(markup)
      markup.split(',').collect do |var|
    	  var =~ /\s*(#{QuotedFragment})\s*/
    	  $1 ? $1 : nil
    	end.compact
    end
  
  end
  
  Template.register_tag('cycle', Cycle)
end

Version data entries

46 entries across 46 versions & 9 rubygems

Version Path
jeremyf-liquid-2.0.2 lib/liquid/tags/cycle.rb
tobi-liquid-2.0.1 lib/liquid/tags/cycle.rb
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/liquid-2.2.2/lib/liquid/tags/cycle.rb
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/liquid-2.2.2/lib/liquid/tags/cycle.rb
spinto-liquid-2.3.0.3 lib/liquid/tags/cycle.rb
spinto-liquid-2.3.0.2 lib/liquid/tags/cycle.rb
spinto-liquid-2.3.0.1 lib/liquid/tags/cycle.rb
liquid-2.3.0 lib/liquid/tags/cycle.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/liquid-2.2.2/lib/liquid/tags/cycle.rb
liquid_cms-0.3.2.0 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.2.2.0 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.2.1.1 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.3.1.0 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.2.1.0 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.2.0.13 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.3.0.10 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.3.0.9 lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.2.0.12 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.2.0.11 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/tags/cycle.rb
liquid_cms-0.2.0.10 generators/liquid_cms/templates/vendor/plugins/cms_plugins/liquid/lib/liquid/tags/cycle.rb