如何在Fluid中分配变量?

5

我希望有一个可以在Fluid中赋值的视图助手,而不需要从控制器中传递变量。


我不太明白你想要什么? - Mihir Bhatt
2
看一下扩展名为vhs的东西。它提供了这样一个ViewHelper,以及许多其他功能。非常有用。 - Jost
3个回答

16

从 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')}

13

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

在Fluid中设置变量的问题在于可能会在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>

使用这个Typoscript。
lib.calc = TEXT
lib.calc.current = 1
lib.calc.prioriCalc = 1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接