TemplaVoila in your own extension
Thursday, May 15th, 2008TemplaVoila is an incredibly smart tool for templating your TYPO3 website. However it cannot only be used for pages and fragments of pages (FCE:s), but also for templating your own extensions output.
The classic way of adding template support for an extension has been to use an HTML file marked with tags like
-
###MY_REPLACED_MARKER###
or similar that would be replaced by content using the cObj function substituteMarkerArrayCached:
-
$markerArray[‘###MY_REPLACED_MARKER###’] = $data;
-
$output = $this->cObj->substituteMarkerArrayCached($template, $markerArray);
Which is a quite simple and easy to use approach. The TemplaVoila way quite similiar. Let me show you a short example more or less cut from the mininews extension with a few simplifications for us mere mortals;
First off we will load up TV with a hard coded TO (Template Object) record uid. I suggest using some kind of FlexForm setting or similar for that
This part normally goes into an init() function or so;
-
// Load and initialize TemplaVoila TO (if any)
-
if (t3lib_extMgm::isLoaded(‘templavoila’)) {
-
$field_templateObject = 1; //read my default page "TO" record uid..
-
$this->TMPLobj = t3lib_div::makeInstance(‘tx_templavoila_htmlmarkup’);
-
$this->TMPLobj->setHeaderBodyParts($this->TMPLobj->tDat[‘MappingInfo_head’],$this->TMPLobj->tDat[‘MappingData_head_cached’]);
-
}
-
}
-
}
What the code above does is checking if TV exists, instantiates the templavoila HTML markup utility class (tx_templavoila_htmlmarkup), loads a TO record with uid one into it and finally sets some HTML header taggings..
Sooo, we got ourself a loaded TO with HTML template contents and also since TO always has a DS, we got the data structure fields (like field_mainMenu or similar). Next of is adding contents to where we’d like to have it. In our case into the field field_content mapped with TV.
-
//we got some content into $content var loaded from db earlier, now render it with TV
-
// Create list of elements:
-
$templatedResult = $this->TMPLobj->mergeDataArrayToTemplateArray(
-
$this->TA,
-
‘field_content’ => $content
-
)
-
);
-
}
-
$out = $templatedResult;
That’s it!
Using TemplaVoila for an extension has many benifits, just as using it for normal page rendering. One of them is the possibility to easily map your template using Templavoila. I suggest that you supply a valid datastructure (DS) xml with your extension, that gets loaded as static ds (again taken from mininews);
-
// Adding datastructure for Mininews:
-
‘title’ => ‘Mininews Template’,
-
‘path’ => ‘EXT:’.$_EXTKEY.‘/template_datastructure.xml’,
-
‘icon’ => ”,
-
‘scope’ => 0,
-
);
For a more detailed documentation around TV and the source to inspiration for this article, see the TemplaVoila extension documentation (http://typo3.org/documentation/document-library/extension-manuals/templavoila/1.3.4/view/1/6/)
