嵌套的 if / else 语句 ext:news 流畅

Nested if / else statements ext:news fluid

使用 TYPO3 v7.6.13 ext:news 5.3.2

我创建了两个用于单个页面的细节部分,这是页面 TS 配置:

tx_news.templateLayouts {
        11 = HomePage Top Banners
        12 = HomePage List
}

我可以写两个条件,以便使用适当的模板。

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
    xmlns:n="http://typo3.org/ns/GeorgRinger/News/ViewHelpers"
    data-namespace-typo3-fluid="true">
<f:layout name="General" />
<!--
  =====================
    Templates/News/List.html
-->

<f:section name="content">
  <!--TYPO3SEARCH_end-->
  <f:if condition="{news}">


<f:then>
        <f:if condition="{settings.templateLayout} == 11">
            <f:then>
                    <f:for each="{news}" as="newsItem">
                       <f:render partial="List/homepagetopbanner" arguments="{newsItem: newsItem, settings:settings}"/>
                    </f:for>
            </f:then>
</f:if>


  <f:if condition="{settings.templateLayout} == 12">
    <f:then>
       <f:for each="{news}" as="newsItem">
        <f:render partial="List/homepagelist" arguments="{newsItem: newsItem, settings:settings}"/>
       </f:for>
    </f:then>
    </f:if>

</f:then>



    <f:else>
      <div class="no-news-found">
        <f:translate key="list_nonewsfound" />
      </div>
    </f:else>
  </f:if>
  <!--TYPO3SEARCH_begin-->
</f:section>
</html>

我正在努力设置回退选项,以便在未选择任何其他选项时使用默认的详细信息。

据我了解,在这种情况下您无法编写 else if 语句,是否可以设置回退选项?

Sure you can use else like in the script below:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
    xmlns:n="http://typo3.org/ns/GeorgRinger/News/ViewHelpers"
    data-namespace-typo3-fluid="true">
<f:layout name="General" />
<!--
  =====================
    Templates/News/List.html
-->

<f:section name="content">
  <!--TYPO3SEARCH_end-->
  <f:if condition="{news}">
      <f:then>
          <f:if condition="{settings.templateLayout} == 11">
              <f:then>
                <f:for each="{news}" as="newsItem">
                   <f:render partial="List/homepagetopbanner" arguments="{newsItem: newsItem, settings:settings}"/>
                </f:for>
              </f:then>
          </f:if>
          <f:if condition="{settings.templateLayout} == 12">
            <f:then>
               <f:for each="{news}" as="newsItem">
                <f:render partial="List/homepagelist" arguments="{newsItem: newsItem, settings:settings}"/>
               </f:for>
            </f:then>
            <f:else>
               <f:for each="{news}" as="newsItem">
                <f:render partial="List/default" arguments="{newsItem: newsItem, settings:settings}"/>
               </f:for>
            </f:else>
          </f:if>
      </f:then>
      <f:else>
        <div class="no-news-found">
          <f:translate key="list_nonewsfound" />
        </div>
      </f:else>
  </f:if>
  <!--TYPO3SEARCH_begin-->
</f:section>
</html>

or you can use it with the default template layout which is 0 like this

<f:else condition="{settings.templateLayout} == 0">
  Add your code here ..
</f:else>

elseif 将 be/is 仅在 TYPO3 8 中可用。

到那时你需要级联 if VHs in else VHs:

<f:if condition="{settings.templateLayout} == 11">
<f:then>
    // do template 11
</f:then>
<f:else>
    <f:if condition="{settings.templateLayout} == 12">
    <f:then>
        // do template 12
    </f:then>
    <f:else>
        // do default-template
    </f:else>
    </f:if>
</f:else>
</f:if>

或者您使用 switch viewhelper:

<f:switch expression="{settings.templateLayout}">
<f:case value="11">
    // do template 11
</f:case>
<f:case value="12">
    // do template 12
</f:case>
<f:case default="TRUE">
    // do default template
</f:case>
</f:switch>

但是那个 viewhelper 有一些小缺点,所以我更喜欢级联 if VHs。

猫王,非常感谢你。

我在使用您提供的模板时遇到了一些奇怪的行为。

当我在前端选择模板11时,id为11的模板和默认模板都渲染了每条新闻。

但是,我使用了下面的代码,它工作正常。我删除了 else 条件,而是只使用三个 if 条件:11,12 或 default(0).

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
    xmlns:n="http://typo3.org/ns/GeorgRinger/News/ViewHelpers"
    data-namespace-typo3-fluid="true">
<f:layout name="General" />
<!--
  =====================
    Templates/News/List.html
-->

<f:section name="content">
  <!--TYPO3SEARCH_end-->
  <f:if condition="{news}">
      <f:then>
          <f:if condition="{settings.templateLayout} == 11">
              <f:then>
                <f:for each="{news}" as="newsItem">
                   <f:render partial="List/homepagetopbanner" arguments="{newsItem: newsItem, settings:settings}"/>
                </f:for>
              </f:then>
          </f:if>
          <f:if condition="{settings.templateLayout} == 12">
            <f:then>
               <f:for each="{news}" as="newsItem">
                <f:render partial="List/homepagelist" arguments="{newsItem: newsItem, settings:settings}"/>
               </f:for>
            </f:then>
          </f:if>
      </f:then>
     <!-- THIS IS THE DEFAULT -->
     <f:if condition="{settings.templateLayout} == 0">
               <f:for each="{news}" as="newsItem">
                <f:render partial="List/Item" arguments="{newsItem: newsItem, settings:settings}"/>
               </f:for>
     </f:if>

      <f:else>
        <div class="no-news-found">
          <f:translate key="list_nonewsfound" />
        </div>
      </f:else>
  </f:if>
  <!--TYPO3SEARCH_begin-->
</f:section>
</html>

希望这是有道理的,

非常感谢您的帮助。