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

Constant Summary collapse

SyntaxHelp =
"Syntax Error in tag 'if' - Valid syntax: if [expression]"
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

Constants inherited from Block

Block::ContentOfVariable, Block::FullToken, Block::IsTag, Block::IsVariable

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Block

#block_delimiter, #block_name, #create_variable, #end_tag, #parse

Methods inherited from Tag

#name, #parse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ If

Returns a new instance of If.



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

def initialize(tag_name, markup, tokens)
  @blocks = []

  push_block('if', markup)

  super
end

Instance Method Details

#render(context) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/liquid/tags/if.rb', line 35

def render(context)
  context.stack do
    @blocks.each do |block|
      if block.evaluate(context)
        return render_all(block.attachment, context)
      end
    end
    ''
  end
end

#unknown_tag(tag, markup, tokens) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/liquid/tags/if.rb', line 27

def unknown_tag(tag, markup, tokens)
  if ['elsif', 'else'].include?(tag)
    push_block(tag, markup)
  else
    super
  end
end