I want viewhelper that can be helpful to assign variable in fluid, I dont want variable to be passed from controller.
How to assign variable in fluid?
23.7k views Asked by Vishal Tanna At
3
There are 3 answers
0
On
Since first fluid version it is possible to define variables for a special area: there is the VH f:alias
which enables you to define new variables in the range of this VH. And that is the difference to the variable.set
VH from ext:vhs.
<f:alias map="{firstName: 'John', lastName: 'Doe'}">
<p>Hello, my name is {firstName} {lastName}</p>
</f:alias>
<f:for each="{users}" as="user" iteration="iterator">
<f:if condition="{iterator.isFirst}">
<v:variable.set name="firstName">{user.firstName}</v:variable.set>
<v:variable.set name="lastName">{user.lastName}</v:variable.set>
</f:if>
:
do other output
:
</f:for>
<p>the first user was {firstName} {lastName}.</p>
The problem with setting variables inside the fluid is the possibility to do programming logic inside fluid:
<v:variable.set name="counter" value="0">
<f:for each="records" as="record">
<f:if condition="{record.flag}">
<v:variable.set name="counter">
<f:cObject typoscriptObjectPath="lib.calc">
{counter}+1
</f:cObject>
</v:variable.set>
</f:if>
</f:for>
<p>there are {counter} records with a flag set.</p>
with this typoscript
lib.calc = TEXT
lib.calc.current = 1
lib.calc.prioriCalc = 1
Define namespace like following at the top of your fluid template
Then use set viewhelper
For registering global variable
Get value of global variable
With inline style usage