35

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

anyavacy
  • 1,618
  • 5
  • 21
  • 43

3 Answers3

84

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

dmnptr
  • 4,258
  • 1
  • 20
  • 19
4

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.

bassplayer7
  • 924
  • 1
  • 13
  • 38
  • 1
    Thanks, this was a big help to me. Couple quick fixes though. The "services.yml" file may be in /app/config instead of Resources/config. Also, be sure to give the AppExtension file a namespace declaration at the top. – Thornview Jul 25 '16 at 15:23
1

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;
    }
}
Community
  • 1
  • 1
Tek
  • 2,888
  • 5
  • 45
  • 73
  • Forms were not mentioned in the original question. If you want reusability, you should create TWIG extension. – dmnptr Jul 11 '14 at 21:59
  • You got me there, but if he is it's another awesome suggestion. :D And another point is that he can actually still use the transformer since it's not coupled to the form component in any way. :D – Tek Jul 11 '14 at 22:00