Sha256: 40bcd35ba2c762b7d8af04094a7653bd2179fe611551eb957f7354c2164dbedb
Contents?: true
Size: 2 KB
Versions: 3
Compression:
Stored size: 2 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})?/ ExpressionsAndOperators = /(?:\b(?:and|or)\b|(?:\s*(?!\b(?:and|or)\b)(?:#{QuotedFragment}|\S+)\s*)+)/ 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.scan(ExpressionsAndOperators).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 & 1 rubygems
Version | Path |
---|---|
liquid-2.2.0 | lib/liquid/tags/if.rb |
liquid-2.1.3 | lib/liquid/tags/if.rb |
liquid-2.1.2 | lib/liquid/tags/if.rb |