David
David

Reputation: 23

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.

Upvotes: 2

Views: 6903

Answers (5)

mtness
mtness

Reputation: 1024

Ten years(!) later, it is now easily doable:

first, declare a variable in your fluid template setup typoscript:

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

then, in your respective fluid template, just use

<f:if condition="{hero}">
    <f:then>
        {hero -> f:format.raw()}
    </f:then>
    <f:else>
        // whatever
    </f:else>
</f:if>

Upvotes: 0

Naderio
Naderio

Reputation: 1419

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>

Upvotes: 3

Claus Due
Claus Due

Reputation: 4271

Consider VHS with ViewHelpers https://fluidtypo3.org/viewhelpers/vhs/master/Content/GetViewHelper.html or https://fluidtypo3.org/viewhelpers/vhs/master/Content/RenderViewHelper.html combined with using the as argument and an f:if condition to check the assigned variable for being empty.

Upvotes: 1

eduludi
eduludi

Reputation: 1693

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>
    

Upvotes: 0

lorenz
lorenz

Reputation: 4558

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>   

Upvotes: 6

Related Questions