tags:

views:

43

answers:

2

Hello. What construct should I use to check whether a value is NULL in a Twig template?

+3  A: 

I don't think you can. This is because if a variable is undefined (not set) in the twig template, it looks like NULL or none (in twig terms). I'm pretty sure this is to suppress bad access errors from occurring in the template.

Due to the lack of a "identity" in Twig (===) this is the best you can do

{% if var == null %}
    stuff in here
{% endif %}

Which translates to:

if ((isset($context['somethingnull']) ? $context['somethingnull'] : null) == null)
{
  echo "stuff in here";
}

Which if your good at your type juggling, means that things such as 0, '', FALSE, NULL, and an undefined var will also make that statement true.

My suggest is to ask for the identity to be implemented into Twig.

Kendall Hopkins
Kendall is right to suggest that they implement it - I've had nothing but good experiences asking for things to be implemented on Twig's github issue tracker. They're very friendly and professional.
Shabbyrobe
+2  A: 

Update: As of Twig 0.9.9, and as a result of the github ticket about this question mentioned below, there is now a way to test for null that does not require my recommended modifications.

{% if foo.attribute is sameas(false) %}
    the foo attribute really is the 'false' PHP value
{% endif %}

One of Twig's greatest strengths is how easy the code is to follow and hack on. It's a wonderful example of self-documentation at work.

If you're game, you can change the code of Twig pretty easily to support the === operator. I actually need this feature as well so I posted my solution on the github: http://github.com/fabpot/Twig/issues/88

Using the latest trunk, you can make the following changes and it will work.

In Twig_ExpressionParser:

public function parseCompareExpression()
{
-        static $operators = array('==', '!=', '<', '>', '>=', '<=');
+        static $operators = array('===', '==', '!=', '<', '>', '>=', '<=');
         $lineno = $this->parser->getCurrentToken()->getLine();

and in Twig_Lexer:

-    const REGEX_OPERATOR = '/<=? | >=? | [!=]= | = | \/\/ | \.\. | [(){}.,%*\/+~|-] | \[ | \] | \? | \:/Ax';
+    const REGEX_OPERATOR = '/<=? | >=? | [!=]==? | = | \/\/ | \.\. | [(){}.,%*\/+~|-] | \[ | \] | \? | \:/Ax';

The following should then work for you:

index.php

<?php

require_once '/blah/blah/blah/Twig/lib/Twig/Autoloader.php';

Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem(dirname(__FILE__)."/templates/");
$options = array(
    'cache'=>dirname(__FILE__)."/cache/",
);
$e = new Twig_Environment($loader, $options);
$template = $e->loadTemplate('index.html');
echo $template->render(array(
    'wahey'=>'foobar', 
    'nulltest'=>null,
    'zerotest'=>0
));

templates/index.html

{% if wahey == "foobar" %}
<p>Yep</p>
{% endif %}

{% if nulltest == null %}
<p>nulltest == null</p>
{% endif %}

{% if nulltest === null %}
<p>nulltest === null</p>
{% endif %}

{% if zerotest == null %}
<p>zerotest == null</p>
{% endif %}

{% if zerotest === null %}
<p>zerotest === null</p>
{% endif %}

The output should be as follows:

Yep

nulltest == null

nulltest === null

zerotest == null
Shabbyrobe