Skip to content

Simple Twig plugin to resolve constant on template cache build

License

Notifications You must be signed in to change notification settings

dimarick/twig-const-resolver

 
 

Repository files navigation

Twig Constant Resolver Extension

Travis Coveralls

Simple Twig plugin to resolve constant on template cache build.

Why

For example we have something like this:

{% if usertype == constant('Users::TYPE_TROLL') %}
    Bye-bye
{% else %}
    Hello!
{% endif %}

Without this extension Twig will compile it in something like this:

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";
}

It will be compiled even if you have no constant with that name. So you will get an error in production.

With this extension you can avoid this types of errors cause it will evaluate constants at the build step, so this template will be compiled in something like this:

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

Installation

The extension is installable via composer:

{
    "require": {
        "silent/twig-const-resolver": "~1.0"
    }
}

Setup

$twig->addExtension(
    new \silent\Twig\ConstantResolverExtension\Extension()
);

About

Simple Twig plugin to resolve constant on template cache build

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%