Search code examples
typo3fluidtypo3-9.x

Condition within FLUID to check if column has content


I'm working with the sitepackage by Ben Kott for Typo3 9.5 and include content into my fluid template like this:

<f:cObject typoscriptObjectPath="lib.dynamicContent" data="{colPos: '1'}" />

I'm tryin to wrap this into a condition withing fluid like

<f:if condition="<f:cObject typoscriptObjectPath='lib.dynamicContent' data='{colPos: \'1\'}'">     
    whatever  
</f:if>

it does not work though. I can not tell if there's anything wrong syntax or if it's not possible.


Solution

  • {f:cObject(typoscriptObjectPath: 'lib.dynamicContent', data: {colPos: 1}) -> f:variable(name: 'content')}
    <f:if condition="{content}">
       There is content. Here it is:
       {content -> f:format.raw()}
    </f:if>
    
    1. Avoids double rendering of the typoscript object, double DB requests etc.
    2. Avoids tag syntax inside tag attributes which is probably going to be impossible to do in future Fluid versions

    Edit for posterity: the exact reason why the code above failed seems to be a syntax error:

    <f:if condition="<f:cObject typoscriptObjectPath='lib.dynamicContent' data='{colPos: \'1\'}'"> 
    

    Should be:

    <f:if condition="<f:cObject typoscriptObjectPath='lib.dynamicContent' data='{colPos: \'1\'}' />">
    

    Since the inner tag was not closed. You should still avoid it though - use inline syntax instead. In the code I put above you can remove the -> f:variable() part and the expression can then be used as tag attribute value.