Jul 11, 2008

Using templates in TYPO3 Frontend

Firsts, I wanted to write a warning article about the substituteMarkerArrayCached function. But then I understood that article is going to be about template functions in general and warning should be in the different article.

Let's have an overview of TYPO3 templates and functions to handle them.

TYPO3 template basics

Templates in TYPO3 are normal HTML files. These files contain special markers that TYPO3 can replace with data. Markers typically are words in CAPS surrounded by three hash marks. For example: ###LINK###.

There are two types of markers: single markers and subpart markers. Single markers is a single piece of data, it is replaced as is. Subpart markers surround parts of the HTML code. Here is an example:

Here ###LIST### and ###REPEATING### are subpart markers. Extension can get a subpart and replace it as whole. Often it is used for repeating elements of for subparts that should be removed under certain conditions. “begin” and “end” are not necessary but conventional.

###NUMBER### is a single marker.

Template functions in TYPO3 Frontend

TYPO3 has functions to work with templates. Extension authors may use these function to process markers.

The functions are located in class tslib_cObj and available in Frontend plugins through $this->cObj. Here is the list:

getSubpartObtains a subpart from template

substituteMarkerThis function substitutes a single marker. Just that.

substituteMarkerArrayThis function does the same as above but for many markers in the array

substituteSubpartSubstitutes a single subpart

substituteMarkerArrayCachedThis is a bad function that you should not use. Read why.

As you see, there is no function to substitute subpart array. You have to do it in a loop for each subpart.

Using TYPO3 template functions

But let's see code example. Example assumes that HTML template is located in $this->templateCode.