Search code examples
twig

"not something is defined" vs "something is not defined"


Is there a difference between the notation not something is defined vs something is not defined. To me it looks like the same behavior but maybe I miss something here.


Solution

  • No, the compiled result is the same. Guess it's just a preference

    Snippet

    {% if not something is defined %}
    
    {% endif %}
    
    -----------------------------------------
    
    {% if something is not defined %}
    
    {% endif %}
    

    Compiled result

        protected function doDisplay(array $context, array $blocks = [])
        {
            $macros = $this->macros;
            // line 1
            if ( !array_key_exists("something", $context)) {
                // line 2
                echo "
    ";
            }
            // line 4
            echo "
    -----------------------------------------
    
    ";
            // line 7
            if ( !array_key_exists("something", $context)) {
                // line 8
                echo "
    ";
            }
        }
    

    demo