Class: Liquid::If

Inherits:
Block show all
Defined in:
lib/liquid/tags/if.rb

Overview

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.

Direct Known Subclasses

Unless

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

Syntax =
/(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/o
ExpressionsAndOperators =
/(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QuotedFragment}|\S+)\s*)+)/o
BOOLEAN_OPERATORS =
%w(and or).freeze

Constants inherited from Block

Block::MAX_DEPTH

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #parse_context, #tag_name

Instance Method Summary collapse

Methods inherited from Block

#blank?, #block_delimiter, #block_name, #raise_tag_never_closed, raise_unknown_tag, #render

Methods inherited from Tag

#blank?, disable_tags, #name, parse, #raw, #render

Methods included from ParserSwitching

#parse_with_selected_parser, #strict_parse_with_error_mode_fallback

Constructor Details

#initialize(tag_name, markup, options) ⇒ If

Returns a new instance of If.



21
22
23
24
25
# File 'lib/liquid/tags/if.rb', line 21

def initialize(tag_name, markup, options)
  super
  @blocks = []
  push_block('if', markup)
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



19
20
21
# File 'lib/liquid/tags/if.rb', line 19

def blocks
  @blocks
end

Instance Method Details

#nodelistObject



27
28
29
# File 'lib/liquid/tags/if.rb', line 27

def nodelist
  @blocks.map(&:attachment)
end

#parse(tokens) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/liquid/tags/if.rb', line 31

def parse(tokens)
  while parse_body(@blocks.last.attachment, tokens)
  end
  @blocks.reverse_each do |block|
    block.attachment.remove_blank_strings if blank?
    block.attachment.freeze
  end
end

#render_to_output_buffer(context, output) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/liquid/tags/if.rb', line 51

def render_to_output_buffer(context, output)
  @blocks.each do |block|
    result = Liquid::Utils.to_liquid_value(
      block.evaluate(context)
    )

    if result
      return block.attachment.render_to_output_buffer(context, output)
    end
  end

  output
end

#unknown_tag(tag, markup, tokens) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/liquid/tags/if.rb', line 43

def unknown_tag(tag, markup, tokens)
  if ELSE_TAG_NAMES.include?(tag)
    push_block(tag, markup)
  else
    super
  end
end