class Liquid::If

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.

Constants

BOOLEAN_OPERATORS
ExpressionsAndOperators
Syntax

Public Class Methods

new(tag_name, markup, options) click to toggle source
Calls superclass method Liquid::Block::new
# File lib/liquid/tags/if.rb, line 17
def initialize(tag_name, markup, options)
  super
  @blocks = []
  push_block('if'.freeze, markup)
end

Public Instance Methods

nodelist() click to toggle source
# File lib/liquid/tags/if.rb, line 28
def nodelist
  @blocks.map(&:attachment)
end
parse(tokens) click to toggle source
# File lib/liquid/tags/if.rb, line 23
def parse(tokens)
  while parse_body(@blocks.last.attachment, tokens)
  end
end
render(context) click to toggle source
# File lib/liquid/tags/if.rb, line 40
def render(context)
  context.stack do
    @blocks.each do |block|
      if block.evaluate(context)
        return block.attachment.render(context)
      end
    end
    ''.freeze
  end
end
unknown_tag(tag, markup, tokens) click to toggle source
Calls superclass method Liquid::Block#unknown_tag
# File lib/liquid/tags/if.rb, line 32
def unknown_tag(tag, markup, tokens)
  if ['elsif'.freeze, 'else'.freeze].include?(tag)
    push_block(tag, markup)
  else
    super
  end
end

Private Instance Methods

lax_parse(markup) click to toggle source
# File lib/liquid/tags/if.rb, line 64
def lax_parse(markup)
  expressions = markup.scan(ExpressionsAndOperators)
  raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop =~ Syntax

  condition = Condition.new(Expression.parse($1), $2, Expression.parse($3))

  until expressions.empty?
    operator = expressions.pop.to_s.strip

    raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop.to_s =~ Syntax

    new_condition = Condition.new(Expression.parse($1), $2, Expression.parse($3))
    raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless BOOLEAN_OPERATORS.include?(operator)
    new_condition.send(operator, condition)
    condition = new_condition
  end

  condition
end
parse_binary_comparison(p) click to toggle source
# File lib/liquid/tags/if.rb, line 91
def parse_binary_comparison(p)
  condition = parse_comparison(p)
  if op = (p.id?('and'.freeze) || p.id?('or'.freeze))
    condition.send(op, parse_binary_comparison(p))
  end
  condition
end
parse_comparison(p) click to toggle source
# File lib/liquid/tags/if.rb, line 99
def parse_comparison(p)
  a = Expression.parse(p.expression)
  if op = p.consume?(:comparison)
    b = Expression.parse(p.expression)
    Condition.new(a, op, b)
  else
    Condition.new(a)
  end
end
push_block(tag, markup) click to toggle source
# File lib/liquid/tags/if.rb, line 53
def push_block(tag, markup)
  block = if tag == 'else'.freeze
    ElseCondition.new
  else
    parse_with_selected_parser(markup)
  end

  @blocks.push(block)
  block.attach(BlockBody.new)
end
strict_parse(markup) click to toggle source
# File lib/liquid/tags/if.rb, line 84
def strict_parse(markup)
  p = Parser.new(markup)
  condition = parse_binary_comparison(p)
  p.consume(:end_of_string)
  condition
end