Class: Liquid::Block

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

Direct Known Subclasses

Capture, Comment, DecisionBlock, Document, For, TableRow

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Tag

#initialize, #name

Constructor Details

This class inherits a constructor from Liquid::Tag

Instance Method Details

#block_delimiterObject



61
62
63
# File 'lib/liquid/block.rb', line 61

def block_delimiter
  "end#{block_name}"
end

#block_nameObject



65
66
67
# File 'lib/liquid/block.rb', line 65

def block_name
  self.class.name.scan(/\w+$/).first.downcase
end

#create_variable(token) ⇒ Object

Raises:

  • (SyntaxError)


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

def create_variable(token)
  token.scan(/^#{VariableStart}(.*)#{VariableEnd}$/) do |content|
    return Variable.new(content.first)
  end
  raise SyntaxError.new("Variable '#{token}' was not properly terminated with regexp: #{VariableEnd.inspect} ")
end

#end_tagObject



47
48
# File 'lib/liquid/block.rb', line 47

def end_tag      
end

#parse(tokens) ⇒ Object



4
5
6
7
8
9
10
11
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
# File 'lib/liquid/block.rb', line 4

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

  while token = tokens.shift 

    case token
    when /^#{TagStart}/                   
      if token =~ /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/

        # if we found the proper block delimitor 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]
          @nodelist << tag.new($2, tokens)
        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, "Tag '#{token}' was not properly terminated with regexp: #{TagEnd.inspect} "
      end
    when /^#{VariableStart}/
      @nodelist << create_variable(token)
    when ''
      # pass
    else
      @nodelist << token
    end
  end       
  
  # Make sure that its ok to end parsing in the current block. 
  # Effectively this method will throw and exception unless the current block is 
  # of type Document 
  assert_missing_delimitation!
end

#render(context) ⇒ Object



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

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

#unknown_tag(tag, params, tokens) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/liquid/block.rb', line 50

def unknown_tag(tag, params, tokens)
  case tag 
  when 'else'
    raise SyntaxError, "#{block_name} tag does not expect else tag"
  when 'end'
    raise SyntaxError, "'end' is not a valid delimiter for #{block_name} tags. use #{block_delimiter}"
  else
    raise SyntaxError, "Unknown tag '#{tag}'"
  end
end