HTML Templating

Basics

Eintrag in setup.txt: plugin.tx_myextension_pi1.templateFile = EXT:my_extension_key/template.html

Verwendung in der main() function des FE Plugins:

# Get the template
$this->templateCode = $this->cObj->fileResource($conf['templateFile']);

# Get the parts out of the template
$template['total'] = $this->cObj->getSubpart($this->templateCode,'###TEMPLATE###');

# create the content by replacing the marker in the template
$markerArray['###MARKER1###'] = 'content for marker 1';
$markerArray['###MARKER2###'] = 'content for marker 2';

$content = $this->cObj->substituteMarkerArrayCached($template['total'],$markerArray);
return $content;


Das Basic template:

<h3>TEMPLATE</h3>
<em>This is a simple HTML-Template</em>
<!-- ###TEMPLATE### begin -->
<h1>###MARKER1###</h1><h1>###MARKER2###</h1>
<!-- ###TEMPLATE### end -->

Subparts

Sinvoll zum Füllen von Reihen.

# Get the template
$this->templateCode = $this->cObj->fileResource($this->conf['templateFile']);

# Get the parts out of the template
$template['total'] = $this->cObj->getSubpart($this->templateCode,'###TEMPLATE###');
$template['item'] = $this->cObj->getSubpart($template['total'],'###ITEM###');

...
while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
# Build content from template
$markerArray['###MARKER1###'] = $row['username'];
$markerArray['###MARKER2###'] = $row['price'];
$content_item .= $this->cObj->substituteMarkerArrayCached($template['item'], $markerArray);
}

$subpartArray['###CONTENT###'] = $content_item;

$content = $this->cObj->substituteMarkerArrayCached($template['total'], array(), $subpartArray);
return $content;

Das Subparts Template (Die Marker zwischen den <!-- ###CONTENT### --> werden mit den Inhalten der Datenbank ersetzt.

h3>TEMPLATE</h3>
<em>This is a more complex html template</em>
<!-- ###TEMPLATE### begin -->
<table>
<!-- ###CONTENT### -->
<!-- ###ITEM### -->
<tr>
<td>###MARKER1###</td><td>###MARKER2###</td>
</tr>
<!-- ###ITEM### -->
<!-- ###CONTENT### -->
</table>
<!-- ###TEMPLATE### end -->

Wrapped Subparts

Nützlich für Links.

#Preconfigure the typolink
$this->local_cObj = t3lib_div::makeInstance('tslib_cObj');
$this->local_cObj->setCurrentVal($GLOBALS['TSFE']->id);
$this->typolink_conf = $this->conf['typolink.'];
$this->typolink_conf['parameter.']['current'] = 1;

# Get the template
$this->templateCode = $this->cObj->fileResource($this->conf['templateFile']);

# Get the parts out of the template
$template['total'] = $this->cObj->getSubpart($this->templateCode,'###TEMPLATE###');

# Typolink
$temp_conf = $this->typolink_conf;
$temp_conf['additionalParams'].= '&tx_myextension_pi1[mode]=list&tx_myextension_pi1[cat]='.$row['uid'];
$temp_conf['useCacheHash']=$this->allowCaching;
$temp_conf['no_cache']=!$this->allowCaching;
$temp_conf['parameter.']['wrap'] = "|,".$GLOBALS['TSFE']->type;
$wrappedSubpartContentArray['###LINK###'] = $this->local_cObj->typolinkWrap($temp_conf);

$content = $this->cObj->substituteMarkerArrayCached($template['total'],array(), array(), $wrappedSubpartContentArray);
return $content;


Das Wrapped Subparts template:

<h3>TEMPLATE</h3>
<em>This is an example template for wrapped subparts</em>
<!-- ###TEMPLATE### begin -->
<table border="1" cellpadding="2">
<tr><td><!--###LINK###-->Hello world!<!--###LINK###--></td></tr>
</table>
<!-- ###TEMPLATE### end -->

Hinweise

Wenn viele Marker ersetzt werden müsse bietet sich eine Schleife an:

$arrayAll = array('uid', 'land', 'kontinent', ...)
foreach ($arrayAll as $marker) {
$markerArray['###'.strtoupper($marker).'###'] = $this->pi_getLL($marker);
}

Alle 3 Marker können kombiniert werden:

# Get the parts out of the template
$template = array();
$template['total'] = $this->cObj->getSubpart($this->templateCode,'###TEMPLATE###');
$template['item'] = $this->cObj->getSubpart($template['total'],'###ITEM###');

# create the content by replacing the marker in the template
$markerArray['###MARKER1###'] = $this->pi_getLL('MARKER1');
$markerArray['###MARKER2###'] = $this->pi_getLL('MARKER2');

...
while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
# Build content from template + array
$markerArray['###MARKER3###'] = $row['username'];
$markerArray['###MARKER4###'] = $row['price'];
$content_item .= $this->cObj->substituteMarkerArrayCached($template["item"], $markerArray);
}

