简体   繁体   中英

How do I check for column content in a typo3 fluid template?

I have a typo3 fluid template in which I want to check if content exists before rendering some elements. Is there an efficient way to do this? eg.

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

Is there a built-in variable or a simple way to check content exists in a column position? This is a common task in CMS templating (don't render this part unless there's something to show), but I can't seem to find how to do it simply.

There is no easy way to do that. But you can use some TypoScript and then pass the count to Fluid and use it in a condition:

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
     }
}

This object will output the number of content rows on the given page and can be accessed in Fluid:

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

If you're going to use the content anyway and don't have global wrap in your content object, you can also use it directly because the Fluid IfViewHelper checks for empty strings. So eg this might be working even better:

lib.content < styles.content.get

(This object is empty if there is no content)

<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>   

Easyest way to check this with VHS and without TypoScript :

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

You can slide the content throug subpages if you want:

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

考虑将VHS与ViewHelpers https://fluidtypo3.org/viewhelpers/vhs/master/Content/GetViewHelper.htmlhttps://fluidtypo3.org/viewhelpers/vhs/master/Content/RenderViewHelper.html结合使用,并使用as参数和一个f:if条件,用于检查分配的变量是否为空。

You can solve this easily:

  1. In your TypoScript file:

     lib.contentInColPos0 < styles.content.get lib.contentInColPos0 t.select.where = colPos = 0 
  2. In your Template file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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