简体   繁体   English

TYPO3流体:如何在外部使用条件和变量 <f:section> 使布局动态化?

[英]TYPO3 Fluid: How is it possible to use conditions and variables outside of <f:section> to make the layout dynamic?

I try to change the layout which wraps each content-element by the definition from the backend. 我尝试更改布局,该布局通过后端的定义包​​装每个内容元素。 Unfortunately it seems that the variable for {data} is ONLY available inside of the <f:section> element. 不幸的是,似乎{data}的变量仅在<f:section>元素内部可用。 Even more problematic, the <f:if> condition ViewHelper has NO effect outsite of <f:section> , both <f:layout> statements are executed and only the last one is applied for rendering. 甚至更成问题的是,条件<f:section> Help <f:section> View <f:if><f:if>条件在<f:section>没有任何影响,这两个<f:layout>语句均被执行,仅最后一个语句被渲染。 That's not useful... and the documentation does talk only about ViewHelper inside of the Section. 没用……并且文档仅在本节中谈论ViewHelper。 Do you know any other way to achieve this? 您知道实现此目的的其他方法吗? Thanks in advance! 提前致谢!

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">

<f:if condition="{data.layout} == 101"><!-- if-condition has no effect here -->
  <f:then>
    <f:layout name="FullWidth" />
  </f:then>
  <f:else>
    <f:layout name="Default" />
  </f:else>
</f:if>

<f:debug>TEST1: {data.layout}</f:debug><!-- THIS DOES NOT SHOW UP AT ALL! -->

<f:section name="Main">
  <f:debug>TEST2: {data.layout}</f:debug><!-- Output is TEST2: 101 (integer) -->
  [...]
</f:section>

</html>

The f:layout tag is read without rendering the template, so an if statement around it will not work. 在不渲染模板的情况下读取了f:layout标记,因此围绕它的if语句将不起作用。 However, the name of the f:layout tag is rendered as a template, so you can use an inline if statement, like this: 但是, f:layout标记的名称呈现为模板,因此可以使用内联if语句,如下所示:

<f:layout name="{f:if(condition: '{data.layout} == 101', then: 'FullWidth', else: 'Default')}" />

Try moving the conditions into your TS, and use the settings for your layout name. 尝试将条件移到TS中,然后使用布局名称的设置。

TypoScript Typo脚本

lib.layout = TEXT
lib.layout.value = Page
[globalVar = GP:print = 1]
lib.layout.value = Print
[global]

Template 模板

<f:layout name="{f:cObject(typoscriptObjectPath: 'lib.layout')}" />

This is how I did for the purpose. 这就是我为此目的所做的。

Another option is to use the value of your data.layout to build the name of the layout. 另一个选择是使用data.layout的值来构建布局的名称。 in this way it is similar to the inline f:if-viewhelper. 这样,它类似于嵌入式f:if-viewhelper。

<f:layout name="layout{data.layout}" />

make sure to handle unknown or empty values. 确保处理未知或空值。 Either with files with matching names or preparing a variable in typoscript with .ifEmpty = default or similar. 使用名称匹配的文件,或者使用.ifEmpty = default或类似内容在错字中准备一个变量。

my normal solution for deciding the layout (I use the two fields backend_layout and backend_layout_next_level as they give the options of inheritance and individual override) : 我确定布局的常规解决方案(我使用两个字段backend_layoutbackend_layout_next_level因为它们提供了继承和单独覆盖的选项):

    templateName = TEXT
    templateName.cObject = CASE
    templateName.cObject {
        key.data = levelfield:-1, backend_layout_next_level, slide
        key.override.field = backend_layout

        #Default Template
        default = TEXT
        default.value = Unterseite

        pagets__startseite = TEXT
        pagets__startseite.value = Startseite

        pagets__katalogseite = TEXT
        pagets__katalogseite.value = Katalogseite

    }

A further way is to evaluate the layout inside the layout-file. 另一种方法是评估布局文件中的布局。 depending on the layout value you can render specific sections or partials which build the complete template. 根据布局值,您可以渲染构建完整模板的特定部分或部分。
Here you have the option to use the layout value inline to decide the section/ partial, too. 在这里,您还可以选择使用内联布局值来确定截面/局部。

<f:render partial="page-{page.layout}" />
<f:render section="section-{page.layout} />

Or you use f:switch or f:if -VHs. 或者您使用f:switchf:if -VHs。


as always: 一如既往:
which solution is the best? 哪种解决方案是最好的? it depends. 这取决于。

you need to consider your special requirements to get the best solution. 您需要考虑自己的特殊要求以获得最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM