Search code examples
typoscripttypo3-10.x

Typopscript hide subtitle if empty


I just can't understand how typoscript if constructs work.

what I would like to do is display the subtitle of a page only if present. I tried with the if clause of the fluidetemplate, in this way, but it doesn't work (always print <p class="subTitle"> tag also if no subtitle is present):

<f:if condition="page : subtitle !=''">
    <p class="subTitle">{f:cObject(typoscriptObjectPath:'lib.pageSubTitle')->f:format.raw()}</p>
</f:if>

So I wanted to try using typoscritp but I can't figure it out.

This is the current situation:

lib.pageSubTitle = TEXT
lib.pageSubTitle.data = page: subtitle
lib.pageSubTitle.wrap = <p class = "subTitle"> | </p>

I would like, in case of absence of subtile, let lib.pageSubTitle was "emptied", so as not to have an empty <p> object that increases the margin between the title and the body of the page. Otherwise i want to wrap subtitle only if is not empty.

I hope I have explained my problem well.

Thanks in advance


Solution

  • Your fluid is a bit too complex, you can just use

    <f:if condition="{page.subtitle}">
        <p class="subTitle">{page.subtitle}</p>
    </f:if>
    

    If you want to use TypoScript, you can use the option stdWrap.required = 1 to make your TypoScript work

    lib.pageSubTitle = TEXT
    lib.pageSubTitle {
        data = page: subtitle
        stdWrap.required = 1
        stdWrap.wrap = <p class = "subTitle"> | </p>
    }