Programmatic ViewHelpers for FLOW3

In the current version of FLOW3 (1.0.0 alpha7), there is no way to create Fluid ViewHelpers programmatically. But there is a workaround which is not that bad either. I got the inspiration from the admin package, so all kudos goes to the creator ;)

First off, why would you want something like this? I wanted to accomplish the following: I have a page entity which contains several page items. Every page item shares the same interface, e.g. all have a display method. Of course I don’t want to generate HTML code directly in there, but have Fluid ViewHelpers based on the properties of the page item.

The soultion is rather simple: Instead of generating Fluid ViewHelpers in the code, render a view! So for every page item, create a Fluid template, and the display method of the page item makes sure all necessary view variables are set.

Here is the code needed in the page item’s display method:

$view = $objectFactory->create('F3\Fluid\View\TemplateView');
$view->setControllerContext($controllerContext);
$view->setTemplatePathAndFilename('package:// »
Your-Package-Name/Private/Templates/PageItemName.html');
$view->assign('varName', $var);
return $view->render();

That’s it. Of course, you need to make sure you have an objectFactory at that place, as well as the controllerContext which you can get from the current controller via getControllerContext().

filed under , posted on March 6, 2010

One Response to “Programmatic ViewHelpers for FLOW3”

  1. flow3 says:

    [...] (HQ) Share and Enjoy: These icons link to social bookmarking sites where readers can share …michaelsauter.net blog Programmatic ViewHelpers for FLOW3In the current version of FLOW3 (1.0.0 alpha7), there is no way to create Fluid ViewHelpers [...]