gpt4 book ai didi

php - 检查方法是否存在或可以在 Twig 中调用

转载 作者:行者123 更新时间:2023-12-05 01:02:58 26 4
gpt4 key购买 nike

如果在 Twig 内部有一个对象,我想测试该对象是否可以在该对象上调用名为 getName 的方法,是否存在这种情况?

我尝试执行以下操作但没有成功:

 {% if lastCategory.method(getName) is defined  %}

最佳答案

是的,你可以这样做:

{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}

在你的情况下,它会是这样的
{{ attribute(lastCategory, 'getName') is defined ? 'Method exists' : 'Method does not exist' }}

documentation

关于php - 检查方法是否存在或可以在 Twig 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25408897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com