anyavacy
anyavacy

Reputation: 1707

filter boolean variable in a twig template

I have a boolean variable(0, 1) in my database and I want to filter it to a word 0 for 'NO', and 1 for 'Yes'. how can I do that in a twig template

I want something like {{ bool_var | '??' }} where the '??' is the filter

Upvotes: 36

Views: 35225

Answers (3)

bassplayer7
bassplayer7

Reputation: 934

To build on what @dmnptr said in his last paragraph, in your app bundle, create a /Twig folder and create an AppExtension class inside.

class AppExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('boolean', array($this, 'booleanFilter')),
        );
    }

    public function booleanFilter($value)
    {
        if ($value) {
            return "Yes";
        } else {
            return "No";
        }
    }

    public function getName()
    {
        return 'app_extension';
    }
}

Then, in your bundle's Resources/config/ folder, add the following to your services.yml where class is the class of the new class:

app.twig_extension:
    class: [YourAppBundleNamespace]\Twig\AppExtension
    public: false
    tags:
        - { name: twig.extension }

The filter will be available in Twig by simply appending a |boolean to any variable.

Upvotes: 4

Tek
Tek

Reputation: 3070

Or even better you could make a boolean to string transformer and add it to your form.

It might be 'more' code but the upside is reusability. You wouldn't have to make your templates dirty with logic and you could reuse it to all the forms you want :)

Pros:

  • Not tied to the form component so you can still use it.
  • Use it anywhere, more functionality than a twig extension.
  • No need to mess with twig or symfony configuration.
  • Can use it in forms themselves.

Documentation: http://symfony.com/doc/current/cookbook/form/data_transformers.html

Example from: Symfony2 Forms BooleanToStringTransformer Issue

<?php

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class BooleanToStringTransformer implements DataTransformerInterface
{
    private $trueValue;
    private $falseValue;

    public function __construct($trueValue, $falseValue)
    {
        $this->trueValue = $trueValue;
        $this->falseValue = $falseValue;
    }

    public function transform($value)
    {
        if (null === $value) {
             return null;
        }

        if (!is_bool($value)) {
            throw new TransformationFailedException('Expected a Boolean.');
        }

        return true === $value ? $this->trueValue : $this->falseValue;
    }

    public function reverseTransform($value)
    {
        if (null === $value) {
            return null;
        }

        if (!is_string($value)) {
            throw new TransformationFailedException('Expected a string.');
        }

        return $this->trueValue === $value;
    }
}

Upvotes: 1

dmnptr
dmnptr

Reputation: 4304

Quick way to achieve that is to use the ternary operator:

{{ bool_var ? 'Yes':'No' }}

http://twig.sensiolabs.org/doc/templates.html#other-operators

You could also create a custom filter that would do this. Read about custom TWIG extensions - http://symfony.com/doc/current/cookbook/templating/twig_extension.html

Upvotes: 85

Related Questions