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
我的数据库中有一个布尔变量(0,1),我希望将其过滤为单词0表示“否”,1表示“是”。我怎么能在树枝模板中做到这一点
I want something like {{ bool_var | '??' }}
where the '??' is the filter
我想要像{{bool_var | “?”那里的'??'是过滤器
3 个解决方案
#1
62
Quick way to achieve that is to use the ternary operator:
快速实现这一目标的方法是使用三元运算符:
{{ bool_var ? 'Yes':'No' }}
http://twig.sensiolabs.org/doc/templates.html#other-operators
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
您还可以创建一个自定义过滤器来执行此操作。阅读有关自定义TWIG扩展的信息 - http://symfony.com/doc/current/cookbook/templating/twig_extension.html
#2
2
To build on what @dmnptr said in his last paragraph, in your app bundle, create a /Twig
folder and create an AppExtension
class inside.
要构建@dmnptr在他的最后一段中所说的内容,在你的app包中创建一个/ Twig文件夹并在里面创建一个AppExtension类。
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:
然后,在bundle的Resources / config /文件夹中,将以下内容添加到services.yml中,其中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.
通过在任何变量上附加一个布尔值,过滤器将在Twig中可用。
#3
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.
- 无需弄乱twig或symfony配置。
- Can use it in forms themselves.
- 可以在表单中使用它。
Documentation: http://symfony.com/doc/current/cookbook/form/data_transformers.html
文档:http://symfony.com/doc/current/cookbook/form/data_transformers.html
Example from: Symfony2 Forms BooleanToStringTransformer Issue
示例来自: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;
}
}
#1
62
Quick way to achieve that is to use the ternary operator:
快速实现这一目标的方法是使用三元运算符:
{{ bool_var ? 'Yes':'No' }}
http://twig.sensiolabs.org/doc/templates.html#other-operators
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
您还可以创建一个自定义过滤器来执行此操作。阅读有关自定义TWIG扩展的信息 - http://symfony.com/doc/current/cookbook/templating/twig_extension.html
#2
2
To build on what @dmnptr said in his last paragraph, in your app bundle, create a /Twig
folder and create an AppExtension
class inside.
要构建@dmnptr在他的最后一段中所说的内容,在你的app包中创建一个/ Twig文件夹并在里面创建一个AppExtension类。
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:
然后,在bundle的Resources / config /文件夹中,将以下内容添加到services.yml中,其中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.
通过在任何变量上附加一个布尔值,过滤器将在Twig中可用。
#3
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.
- 无需弄乱twig或symfony配置。
- Can use it in forms themselves.
- 可以在表单中使用它。
Documentation: http://symfony.com/doc/current/cookbook/form/data_transformers.html
文档:http://symfony.com/doc/current/cookbook/form/data_transformers.html
Example from: Symfony2 Forms BooleanToStringTransformer Issue
示例来自: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;
}
}