Class: Twig::TokenParser::Embed

Inherits:
Include show all
Defined in:
lib/twig/token_parser/embed.rb

Instance Attribute Summary

Attributes inherited from Base

#parser

Instance Method Summary collapse

Instance Method Details

#parse(token) ⇒ Object



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
46
47
48
49
# File 'lib/twig/token_parser/embed.rb', line 8

def parse(token)
  stream = parser.stream
  parent = parser.parse_expression

  variables, only, ignore_missing = parse_arguments

  parent_token = fake_parent_token = Token.new(Token::STRING_TYPE, '__parent__', token.lineno)

  if parent.is_a?(Node::Expression::Constant)
    parent_token = Token.new(Token::STRING_TYPE, parent.attributes[:value], token.lineno)
  elsif parent.is_a?(Node::Expression::Variable::Context)
    parent_token = Token.new(Token::NAME_TYPE, parent.attributes[:name], token.lineno)
  end

  # inject a fake parent to make the parent() function work
  stream.inject([
    Token.new(Token::BLOCK_START_TYPE, '', token.lineno),
    Token.new(Token::NAME_TYPE, 'extends', token.lineno),
    parent_token,
    Token.new(Token::BLOCK_END_TYPE, '', token.lineno),
  ])

  node = parser.parse(stream, method(:decide_block_end), drop_needle: true)

  # override the parent with the correct one
  if fake_parent_token == parent_token
    node.nodes[:parent] = parent
  end

  parser.embed_template(node)

  stream.expect(Token::BLOCK_END_TYPE)

  Node::Embed.new(
    node.template_name,
    node.attributes[:index],
    variables,
    only,
    ignore_missing,
    token.lineno
  )
end

#tagObject



51
52
53
# File 'lib/twig/token_parser/embed.rb', line 51

def tag
  'embed'
end