@Marc and I looked at some ways of conditionally rendering things in Fusion and came to the conclusion that there is room for improvement.
Current ways
Neos.Fusion:Case
The classic Case
is probably the most used way of conditionally deciding which things should be rendered:
Neos.Fusion:Case {
foo {
condition = ${q(node).property('thing') === "foo"}
renderer = "oof"
}
bar {
condition = ${q(node).property('thing') === "bar"}
renderer = "rab"
}
default {
condition = true
renderer = null
}
}
The case implementation is not straight forward. Neither the fusion side nor the actual php implementation. Beginning with the incoherent name of case
which name contradicts its counterpart switch
in almost all programming languages. Also the explicitly set condition
and rendering
properties inside the implicit Neos.Fusion:Matcher
are not ideal.
Neos.Fusion:Match
For less complex and string-only conditions the Neos.Fusion:Match
can be used:
myValue = Neos.Fusion:Match {
@subject = 'hello'
@default = 'World?'
hello = 'Hello World'
bye = 'Goodbye world'
}
@if
Using @if
to “switch” what should be renderer is most common in AFX:
renderer = afx`
<a href="/logout" @if.isLoggedIn={user}>Logout</a>
<a href="/login" @if.isNotLoggedIn={!user}>Login</a>
`
Replacing: Neos.Fusion:Case
By using common naming like switch and case it is easier to understand what the parts do. Especially for newcomers.
The assignment of a property instead of the implicit Neos.Fusion:Matcher
makes the code slimmer and more inline with other commonly used prototypes while keeping the functionality of the Neos.Fusion:Case
.
Instead of using @if
the new @case
and the somewhat new @default
properties are used. @case
can be also be named to mimic multiple cases like in PHP where the condition is matched if any of the provided @case
s is true.
Also the default case is now predefined with the @default
property
Neos.Fusion:Switch {
someCondition = Neos.Fusion:Renderer {
@case = ${q(node).is('[instanceof MyNamespace:My.Special.SuperType]')}
type = 'MyNamespace:My.Special.Type'
}
otherCondition = Neos.Fusion:Value {
@position = 'start'
@case.isSpecial = ${q(documentNode).property('layout') == 'special'}
@case.isSomethinElse = ${q(documentNode).property('layout') == 'else'}
renderer = ${'<marquee>' + q(node).property('content') + '</marquee>'}
}
@default = Neos.Fusion:Renderer {
path = '/myPath'
}
}