如何检查typo3流体模板中的列内容?

问题描述 投票:0回答:5

我有一个typo3流体模板,我想在渲染某些元素之前检查内容是否存在。 有没有有效的方法来做到这一点? 例如。

<f:if condition="{contentInColPos0}">
  <div class="section-content">
    <f:render section="Main" />
  </div>
</f:if>

是否有内置变量或简单的方法来检查列位置中是否存在内容? 这是 CMS 模板中的常见任务(除非有东西要显示,否则不要渲染这部分),但我似乎找不到简单的方法。

typo3 fluid
5个回答
6
投票

没有简单的方法可以做到这一点。但是您可以使用一些 TypoScript,然后将计数传递给 Fluid 并在条件中使用它:

lib.countContent = CONTENT
lib.countContent {
   table = tt_content
   select {
     selectFields = count(uid) AS count
     pidInList = this
     andWhere = (deleted = 0 AND hidden = 0)
   }

   renderObj = COA
   renderObj {
     10 = TEXT
     10 {
       data = field:count
     }
}

该对象将输出给定页面上的内容行数,并且可以在 Fluid 中访问:

<f:if condition="{f:cObject(typoscriptObjectPath:'lib.countContent')} > 0">
  Then show some stuff
</f:if>

如果您无论如何都要使用内容并且内容对象中没有全局换行,您也可以直接使用它,因为 Fluid IfViewHelper 会检查空字符串。所以例如这可能效果更好:

lib.content < styles.content.get

(没有内容则该对象为空)

<f:if condition="{f:cObject(typoscriptObjectPath:'lib.content')}">
  <f:then>
    <f:format.html>{lib.content}</f:format.html>
  </f:then>
  <f:else>
    No content found
  </f:else>
</f:if>   

3
投票

使用 VHS 和 不使用 TypoScript 来检查这一点的最简单方法

<f:if condition="{v:content.get(column:6) -> v:iterator.first()}">
    <div class="myAmazingClass">
    </div>
</f:if>

如果需要,您可以在子页面中滑动内容:

<f:if condition="{v:content.get(column:6, slide:'-1') -> v:iterator.first()}">
    <div class="myAmazingClass">
    </div>
</f:if>

1
投票

考虑使用 ViewHelpers 的 VHS https://fluidtypo3.org/viewhelpers/vhs/master/Content/GetViewHelper.htmlhttps://fluidtypo3.org/viewhelpers/vhs/master/Content/RenderViewHelper.html 与使用

as
参数和
f:if
条件来检查分配的变量是否为空。


0
投票

你可以轻松解决这个问题:

  1. 在您的 TypoScript 文件中:

    lib.contentInColPos0 < styles.content.get
    lib.contentInColPos0 t.select.where = colPos = 0
    
  2. 在您的模板文件中:

    <f:if condition="{f:cObject(typoscriptObjectPath:'lib.contentInColPos0')}">
      <div class="section-content">
        <f:render section="Main" />
      </div>
    </f:if>
    

0
投票

十年(!)之后,现在很容易做到:

首先,在流体模板设置打字稿中声明一个变量:

page.10 = FLUIDTEMPLATE
page.10 {
    # ...
    variables {
        hero < styles.content.get
        hero.select.where = {#colPos}=200
    }
}

然后,在您各自的流体模板中,只需使用

<f:if condition="{hero}">
    <f:then>
        {hero -> f:format.raw()}
    </f:then>
    <f:else>
        // whatever
    </f:else>
</f:if>
© www.soinside.com 2019 - 2024. All rights reserved.