Processing a list with .split

2008 January 1 | Filed in: Typoscript, Blog Christopher Torgalson

How to loop through a comma-separated list of items with Typoscript's .split function

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

TS output

(Screen)

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

(Source)

  • Dansk
  • Nederlands
  • English

Typoscript

  1. ### Split a list:
  2. lib.blog.split_process_list = HTML
  3. lib.blog.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 inserted around this one.
  15.         cObject = COA
  16.         cObject {
  17.           5 = HTML
  18.           5{
  19.             ### .insertData works a little like .prioriCalc. You can use TS to construct a string using the conventional .dataWrap syntax and then, once it's done, process it:
  20.             value {
  21.               ### Start with a cObject--use .dataWrap syntax:
  22.               cObject = HTML
  23.               cObject {
  24.                 value {
  25.                   current = 1
  26.                   wrap = <li>{DB:static_languages:|:lg_name_local}</li>
  27.                 }
  28.               }
  29.               ### Then, process it:
  30.               insertData = 1
  31.             }
  32.           }
  33.           ### Add a newline to the end of each list item to make the code slightly prettier:
  34.           10 = HTML
  35.           10{
  36.             value.char = 10
  37.           }
  38.         }
  39.       }
  40.     }
  41.   }
  42. }

Processing a list with .split Download plain text version

Comments

No comments

Add comment

*
*
* required field

www.typo3apprentice.com
A Bedlam Hotel project © 2007—2010

Report a problem with this page

Bookmark and Share