本文介绍了如何确定键是否是对象(树枝)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想确定我的密钥是否是一个对象:
I want to determine if my key is an object:
{% for key in columns %}
{% if key is object %}
This is an object
{% else %}
This in not an object
{% endif %}
{% endfor %}
但我收到错误消息:
未知的对象"测试.
推荐答案
您可以创建自己的 Twig 扩展.我看到您已经用 Symfony 标记了您的问题,因此假设您在 Symfony 中使用 Twig,您可以按照本教程进行操作:
You can create your own Twig extension. I see you've tagged your question with Symfony so assuming you use Twig in Symfony, you can follow this tutorial:
https://symfony.com/doc/3.4/templating/twig_extension.html
你需要做的是基于这个例子添加新的TwigTest
:
What you need to do is add new TwigTest
based on this example:
https://twig.symfony.com/doc/2.x/advanced.html#tests
你应该得到这样的结果:
You should end up with something like this:
// src/AppBundle/Twig/AppExtension.php
namespace AppBundle\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigTest;
class AppExtension extends AbstractExtension
{
public function getTests()
{
return array(
new TwigTest('object', array($this, 'isObject')),
);
}
public function isObject($object)
{
return is_object($object);
}
}
上面的代码没有经过测试,但应该可以正常工作.
Code above is not tested, but should work fine.
这篇关于如何确定键是否是对象(树枝)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!