Class: Twig::Node::Expression::Variable::Template

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/node/expression/variable/template.rb

Constant Summary collapse

RESERVED_NAMES =
%w[varargs context macros blocks self].freeze

Instance Attribute Summary

Attributes inherited from Base

#attributes, #lineno, #nodes, #source_context, #tag

Instance Method Summary collapse

Methods inherited from Base

#explicit_parentheses?, #set_explicit_parentheses

Methods inherited from Base

#empty?, #length, #template_name, #to_s

Constructor Details

#initialize(name, lineno) ⇒ Template

Returns a new instance of Template.

Parameters:

  • name (String, Integer, nil)
  • lineno (Integer)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/twig/node/expression/variable/template.rb', line 12

def initialize(name, lineno)
  if name && %w[true false none null nil].include?(name.to_s.downcase)
    raise Error::Syntax.new("You cannot assign a value to \"#{name}\".", lineno)
  end

  # Convert to integer if name is an integer or consists of digits only
  if !name.nil? && (name.is_a?(Integer) || name == name.to_i.to_s)
    name = name.to_i
  elsif RESERVED_NAMES.include?(name)
    name = "\u035C#{name}"
  end

  super({}, { name: }, lineno)
end

Instance Method Details

#compile(compiler) ⇒ Object

Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/twig/node/expression/variable/template.rb', line 38

def compile(compiler)
  name_value = name(compiler)

  if name_value == '_self'
    compiler.raw('self')
  else
    compiler.
      raw('macros[').
      string(name_value).
      raw(']')
  end
end

#name(compiler) ⇒ String

Parameters:

Returns:

  • (String)


29
30
31
32
33
34
35
# File 'lib/twig/node/expression/variable/template.rb', line 29

def name(compiler)
  if attributes[:name].nil?
    attributes[:name] = compiler.var_name
  end

  attributes[:name]
end