Processing a list with .split

When working with dynamic content, it is often convenient to loop through the items in a list, but Typoscript apparently has no loop constructs—at least not in the conventional sense.

However, it turns out that it is possible to feed Typoscript a list and separately process each item in the list. In other words, Typoscript does have a looping function, but instead of 'while,' 'do,' 'for,' or 'foreach', Typoscript calls it the 'split function.' In the following example, a developer needed to take a variable-length list composed of the uid fields of various languages from the static_languages table, break the list apart, and retrieve the localized language name for each item.

In this case, it was impossible to use other cObjects such as TMENU or CONTENT since (among other things) they do not have access to the static_languages table:

Output

Screen:

  • Dansk
  • Nederlands
  • English

Source:

<ul><li>Dansk</li>
<li>Nederlands</li>
<li>English</li>
</ul>

TS Code

  1. // Split a list:
  2. lib.samples.split_process_list = HTML
  3. lib.samples.split_process_list {
  4.   // List of language uids:
  5.   value = 28,29,30
  6.   value {
  7.     wrap = <ul>|</ul>
  8.     split {
  9.       // Split list by commas:
  10.       token = ,
  11.       // First cObject:
  12.       cObjNum = 1
  13.       1 {
  14.         // Use COA so that  other cObjects can be
  15.         // inserted around this one.
  16.         cObject = COA
  17.         cObject {
  18.           5 = HTML
  19.           5 {
  20.             // .insertData works a little like
  21.             // .prioriCalc. You can use TS to
  22.             // construct a string using the con-
  23.             // ventional .dataWrap syntax and then,
  24.             // once it's done, process it:
  25.             value {
  26.               // Start with a cObject--use .dataWrap
  27.               // syntax:
  28.               cObject = HTML
  29.               cObject {
  30.                 value {
  31.                   current = 1
  32.                   wrap = <li>{DB:static_languages:|:lg_name_local}</li>
  33.                 }
  34.               }
  35.               // Then, process it:
  36.               insertData = 1
  37.             }
  38.           }
  39.           // Add a newline to the end of each list
  40.           // item to make the code slightly prettier:
  41.           10 = HTML
  42.           10 {
  43.             value.char = 10
  44.           }
  45.         }
  46.       }
  47.     }
  48.   }
  49. }
Download this file: split_process_list.txt