Class: Liquid::Block

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

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from Tag

#line, #nodelist, #options

Instance Method Summary collapse

Methods inherited from Tag

#initialize, #name, new_with_options, #parse_with_selected_parser

Constructor Details

This class inherits a constructor from Liquid::Tag

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?
  @blank || false
end

#block_delimiterObject



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

def block_delimiter
  "end#{block_name}"
end

#block_nameObject



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

def block_name
  @tag_name
end

#create_variable(token) ⇒ Object

Raises:



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

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", :token => token, :tag_end => VariableEnd.inspect), line)
end

#end_tagObject



81
82
# File 'lib/liquid/block.rb', line 81

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
64
65
66
67
# File 'lib/liquid/block.rb', line 12

def parse(tokens)
  self.line ||= 1
  @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.new_with_options($1, $2, tokens, @options.merge(line: line))
          @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", :token => token, :tag_end => TagEnd.inspect), line)
      end
    when IsVariable
      new_var = create_variable(token)
      @nodelist << new_var
      @children << new_var
      @blank = false
    when ''
      # pass
    else
      self.line += token.count("\n") if @options[:count_lines] || Template.count_lines
      @nodelist << token
      @blank &&= (token =~ /\A\s*\z/)
    end
  end

  # restore the line

  # 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



113
114
115
# File 'lib/liquid/block.rb', line 113

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

#unknown_tag(tag, params, tokens) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/liquid/block.rb', line 84

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

#warningsObject

warnings of this block and all sub-tags



70
71
72
73
74
75
76
77
78
79
# File 'lib/liquid/block.rb', line 70

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

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

  all_warnings
end