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:
<li>Nederlands</li>
<li>English</li>
</ul>
TS Code
- // Split a list:
- lib.samples.split_process_list = HTML
- lib.samples.split_process_list {
- // List of language uids:
- value = 28,29,30
- value {
- wrap = <ul>|</ul>
- split {
- // Split list by commas:
- token = ,
- // First cObject:
- cObjNum = 1
- 1 {
- // Use COA so that other cObjects can be
- // inserted around this one.
- cObject = COA
- cObject {
- 5 = HTML
- 5 {
- // .insertData works a little like
- // .prioriCalc. You can use TS to
- // construct a string using the con-
- // ventional .dataWrap syntax and then,
- // once it's done, process it:
- value {
- // Start with a cObject--use .dataWrap
- // syntax:
- cObject = HTML
- cObject {
- value {
- current = 1
- wrap = <li>{DB:static_languages:|:lg_name_local}</li>
- }
- }
- // Then, process it:
- insertData = 1
- }
- }
- // Add a newline to the end of each list
- // item to make the code slightly prettier:
- 10 = HTML
- 10 {
- value.char = 10
- }
- }
- }
- }
- }
- }
