我希望有一个可以在Fluid中赋值的视图助手,而不需要从控制器中传递变量。
从 TYPO3 库安装名为 vhs 的扩展
在 Fluid 模板顶部定义如下的命名空间
{namespace v=FluidTYPO3\Vhs\ViewHelpers}
然后使用set viewhelper
<v:variable.set name="test" value="12345" />
Value of test : {test}
{测试}将返回值12345
用于注册全局变量
<v:variable.register.set name="test" value="12345"/>]
获取全局变量的值
Value of global variable : <v:variable.register.get name="test">
自 TYPO3 8.7 版本起,Fluid 引入了变量的视图辅助器(无需使用 VHS)。<f:variable name="myvariable">My variable's content</f:variable>
<f:variable name="myvariable" value="My variable's content"/>
使用内联样式
{f:variable(name: 'myvariable', value: 'My variable\'s content')}
{myoriginalvariable -> f:variable.set(name: 'mynewvariable')}
自 TYPO3 8.6 版本开始,无需安装“vhs”扩展即可实现此功能:
<f:variable name="myvariable" value="My variable's content"/>
f:alias
,它使您能够在此VH范围内定义新变量。这就是与ext:vhs的variable.set
VH的区别。<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>
<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>
lib.calc = TEXT
lib.calc.current = 1
lib.calc.prioriCalc = 1
vhs
的东西。它提供了这样一个ViewHelper,以及许多其他功能。非常有用。 - Jost