contao/import — Issue #3097: Hook to Alter Template Data Before Parsing
Author: importer
Created: 2011-11-29 20:52:44+00:00
State: open
Labels: Feature, framework

Would you ever consider adding a hook that allows developers to alter a template's $arrData _before_ being parsed?

Something like what's suggested by Thyon at the bottom of this thread: http://www.contao-community.org/viewtopic.php?f=9&t=1221&p=5086&hilit=add+variables+to+template#p5086.

Just to illustrate, here's an example (for Frontend Templates)...

```
class FrontendTemplate extends Template
{
public function parse()
{
// HOOK: modify template data
if (isset($GLOBALS['TL_HOOKS']['someTemplateDataHook']) && is_array($GLOBALS['TL_HOOKS']['someTemplateDataHook']))
{
foreach ($GLOBALS['TL_HOOKS']['someTemplateDataHook'] as $callback)
{
$this->import($callback[0]);
$this->arrData = $this->$callback[0]->$callback[1]($this->arrData, $this->strTemplate);
}
}

$strBuffer = parent::parse();

// ...rest of the class...
```

Which could get picked up by...

```
$GLOBALS['TL_HOOKS']['someTemplateDataHook'][] = array('TemplateDataFilter', 'filterTemplateData');

class TemplateDataFilter
{
public function filterTemplateData($arrData, $strTemplate)
{
if ($strTemplate == 'mod_navigation')
{
$arrData['someProperty'] = 'someValue';
}
return $arrData;
}
}
```

Or is this a bad idea? What do you think of this? On the surface, it seems it would be really convenient, and make things really flexible. But I'm not nearly as smart as you all. :)

I know most (maybe even all) of what I'd want to do with this can technically be done inside the template file. But it would be so nice to keep those clean and keep the logic somewhere else -- especially since it can be centralized in one class. Plus it would open some new doors for extension developers.

But this strikes me as something that may have been asked before (I looked but couldn't find it). Anyway, just thought I'd ask.

Related issues: #2582

--- _Originally created by Medianomaly on May 23rd, 2011, at 06:33am (ID 3097)_