Class: Twig::TokenParser::Macro

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

Overview

Defines a macro.

macro input(name, value, type, size) %

<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />

endmacro %

Instance Attribute Summary

Attributes inherited from Base

#parser

Instance Method Summary collapse

Instance Method Details

#decide_block_end(token) ⇒ Object



42
43
44
# File 'lib/twig/token_parser/macro.rb', line 42

def decide_block_end(token)
  token.test('endmacro')
end

#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
# File 'lib/twig/token_parser/macro.rb', line 12

def parse(token)
  lineno = token.lineno
  stream = parser.stream
  name = stream.expect(Token::NAME_TYPE).value
  arguments = parse_definition

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

  if (token = stream.next_if(Token::NAME_TYPE))
    value = token.value

    if value != name
      raise Error::Syntax.new(
        "Expected endmacro for macro \"#{name}\" (but \"#{value}\" given).",
        stream.current.lineno,
        stream.source_context
      )
    end
  end

  parser.pop_local_scope
  stream.expect(Token::BLOCK_END_TYPE)

  parser.set_macro(name, Node::Macro.new(name, Node::Body.new({ 0 => body }), arguments, lineno))

  Node::Empty.new(lineno)
end

#tagObject



46
47
48
# File 'lib/twig/token_parser/macro.rb', line 46

def tag
  'macro'
end