Class: Liquid::Block

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

Direct Known Subclasses

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

Constant Summary collapse

FullToken =
/\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
ContentOfVariable =
/\A#{VariableStart}(.*)#{VariableEnd}\z/om
TAGSTART =
"{%".freeze
VARSTART =
"{{".freeze

Instance Attribute Summary

Attributes inherited from Tag

#line_number, #nodelist, #options

Instance Method Summary collapse

Methods inherited from Tag

#initialize, #name, parse, #raw

Methods included from ParserSwitching

#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
end

#block_delimiterObject



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

def block_delimiter
  @block_delimiter ||= "end#{block_name}"
end

#block_nameObject



91
92
93
# File 'lib/liquid/block.rb', line 91

def block_name
  @tag_name
end

#create_variable(token) ⇒ Object

Raises:



99
100
101
102
103
104
105
# File 'lib/liquid/block.rb', line 99

def create_variable(token)
  token.scan(ContentOfVariable) do |content|
    markup = token.is_a?(Token) ? token.child(content.first) : content.first
    return Variable.new(markup, @options)
  end
  raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, :token => token, :tag_end => VariableEnd.inspect))
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

  while token = tokens.shift
    begin
      unless token.empty?
        case
        when token.start_with?(TAGSTART)
          if token =~ FullToken

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

            # fetch the tag from registered blocks
            if tag = Template.tags[$1]
              markup = token.is_a?(Token) ? token.child($2) : $2
              new_tag = tag.parse($1, markup, tokens, @options)
              new_tag.line_number = token.line_number if token.is_a?(Token)
              @blank &&= new_tag.blank?
              @nodelist << 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 token.start_with?(VARSTART)
          new_var = create_variable(token)
          new_var.line_number = token.line_number if token.is_a?(Token)
          @nodelist << new_var
          @blank = false
        else
          @nodelist << token
          @blank &&= (token =~ /\A\s*\z/)
        end
      end
    rescue SyntaxError => e
      e.set_line_number_from_token(token)
      raise
    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



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

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

#render_token_with_profiling(token, context) ⇒ Object Also known as: render_token



3
4
5
6
7
# File 'lib/liquid/profiler/hooks.rb', line 3

def render_token_with_profiling(token, context)
  Profiler.profile_token_render(token) do
    render_token_without_profiling(token, context)
  end
end

#unknown_tag(tag, params, tokens) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/liquid/block.rb', line 77

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

  (nodelist || []).each do |node|
    all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
  end

  all_warnings
end