Sha256: d8505c547f89f987266c4fdf521ed834d286e0b5f3988d6bf6d633d9164e630a
Contents?: true
Size: 1.88 KB
Versions: 3
Compression:
Stored size: 1.88 KB
Contents
module Liquid # If is the conditional block # # {% if user.admin %} # Admin user! # {% else %} # Not admin user # {% endif %} # # There are {% if count < 5 %} less {% else %} more {% endif %} items than you need. # # class If < Block SyntaxHelp = "Syntax Error in tag 'if' - Valid syntax: if [expression]" Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/ def initialize(tag_name, markup, tokens) @blocks = [] push_block('if', markup) super end def unknown_tag(tag, markup, tokens) if ['elsif', 'else'].include?(tag) push_block(tag, markup) else super end end def render(context) context.stack do @blocks.each do |block| if block.evaluate(context) return render_all(block.attachment, context) end end '' end end private def push_block(tag, markup) block = if tag == 'else' ElseCondition.new else expressions = markup.split(/\b(and|or)\b/).reverse raise(SyntaxError, SyntaxHelp) unless expressions.shift =~ Syntax condition = Condition.new($1, $2, $3) while not expressions.empty? operator = expressions.shift raise(SyntaxError, SyntaxHelp) unless expressions.shift.to_s =~ Syntax new_condition = Condition.new($1, $2, $3) new_condition.send(operator.to_sym, condition) condition = new_condition end condition end @blocks.push(block) @nodelist = block.attach(Array.new) end end Template.register_tag('if', If) end
Version data entries
3 entries across 3 versions & 3 rubygems
Version | Path |
---|---|
jeremyf-liquid-2.0.2 | lib/liquid/tags/if.rb |
tobi-liquid-2.0.1 | lib/liquid/tags/if.rb |
agilitic-liquid-2.0.1 | lib/liquid/tags/if.rb |