2017 © Pedro Peláez
 

library twig-const-resolver

Simple Twig plugin to resolve constant on template cache build

image

silent/twig-const-resolver

Simple Twig plugin to resolve constant on template cache build

  • Wednesday, October 21, 2015
  • by silent
  • Repository
  • 2 Watchers
  • 1 Stars
  • 16,323 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 5 % Grown

The README.md

Twig Constant Resolver Extension

Packagist Travis Coveralls, (*1)

Simple Twig plugin to resolve constant on template cache build., (*2)

Why

For example we have something like this:, (*3)

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

Without this extension Twig will compile it in something like this:, (*4)

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., (*5)

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:, (*6)

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

Also in evaluation mode it can be compiled in something like this:, (*7)

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

Installation

The extension is installable via composer:, (*8)

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

Setup

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

The Versions

21/10 2015

dev-master

9999999-dev https://github.com/silentroach/twig-const-resolver

Simple Twig plugin to resolve constant on template cache build

  Sources   Download

MIT

The Requires

 

The Development Requires

templating twig extension constant

21/10 2015

1.1.1

1.1.1.0 https://github.com/silentroach/twig-const-resolver

Simple Twig plugin to resolve constant on template cache build

  Sources   Download

MIT

The Requires

 

The Development Requires

templating twig extension constant

08/09 2015

1.1.0

1.1.0.0

Simple Twig plugin to resolve constant on template cache build

  Sources   Download

The Requires

 

The Development Requires

08/09 2015

1.0.3

1.0.3.0

Simple Twig plugin to resolve constant on template cache build

  Sources   Download

The Requires

 

The Development Requires

07/09 2015

1.0.2

1.0.2.0

Simple Twig plugin to resolve constant on template cache build

  Sources   Download

The Requires

 

The Development Requires

07/09 2015

1.0.1

1.0.1.0

Simple Twig plugin to resolve constant on template cache build

  Sources   Download

The Requires

 

The Development Requires

07/09 2015

1.0.0

1.0.0.0

Simple Twig plugin to resolve constant on template cache build

  Sources   Download

The Requires

 

The Development Requires