Krz*_*zos 39
终于找到了一些东西.其中一个答案来自:https: //craftcms.stackexchange.com/questions/932/how-to-check-variable-type
{# Match integer #}
{% if var matches '/^\\d+$/' %}
{% endif %}
{# Match floating point number #}
{% if var matches '/^[-+]?[0-9]*\\.?[0-9]+$/' %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
您可以创建一个树枝扩展来添加测试“数字”
有了这个,你就可以写:
{% if foo is numeric %}...{% endif %}
Run Code Online (Sandbox Code Playgroud)
创建您的扩展类:
namespace MyNamespace;
class MyTwigExtension extends \Twig_Extension
{
public function getName()
{
return 'my_twig_extension';
}
public function getTests()
{
return [
new \Twig_Test('numeric', function ($value) { return is_numeric($value); }),
];
}
}
Run Code Online (Sandbox Code Playgroud)
在您的配置中:
services:
my_twig_extension:
autowire: true
class: AppBundle\MyNamespace\MyTwigExtension
tags:
- { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)
请参阅文档:
https://twig.symfony.com/doc/2.x/advanced.html#tests
https://symfony.com/doc/current/templating/twig_extension.html
归档时间: |
|
查看次数: |
20351 次 |
最近记录: |