PHP code example of silent / twig-const-resolver

1. Go to this page and download the library: Download silent/twig-const-resolver library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

silent / twig-const-resolver example snippets


if (((isset($context["usertype"]) ? $context["usertype"] : null) == twig_constant("Users::TYPE_TROLL"))) {
    // twig_constant will be evaluated in runtime
    echo "Bye-bye";
} else {
    echo "Hello";
}

if (((isset($context["usertype"]) ? $context["usertype"] : null) == Users::TYPE_TROLL)) {
    // constant is resolved at the build step (Users::TYPE_TROLL)
    echo "Bye-bye";
} else {
    echo "Hello";
}

if (((isset($context["usertype"]) ? $context["usertype"] : null) == 2)) {
    // constant is resolved and evaluated at the build step (Users::TYPE_TROLL => 2)
    echo "Bye-bye";
} else {
    echo "Hello";
}

$twig->addExtension(
    /**
     * @param $evaluate bool Evaluate consts (default: false)
     */
    new \silent\Twig\ConstantResolverExtension\Extension()
);