Bradley
Bradley

Reputation: 109

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.

Upvotes: 0

Views: 3421

Answers (2)

Josef Glatz
Josef Glatz

Reputation: 565

Another solution would be a dedicated TypoScript object which you can use in Fluid if conditions

################################################
#### COUNT CONTENT LIB FOR USAGE IN FLUID ####
################################################
#
#  EXAMPLE: amount of content elements in colPos 1 of actual PID
#  ---------------
#  <f:cObject typoscriptObjectPath="lib.countContent" data="{colPos: 1}" />
#  {f:cObject(typoscriptObjectPath: 'lib.countContent', data: '{colPos: 1}')}
#
#  EXAMPLE: amount of content elements in more than one colPos of actual PID
#  ---------------
#  <f:cObject typoscriptObjectPath="lib.countContent" data="{colPos: '1,2'}" />
#  {f:cObject(typoscriptObjectPath: 'lib.countContent', data: '{colPos: \'1,2\'}')}
#
#
#
#
#  Usage examples:
#  --------------
#
#  <f:if condition="{f:cObject(typoscriptObjectPath: 'lib.countContent', data: '{colPos: 1}')}">
#      <f:cObject typoscriptObjectPath="lib.dynamicContent" data="{pageUid: '{data.uid}', colPos: '1', wrap: '<aside class=\"l-aside\">|</aside>'}" />
#  </f:if>
#
#
#  <f:if condition="{f:cObject(typoscriptObjectPath: 'lib.countContent', data: '{colPos: 1}')}">
#      <aside class="l-aside">
#         <f:cObject typoscriptObjectPath="lib.dynamicContent" data="{colPos: '1'}" />
#      </aside>
#  </f:if>
#
#
###############
lib.countContent = COA
lib.countContent {
    5 = LOAD_REGISTER
    5 {
        colPos.cObject = TEXT
        colPos.cObject {
            field = colPos
            ifEmpty.cObject = TEXT
            ifEmpty.cObject {
                value.current = 1
                ifEmpty = 0
            }
        }
        pageUid.cObject = TEXT
        pageUid.cObject {
            field = pageUid
            ifEmpty.data = TSFE:id
        }
        contentFromPid.cObject = TEXT
        contentFromPid.cObject {
            data = DB:pages:{register:pageUid}:content_from_pid
            data.insertData = 1
        }
    }
    20 = CONTENT
    20 {
        table = tt_content
        select {
            selectFields = count(uid) AS counter
            where = {#colPos} IN({register:colPos})
            where.insertData = 1
            pidInList.data = register:pageUid
            pidInList.override.data = register:contentFromPid
            andWhere = (deleted = 0 AND hidden = 0)
        }

        renderObj = COA
        renderObj {
            10 = TEXT
            10 {
                data = field:counter
            }
        }
    }
    90 = RESTORE_REGISTER
}

This snippet is tested and used in TYPO3 8.7 LTS without workspaces

Upvotes: 1

Claus Due
Claus Due

Reputation: 4271

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

Upvotes: 7

Related Questions