如何在流体中分配变量?

How to assign variable in fluid?

我想要可以帮助在流体中分配变量的 viewhelper,我不希望从控制器传递变量。

Install extension called vhs from TYPO3 repository

在流体模板的顶部定义命名空间,如下所示

{namespace v=FluidTYPO3\Vhs\ViewHelpers}

然后使用set viewhelper

<v:variable.set name="test" value="12345" />
Value of test : {test}

{test} will return value 12345

用于注册全局变量

<v:variable.register.set name="test" value="12345"/>]

获取全局变量的值

Value of global variable : <v:variable.register.get name="test">

Since TYPO3 8.7, fluid introduces viewhelper for the variable (No need of 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"/>

https://docs.typo3.org/typo3cms/extensions/core/Changelog/8.6/Feature-79402-VariableViewHelperForFluid.html

从第一个流体版本开始,可以为特殊区域定义变量:有 VH 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