Class: Twig::Node::Expression::Test::Constant

Inherits:
Base show all
Defined in:
lib/twig/node/expression/test/constant.rb

Overview

Checks if a variable is the exact same value as a constant.

{% if post.status is constant('Post::PUBLISHED') %}
  the status attribute is exactly the same as Post::PUBLISHED
{% endif %}

Instance Attribute Summary

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods inherited from Base

#explicit_parentheses?, #set_explicit_parentheses

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from Twig::Node::Expression::Test::Base

Instance Method Details

#compile(compiler) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twig/node/expression/test/constant.rb', line 14

def compile(compiler)
  compiler.
    raw('(').
    subcompile(nodes[:node]).
    raw(' == ::Twig::Extension::Core.constant(')

  compiler.
    subcompile(nodes[:arguments].nodes[0])

  if nodes[:arguments].nodes.key?(1)
    compiler.
      raw(', ').
      subcompile(nodes[:arguments].nodes[1])
  end

  compiler.raw('))')
end