1

I would like to render a page from tt_content table resulting something like example given below. The pseudo-code is as follows:

if (header_link != "" && image != "") {
    // build content with header_link and image
    html = <div><a href="{header_link}"><img src="{img}" /></a></div>
} else {
    // build content without those fields
    html = <div><h1>{header}</h1></div>
}

I have the typoscript structure as follows:

temp.myContent = CONTENT
temp.myContent {
    table = tt_content
    select {
        begin = 1
        orderBy = sorting
        where = (colPos = 2)
    }
    renderObj = COA 
    renderObj {

        # PLANNING TO PLACE THE CONDITIONS SOMEWHERE HERE

        # 10 = TEXT  
        # 10 {
             ...
        # }
    }
}

I don't know why there is no working sample codes on Typo3's official website, all they have is descriptions. I am therefore again relying on stackoverflow-ies. :)

derhansen
  • 5,585
  • 1
  • 19
  • 29
Ren
  • 330
  • 6
  • 23

2 Answers2

1

Remember that TypoScript is just a configuration table and not programing language. What's more you cannot place condition within any object as it's required that all conditions must be resolved at the top level. Fortunately you have at least two workarounds, you can just create own ContentElements Let's name it MySepcialHeader and MySpecialHeaderWithImg so instead fighting with conditions you can place one of them before text CE. How to achieve this?

GridElements extension

Is quite useful tool, you can create own CE-like structures without touching any single line of PHP, all is done within Db records (CE Backend Layouts) and corresponding TypoScript. It can be compared to TemplaVoilà's FCEs, more in docs here and here.

Own real CType

You can also just create own CTypes i.e. using Extension Builder it's little bit more of fun, but it gives you unlimited possibilities as you operate with PHP on the Fluid views, that's enough if you'll create one extension, then you can add within it unlimited number of plugins/CTypes.

You'll find more about this technique in the other answer.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182
0

This TypoScript template worked for me on a TYPO3 CMS 6.2.17 server.

page.30 = CONTENT
page.30 {
    table = tt_content
    select {
        orderBy = sorting
        where = colPos = 0
    }
    renderObj = COA 
    renderObj {
        wrap = <div>|</div>
        10 = COA
        10.10 = TEXT
        10.10.if.isTrue.field = header_link
        10.10.if.isFalse.field = image
        10.10 {
            stdWrap.data = header_link
            stdWrap.typolink {
                parameter.field = header_link
                wrap = <img src="|" />
                ATagBeforeWrap = 1
            }
        }
        10.20 = TEXT
        10.20.if.isFalse.field = header_link
        10.20.stdWrap.field = header
        10.20.stdWrap.wrap = <h1>|</h1>
    }
}

The result displays the tt_content header field unless both the header_link and image fields test positive. Here are the relevant fields of the tt_content records I used in the test. (The || indicates an empty field value.)

uid|hidden|Sorting|CType|header|image|deleted|colPos|header_link|
29|0|256|text|Regular text element|NULL|0|0||
30|0|512|image|Image element|0|0|0|file:37|
31|0|384|image|Image element with no image|0|0|0||
32|0|768|div|Divider|NULL|0|0||

I include the hidden and deleted columns as a reminder, because if either are set to 1, the CONTENT select function will not return that record for renderObj to use. There are also checks for start and end date, and whether the current user has access privileges to the content element. See "select" in the TypoScript Reference manual.

Multiple TypoScript if statements on an object are joined together with an implicit logical AND. See the "if" page in the TypoScript Reference and its "more complex" example having two conditions under "Explanation".

Here is the if logic involved in this test.

Object page.30.renderObj.10.10.
uid|header_link|if.isTrue.field = header_link|image|if.isFalse.field = image|Result
29||no|NULL|no|not rendered
30|file:37|yes|0|yes|rendered
31||no|0|yes|not rendered
32||no|NULL|no|not rendered

Object page.30.renderObj.10.20.
uid|header_link|if.isFalse.field = header_link|Result
29||yes|rendered
30|file:37|no|not rendered
31||yes|rendered
32||yes|rendered

The output display yields the same result if we use only the if.isTrue.field = header_link test on for the page.30.renderObj.10.10 object, and leave off the if.isFalse.field = image test. However, keeping the if.isFalse.field = image test more closely answers the request, and illustrates checking multiple fields.

Andrew
  • 585
  • 4
  • 17