TYPO3: how to display content elements from different pages

1k Views Asked by At

Hopefully, someone knows how I can simplify the following TypoScript so I can pass a list of page ids from the template constant to the template script. say, I have defined a TEASER_IDS = 6,7,9,12,4, and the TypeScript walks thru that list and uses it for the select.pidInList one after the other instead of having to manually create a CONTENT object in the TypoScript for each ID.

lib.teaser = COA
lib.teaser {  
  10 = CONTENT
  10 {
    stdWrap.wrap = <div class="part">|</div>
    table = tt_content
    select.orderBy = sorting
    select.pidInList = 6
    select.where = colPos=1
    select.languageField = sys_language_uid
  }
  20 = CONTENT
  20 {
    stdWrap.wrap = <div class="part">|</div>
    table = tt_content
    select.orderBy = sorting
    select.pidInList = 7
    select.where = colPos=1
    select.languageField = sys_language_uid
  }
}

page.50.30 < lib.teaser

It is TYPO3 version 10.4.8

EDIT: new version but I'd like to have the elements be wrapped each, so it results in

//this is what it should look like
<div class="header">CONTENT ID 6 colPos 1</div>
<div class="text">CONTENT ID 6 colPos 2</div>
<div class="header">CONTENT ID 7 colPos 1</div>
<div class="text">CONTENT ID 7 colPos 2</div>
<div class="header">CONTENT ID 4 colPos 1</div>
<div class="text">CONTENT ID 4 colPos 2</div>

TypoScript now:

lib.teaser = COA
lib.teaser {  
  10 = CONTENT
  10 {
    stdWrap.wrap = <div class="header">|</div>
    table = tt_content
    select.orderBy = sorting
    select.pidInList = 6,7,4
    select.where = colPos=1
    select.languageField = sys_language_uid
  }
  20 = CONTENT
  20 {
    stdWrap.wrap = <div class="text">|</div>
    table = tt_content
    select.orderBy = sorting
    select.pidInList = 6,7,4
    select.where = colPos=2
    select.languageField = sys_language_uid
  }
}

page.50.30 < lib.teaser

The code gives me, which makes sense, but I have no idea how to change the code so it gives me the preferred output like mentioned above the new code

//this is not what i want
<div class="header">
CONTENT ID 6 colPos 1
CONTENT ID 7 colPos 1
CONTENT ID 4 colPos 1
</div>
<div class="text">
CONTENT ID 6 colPos 2
CONTENT ID 7 colPos 2
CONTENT ID 4 colPos 2
</div>
2

There are 2 best solutions below

0
On

If you want a loop over the pages where you collect content from different columns you need to build this structure in typoscript.

As you have noticed now you get all content for column 1 from all pages and then all content from column2.

How could you build a loop over the page IDs? we have no for loop in typoscript as it is a configuration language and no programming language.

That might be possible with a split on the comma separated list of the ids.

lib.teaser = TEXT
lib.teaser {
    # list of page ids:
    value = 6,7,9,12,4
    split.token = ,

    # only one kind of rendering (otherwise use optionsplit to define cases)
    cObjNum = 1
    # example: 1 = first, 4 = last, between: 2,3 alternating
    # cObjNum = 1 |*| 2 || 3 |*| 4
    # cases of rendering:
    1 = COA
    1 {
        10 = CONTENT
        10 {
            stdWrap.wrap = <div class="part">|</div>
            table = tt_content
            select.orderBy = sorting
            # use current value of split:
            select.pidInList.current = 1
            select.where = colPos=1
            select.languageField = sys_language_uid
        }
        20 = CONTENT
        20 {
            stdWrap.wrap = <div class="part">|</div>
            table = tt_content
            select.orderBy = sorting
            select.pidInList.current = 1
            select.where = colPos=1
            select.languageField = sys_language_uid
        }
    }
}
0
On

Do not use two CONTENT objects as they both iterate over all matching CEs. Instead, use the option to overwrite the wrap if the colPos has a corresponding value.

stdWrap {
  wrap = <div class="text">|</div>
  override = <div class="header">|</div>
  override.if {
    value.field = colPos
    equals = 1
  }
}

( https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/Functions/If.html#if )