Class: Liquid::Block

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/block.rb

Direct Known Subclasses

Capture, Case, Comment, Document, For, If, Ifchanged, Raw, TableRow

Constant Summary

IsTag =
/\A#{TagStart}/o
IsVariable =
/\A#{VariableStart}/o
FullToken =
/\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
ContentOfVariable =
/\A#{VariableStart}(.*)#{VariableEnd}\z/om

Instance Attribute Summary

Attributes inherited from Tag

#nodelist, #options

Instance Method Summary collapse

Methods inherited from Tag

#initialize, #name, parse, #parse_with_selected_parser

Constructor Details

This class inherits a constructor from Liquid::Tag

Instance Method Details

#blank?Boolean



8
9
10
# File 'lib/liquid/block.rb', line 8

def blank?
  @blank || false
end

#block_delimiterObject



94
95
96
# File 'lib/liquid/block.rb', line 94

def block_delimiter
  "end#{block_name}"
end

#block_nameObject



98
99
100
# File 'lib/liquid/block.rb', line 98

def block_name
  @tag_name
end

#create_variable(token) ⇒ Object

Raises:



102
103
104
105
106
107
# File 'lib/liquid/block.rb', line 102

def create_variable(token)
  token.scan(ContentOfVariable) do |content|
    return Variable.new(content.first, @options)
  end
  raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, :token => token, :tag_end => VariableEnd.inspect))
end

#end_tagObject



77
78
# File 'lib/liquid/block.rb', line 77

def end_tag
end

#parse(tokens) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/liquid/block.rb', line 12

def parse(tokens)
  @blank = true
  @nodelist ||= []
  @nodelist.clear

  # All child tags of the current block.
  @children = []

  while token = tokens.shift
    case token
    when IsTag
      if token =~ FullToken

        # if we found the proper block delimiter just end parsing here and let the outer block
        # proceed
        if block_delimiter == $1
          end_tag
          return
        end

        # fetch the tag from registered blocks
        if tag = Template.tags[$1]
          new_tag = tag.parse($1, $2, tokens, @options)
          @blank &&= new_tag.blank?
          @nodelist << new_tag
          @children << new_tag
        else
          # this tag is not registered with the system
          # pass it to the current block for special handling or error reporting
          unknown_tag($1, $2, tokens)
        end
      else
        raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect))
      end
    when IsVariable
      new_var = create_variable(token)
      @nodelist << new_var
      @children << new_var
      @blank = false
    when ''.freeze
      # pass
    else
      @nodelist << token
      @blank &&= (token =~ /\A\s*\z/)
    end
  end

  # Make sure that it's ok to end parsing in the current block.
  # Effectively this method will throw an exception unless the current block is
  # of type Document
  assert_missing_delimitation!
end

#render(context) ⇒ Object



109
110
111
# File 'lib/liquid/block.rb', line 109

def render(context)
  render_all(@nodelist, context)
end

#unknown_tag(tag, params, tokens) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/liquid/block.rb', line 80

def unknown_tag(tag, params, tokens)
  case tag
  when 'else'.freeze
    raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_else".freeze,
                                             :block_name => block_name))
  when 'end'.freeze
    raise SyntaxError.new(options[:locale].t("errors.syntax.invalid_delimiter".freeze,
                                             :block_name => block_name,
                                             :block_delimiter => block_delimiter))
  else
    raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, :tag => tag))
  end
end

#warningsObject

warnings of this block and all sub-tags



66
67
68
69
70
71
72
73
74
75
# File 'lib/liquid/block.rb', line 66

def warnings
  all_warnings = []
  all_warnings.concat(@warnings) if @warnings

  (@children || []).each do |node|
    all_warnings.concat(node.warnings || [])
  end

  all_warnings
end