Render single fusion object

Hello everyone,

I am currently working on a JSON API so my PHP Controller is using the JsonView. As one part of the JSON I need to return the rendered HTML of a Fusion Object. Unfortunately I haven’t found a solution to really render only a single Fusion Object.

Can you point to an example how to render a single Fusion objects regardless of a site node?

Prior I would have use Fluid but I really want to get rid of it in Neos CMS.

Thanks in advance & best regards!

so you dont want to assign that object first to a path but render it directly?

There is an internal syntax for it - with it you can access the premium features of fusion :wink:

$view->setFusionPath('<Vendor.Site:MyPrototype>');

do you mean this? Or do you want to know how to instantiate a fusion view?

Thank you for answering, Marc!
Your code snippet assumes that I am using the FusionView::class. My whole controller is using JsonView::class and therefore no configured fusion view is available. Is it possible to render the fusion objects in a limited scope of an method? :slight_smile:

Its more like “render me this fusion object (which is not doing any special stuff and just place variables in a HTML structure) and return the HTML so I can use it further on in my PHP controller”?

ah i see, sure :slight_smile:

for that you have to create another \Neos\Fusion\View\FusionView where you have to set the fusion files manually to be included. This can be done like this: (but you can also look into the $supportedOptions variable and read into it yourself ;))

$view = new FusionView();
$view->setControllerContext($controllerContext);

$view->setFusionPathPatterns([
    'resource://Neos.Fusion/Private/Fusion/Root.fusion',
    'resource://Your.Vendor/Private/Fusion/Root.fusion'
]);

$view->setFusionPath('<Your.Vendor:MyFusionObjectToRender>');
$view->assign('anyValueIfNeeded', 1234);
$result = $view->render();

If youre also interested in creating a view for testing context you just have to add this part:

$controllerContext = $this->getMockBuilder(ControllerContext::class)->disableOriginalConstructor()->getMock();

$view->setPackageKey('Any.Thing'); // dummy not needed in 9.0 anymore
1 Like

Thank you!

I needed to add this in fusion:

myFusionObjectToRender = Your.Vendor:MyFusionObjectToRender

Afterwards I got it working by calling it this way:

$view->setFusionPath('myFusionObjectToRender');

Unfortunately I have to set a link to a node now but the FusionView is missing a documentNode to refer to. All <a>-Tags are generated but with an empty href-attribute.

Do you or someone else know how to properly generate a documentNode so the FusionView is able to generate links?

Found it!

As my default view is a JsonView, Neos needs to know it should resolute the link for the html view:

link = Neos.Neos:NodeUri {
        node = ${node}
        format = 'html'
}

Also, if somebody runs in the same error: Make sure you are using the correct context (contentContext != context).

2 Likes