$subpartArray['###CONTENT###'] = $content_item;

# Typolink
$temp_conf = $this->typolink_conf;
$temp_conf["additionalParams"].= '&tx_myextension_pi1[mode]=list&tx_myextension_pi1[cat]='.$row['uid'];
$temp_conf['useCacheHash']=$this->allowCaching;
$temp_conf['no_cache']=!$this->allowCaching;
$temp_conf['parameter.']['wrap'] = "|,".$GLOBALS['TSFE']->type;
$wrappedSubpartContentArray['###LINK###'] = $this->local_cObj->typolinkWrap($temp_conf);

$content = $this->cObj->substituteMarkerArrayCached($template['total'], $markerArray, $subpartArray, $wrappedSubpartContentArray);

Das kombinierte Template:

<h3>TEMPLATE</h3>
<em>This is an example template for all combined markers</em>
<!-- ###TEMPLATE### begin -->
<h1>###MARKER1###</h1><h1>###MARKER2###</h1>
<table>
<!-- ###CONTENT### -->
<!-- ###ITEM### -->
<tr>
<td>###MARKER3###</td><td>###MARKER4###</td>
</tr>
<!-- ###ITEM### -->
<!-- ###CONTENT### -->
</table>
<table border="1" cellpadding="2">
<tr><td><!--###LINK###-->Hello World!<!--###LINK###--></td></tr>
</table>
<!-- ###TEMPLATE### end -->

Abwechselnde Reihen

Für hübsche Tabellen mit abwechselnden Farben können abwechselnde Reihen mit Subparts realisiert weden:

# Get the template
$this->templateCode = $this->cObj->fileResource($this->conf['templateFile']);

# Get the parts out of the template
$template['total'] = $this->cObj->getSubpart($this->templateCode,'###TEMPLATE###');
$template['singlerow'] = $this->cObj->getSubpart($template['total'],'###SINGLEROW###');
$template['row'] = $this->cObj->getSubpart($template['singlerow'],'###ITEM###');
$template['row_alt'] = $this->cObj->getSubpart($template['singlerow'],'###ITEM_ALT###');

$switch_row = 0;
...
while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
# Build content from template + array
$markerArray['###MARKER1###'] = $row['username'];
$markerArray['###MARKER2###'] = $row['price'];

$switch_row = $switch_row ^ 1;
if($switch_row) {
$content_item .= $this->cObj->substituteMarkerArrayCached($template['row'], $markerArray);
} else {
$content_item .= $this->cObj->substituteMarkerArrayCached($template['row_alt'], $markerArray);
}
}

$subpartArray['###SINGLEROW###'] = $content_item;

$content = $this->cObj->substituteMarkerArrayCached($template['total'], array(), $subpartArray);
return $content;

Das abwechselnden Reihen Template:

<h3>TEMPLATE</h3>
<em>This is the alternative rows html template</em>
<!-- ###TEMPLATE### begin -->
<table>
<!-- ###SINGLEROW### begin -->
<!-- ###ITEM### -->
<tr bgcolor="#e0e0e0">
<td>###MARKER1###</td><td>###MARKER2###</td>
</tr>
<!-- ###ITEM### -->
<!-- ###ITEM_ALT### -->
<tr>
<td>###MARKER1###</td><td>###MARKER2###</td>
</tr>
<!-- ###ITEM_ALT### -->
<!-- ###SINGLEROW### end -->
</table>
<!-- ###TEMPLATE### end -->

(Siehe wiki.typo3.org/index.php/Extension_Development%2C_using_HTML-Templates)

Erstellt: 07/2010| Geändert: 10/2015