如何访问方法并在模板内传递参数? [英] How to access a method and pass an argument within the template?
问题描述
在我的模板中,我想检查一个实体是否与另一个实体有关系.意味着一个对象在另一个对象的附加对象存储中.
In my template I want check whether an entity has a relation to another one. Meaning one object is in an attached Object Storage of another one.
在控制器中,我可以简单地调用:
In the controller I can simply call:
if ($product->getCategory()->offsetExists($category) {
print 'In category ' . $category->getName();
}
但是我无法在模板中找出正确的语法.我尝试了那些没有运气的人(每次都评估为true
):
But I can't figure out the correct syntax in the template. I tried those without luck (both evaluate to true
everytime):
<f:if condition="{product.category.offsetExists(category)}">true</f:if>
<f:if condition="{product.category.offsetExists({category})}">true</f:if>
这甚至可以在模板中实现吗?
Is this even possible within the template?
推荐答案
除了 sretuer的答案外,我只提到您可以创建VH,该VH将有条件地显示块,例如:
Additionally to sretuer's answer, I'll only mention that you can create VH which will display block conditionally like:
文件typo3conf/ext/your_ext/ViewHelpers/CheckOffsetViewHelper.php
<?php
namespace VENDORNAME\YourExt\ViewHelpers;
class CheckOffsetViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
public function render() {
return ($product->getCategory()->offsetExists($category))
? $this->renderChildren()
: '';
}
}
?>
因此您可以在视图中使用它:
So you can use it in the view:
{namespace vh=VENDORNAME\YourExt\ViewHelpers}
<vh:checkOffset product="{product}" category="{category}" >
Display this only if product is in category
</vh:checkOffset>
当然,您需要根据扩展名来修复VENDORNAME
和YourExt
,可以在每个控制器,模型,存储库等的开头找到该文件.
Of course you need to fix VENDORNAME
and YourExt
according to your extension, can be found at the beginning of every controller, model, repository etc.
这篇关于如何访问方法并在模板内传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!