So anyway, just in case anyone else is interested in this feature, here is what I did. This is -- I think -- a much better version than the one I put in the feature request because it parses the templates folder once upon page load and stores as $GLOBALS['TL_CONFIG']['customTemplates'], then every time after that references this array instead.
First I added this function:
Code:
function getCustomTemplates($strBaseFolder)
{
$strBaseFolder = rtrim($strBaseFolder, '/');
$arrTemplates = array();
$arrFilters = array();
$arrFilters[] = '.';
$arrFilters[] = '..';
$arrFilters[] = '.htaccess';
foreach (array_diff(scandir($strBaseFolder), $arrFilters) as $strFileName)
{
if (!is_bool(strripos($strFileName, '.tpl')))
{
$arrTemplates[basename($strFileName, '.tpl')] = $strBaseFolder;
}
if (is_dir($strBaseFolder . '/' . $strFileName))
{
$arrTemplates = array_merge($arrTemplates, getCustomTemplates($strBaseFolder . '/' . $strFileName));
}
}
return $arrTemplates;
}
This into localconfig.php:
Code:
$GLOBALS['TL_CONFIG']['customTemplates'] = getCustomTemplates(TL_ROOT . '/templates/');
And finally made the following two changes to the functions in Controller.php:
Code:
protected function getTemplate($strTemplate)
{
$strTemplate = basename($strTemplate);
$arrCustomTemplates = $GLOBALS['TL_CONFIG']['customTemplates'];
foreach($arrCustomTemplates as $strCustomTemplate => $strCustomFolder)
{
if ($strCustomTemplate == $strTemplate)
{
$strFile = sprintf('%s/%s.tpl', $strCustomFolder, $strCustomTemplate);
}
}
if (!isset($strFile))
{
foreach ($this->Config->getActiveModules() as $strModule)
{
$strPath = sprintf('%s/system/modules/%s/templates/%s.tpl', TL_ROOT, $strModule, $strTemplate);
if (file_exists($strPath))
{
$strFile = $strPath;
break;
}
}
}
if (!file_exists($strFile))
{
throw new Exception(sprintf('Could not find template file "%s"', $strTemplate));
}
return $strFile;
}
Code:
protected function getTemplateGroup($strPrefix)
{
$arrTemplates = array();
$arrFolders = array();
$arrCustomTemplates = $GLOBALS['TL_CONFIG']['customTemplates'];
foreach ($this->Config->getActiveModules() as $strModule)
{
$strFolder = sprintf('%s/system/modules/%s/templates', TL_ROOT, $strModule);
if (is_dir($strFolder))
{
$arrFolders[] = $strFolder;
}
}
foreach ($arrFolders as $strFolder)
{
$arrFiles = preg_grep('/^' . preg_quote($strPrefix, '/') . '.*\.tpl$/i', scan($strFolder));
foreach ($arrFiles as $strTemplate)
{
$arrTemplates[] = basename($strTemplate, '.tpl');
}
}
foreach ($arrCustomTemplates as $strCustomTemplate => $strCustomFolder)
{
if(!is_bool(strripos($strCustomTemplate, $strPrefix)))
{
$arrTemplates[] = $strCustomTemplate;
}
}
natcasesort($arrTemplates);
return array_values(array_unique($arrTemplates));
}
I have no idea how to make this update safe, or if such a thing is even possible.
Brian
Bookmarks