Class: Twig::TokenParser::Block

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/token_parser/block.rb

Overview

Marks a section of a template as being reusable.

{% block head %}
  <link rel="stylesheet" href="style.css" />
  <title>{% block title %}{% endblock %} - My Webpage</title>
{% endblock %}

Instance Attribute Summary

Attributes inherited from Base

#parser

Instance Method Summary collapse

Instance Method Details

#parse(token) ⇒ 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
# File 'lib/twig/token_parser/block.rb', line 12

def parse(token)
  lineno = token.lineno
  stream = parser.stream
  name = stream.expect(Token::NAME_TYPE).value
  block = Node::Block.new(name, Node::Empty.new, lineno)

  parser.set_block(name, block)
  parser.push_local_scope
  parser.push_block_stack(name)

  if stream.next_if(Token::BLOCK_END_TYPE)
    body = parser.subparse(method(:decide_block_end), drop_needle: true)

    if (token = stream.next_if(Token::NAME_TYPE)) && token.value != name
      raise "Expected end block for #{name}, given #{token.value}"
    end
  else
    body = Node::Nodes.new({
      0 => Node::Print.new(parser.parse_expression, lineno),
    })
  end

  stream.expect(Token::BLOCK_END_TYPE)
  block.nodes[:body] = body

  parser.pop_block_stack
  parser.pop_local_scope

  Node::BlockReference.new(name, lineno)
end

#tagObject



43
44
45
# File 'lib/twig/token_parser/block.rb', line 43

def tag
  'block'
end