Search code examples
typo3fluid

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.


Solution

  • 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 e.g. 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>