Contao\Controller::getTemplateGroup PHP Method

getTemplateGroup() public static method

Return all template files of a particular group as array
public static getTemplateGroup ( string $strPrefix ) : array
$strPrefix string The template name prefix (e.g. "ce_")
return array An array of template names
    public static function getTemplateGroup($strPrefix)
    {
        $arrTemplates = array();
        // Get the default templates
        foreach (\TemplateLoader::getPrefixedFiles($strPrefix) as $strTemplate) {
            $arrTemplates[$strTemplate][] = 'root';
        }
        $arrCustomized = glob(TL_ROOT . '/templates/' . $strPrefix . '*');
        // Add the customized templates
        if (is_array($arrCustomized)) {
            foreach ($arrCustomized as $strFile) {
                $strTemplate = basename($strFile, strrchr($strFile, '.'));
                $arrTemplates[$strTemplate][] = $GLOBALS['TL_LANG']['MSC']['global'];
            }
        }
        // Do not look for back end templates in theme folders (see #5379)
        if ($strPrefix != 'be_' && $strPrefix != 'mail_') {
            // Try to select the themes (see #5210)
            try {
                $objTheme = \ThemeModel::findAll(array('order' => 'name'));
            } catch (\Exception $e) {
                $objTheme = null;
            }
            // Add the theme templates
            if ($objTheme !== null) {
                while ($objTheme->next()) {
                    if ($objTheme->templates != '') {
                        $arrThemeTemplates = glob(TL_ROOT . '/' . $objTheme->templates . '/' . $strPrefix . '*');
                        if (is_array($arrThemeTemplates)) {
                            foreach ($arrThemeTemplates as $strFile) {
                                $strTemplate = basename($strFile, strrchr($strFile, '.'));
                                $arrTemplates[$strTemplate][] = $objTheme->name;
                            }
                        }
                    }
                }
            }
        }
        // Show the template sources (see #6875)
        foreach ($arrTemplates as $k => $v) {
            $v = array_filter($v, function ($a) {
                return $a != 'root';
            });
            if (empty($v)) {
                $arrTemplates[$k] = $k;
            } else {
                $arrTemplates[$k] = $k . ' (' . implode(', ', $v) . ')';
            }
        }
        // Sort the template names
        ksort($arrTemplates);
        return $arrTemplates;
    }

Usage Example

 /**
  * Collect a template group.
  *
  * @param GetTemplateGroupEvent $event The event.
  *
  * @return void
  */
 public function handleGetTemplateGroup(GetTemplateGroupEvent $event)
 {
     $templatesArray = Controller::getTemplateGroup($event->getPrefix());
     $templates = $event->getTemplates();
     foreach ($templatesArray as $templateName => $templateLabel) {
         $templates[$templateName] = $templateLabel;
     }
 }