5085

How to access a method and pass an argument within the template?

Question:

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(); }

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?

Answer1:

Additionally to <strong>sretuer's</strong> answer, I'll only mention that you can create VH which will display block conditionally like:

File 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>

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.

Answer2:

You can only access properties via Getter from Fluid with no parameters, but you can implement an own ViewHelper to check that. As parameters you can use your Product and the Category. Then you can call your ViewHelper from Fluid this way:

<vh:checkOffset product="{product}" category="{category}" />

or inline

{vh:checkOffset(product: product, category: category)}

Within your ViewHelper you can check this in the way you've done it in your Controller:

public function render($product, $category){ return ($product->getCategory()->offsetExists($category)); }

Answer3:

You may consider <a href="https://fluidtypo3.org/viewhelpers/vhs/master/Condition/Iterator/ContainsViewHelper.html" rel="nofollow">https://fluidtypo3.org/viewhelpers/vhs/master/Condition/Iterator/ContainsViewHelper.html</a> which is designed for creating conditions in Fluid that check if an array or Iterator contains another object and works exactly like f:if regarding then and else arguments and f:then and f:else child nodes.

Recommend

  • Image is not moving completely to the left
  • S4s-elt-invalid-content.1: Element 'element' Is Invalid, Misplaced, Or Occurs Too Often
  • Capture Scroll on Overflow:Hidden elements
  • C#, Updating a Progress Bar Using Background Worker From a Different Class
  • How to reduce the time delay to reach run method of Runnable class using ExecutorService Java
  • How to select all in PrimeFaces TreeTable?
  • Persistent stty Settings?
  • Tools for understanding HTML layout
  • Responsive left sidebar open close
  • Prevent page break in text block with iText, XMLWorker
  • C++ friend class std::vector
  • SyntaxError: (irb):26: both block arg and actual block given
  • hide missing dates from x-axis ggplot2
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • Thread safety of a fluent like class using clone() and non final fields
  • ActionScript 2 vs ActionScript 3 performance
  • To display the title for the current loaction in map in iphone
  • ORA-29908: missing primary invocation for ancillary operator
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • Traverse Array and Display in markup
  • Comma separated Values
  • How to disable jQuery.jplayer autoplay?
  • WPF Applying a trigger on binding failure
  • Error creating VM instance in Google Compute Engine
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Java static initializers and reflection
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal