The class supports loading template files, adding variables to them and then
printing them to the screen. It functions as abstract parent class for the
two core classes "BackendTemplate" and "FrontendTemplate".
Usage:
$template = new BackendTemplate();
$template->name = 'Leo Feyer';
$template->output();
/** * Replace the default template * * @param Template $template */ public function onParseTemplate(Template $template) { // Do not override if the current scope is not frontend or the template has a custom template set // or the Bootstrap is not enabled if ($this->requestStack->getCurrentRequest()->get('_scope') !== ContaoCoreBundle::SCOPE_FRONTEND || $template->customTpl || !$this->isBootstrapEnabled()) { return; } $templateName = $template->getName(); // Do not override if there is no mapped template if (!$this->templateMapper->has($templateName)) { return; } $template->setName($this->templateMapper->get($templateName)); }
/** * Compile the template * * @param bool $blnCheckRequest If true, check for unsued $_GET parameters * * @throws \UnusedArgumentsException If there are unused $_GET parameters * * @internal Do not call this method in your code. It will be made private in Contao 5.0. */ protected function compile($blnCheckRequest = false) { $this->keywords = ''; $arrKeywords = array_map('trim', explode(',', $GLOBALS['TL_KEYWORDS'])); // Add the meta keywords if (strlen($arrKeywords[0])) { $this->keywords = str_replace(array("\n", "\r", '"'), array(' ', '', ''), implode(', ', array_unique($arrKeywords))); } // Parse the template $this->strBuffer = $this->parse(); // HOOK: add custom output filters if (isset($GLOBALS['TL_HOOKS']['outputFrontendTemplate']) && is_array($GLOBALS['TL_HOOKS']['outputFrontendTemplate'])) { foreach ($GLOBALS['TL_HOOKS']['outputFrontendTemplate'] as $callback) { $this->import($callback[0]); $this->strBuffer = $this->{$callback[0]}->{$callback[1]}($this->strBuffer, $this->strTemplate); } } // Add the output to the cache $this->addToCache(); // Unset only after the output has been cached (see #7824) unset($_SESSION['LOGIN_ERROR']); // Replace insert tags and then re-replace the request_token tag in case a form element has been loaded via insert tag $this->strBuffer = $this->replaceInsertTags($this->strBuffer, false); $this->strBuffer = str_replace(array('{{request_token}}', '[{]', '[}]'), array(REQUEST_TOKEN, '{{', '}}'), $this->strBuffer); $this->strBuffer = $this->replaceDynamicScriptTags($this->strBuffer); // see #4203 // HOOK: allow to modify the compiled markup (see #4291) if (isset($GLOBALS['TL_HOOKS']['modifyFrontendPage']) && is_array($GLOBALS['TL_HOOKS']['modifyFrontendPage'])) { foreach ($GLOBALS['TL_HOOKS']['modifyFrontendPage'] as $callback) { $this->import($callback[0]); $this->strBuffer = $this->{$callback[0]}->{$callback[1]}($this->strBuffer, $this->strTemplate); } } // Check whether all $_GET parameters have been used (see #4277) if ($blnCheckRequest && \Input::hasUnusedGet()) { throw new \UnusedArgumentsException(); } parent::compile(); }
/** * Create all header scripts * * @param \PageModel $objPage * @param \LayoutModel $objLayout */ protected function createHeaderScripts($objPage, $objLayout) { $strStyleSheets = ''; $strCcStyleSheets = ''; $arrStyleSheets = deserialize($objLayout->stylesheet); $arrFramework = deserialize($objLayout->framework); // Google web fonts if ($objLayout->webfonts != '') { $strStyleSheets .= \Template::generateStyleTag('//fonts.googleapis.com/css?family=' . str_replace('|', '%7C', $objLayout->webfonts), 'all') . "\n"; } // Add the Contao CSS framework style sheets if (is_array($arrFramework)) { foreach ($arrFramework as $strFile) { if ($strFile != 'tinymce.css') { $GLOBALS['TL_FRAMEWORK_CSS'][] = 'assets/contao/css/' . basename($strFile, '.css') . '.min.css'; } } } // Make sure TL_USER_CSS is set if (!is_array($GLOBALS['TL_USER_CSS'])) { $GLOBALS['TL_USER_CSS'] = array(); } // User style sheets if (is_array($arrStyleSheets) && strlen($arrStyleSheets[0])) { $objStylesheets = \StyleSheetModel::findByIds($arrStyleSheets); if ($objStylesheets !== null) { while ($objStylesheets->next()) { $media = implode(',', deserialize($objStylesheets->media)); // Overwrite the media type with a custom media query if ($objStylesheets->mediaQuery != '') { $media = $objStylesheets->mediaQuery; } // Style sheets with a CC or a combination of font-face and media-type != all cannot be aggregated (see #5216) if ($objStylesheets->cc || $objStylesheets->hasFontFace && $media != 'all') { $strStyleSheet = ''; // External style sheet if ($objStylesheets->type == 'external') { $objFile = \FilesModel::findByPk($objStylesheets->singleSRC); if ($objFile !== null) { $strStyleSheet = \Template::generateStyleTag(TL_ASSETS_URL . $objFile->path, $media); } } else { $strStyleSheet = \Template::generateStyleTag(TL_ASSETS_URL . 'assets/css/' . $objStylesheets->name . '.css', $media); } if ($objStylesheets->cc) { $strStyleSheet = '<!--[' . $objStylesheets->cc . ']>' . $strStyleSheet . '<![endif]-->'; } $strCcStyleSheets .= $strStyleSheet . "\n"; } else { // External style sheet if ($objStylesheets->type == 'external') { $objFile = \FilesModel::findByPk($objStylesheets->singleSRC); if ($objFile !== null) { $GLOBALS['TL_USER_CSS'][] = $objFile->path . '|' . $media . '|static|' . filemtime(TL_ROOT . '/' . $objFile->path); } } else { $GLOBALS['TL_USER_CSS'][] = 'assets/css/' . $objStylesheets->name . '.css|' . $media . '|static|' . max($objStylesheets->tstamp, $objStylesheets->tstamp2, $objStylesheets->tstamp3); } } } } } $arrExternal = deserialize($objLayout->external); // External style sheets if (!empty($arrExternal) && is_array($arrExternal)) { // Consider the sorting order (see #5038) if ($objLayout->orderExt != '') { $tmp = deserialize($objLayout->orderExt); if (!empty($tmp) && is_array($tmp)) { // Remove all values $arrOrder = array_map(function () { }, array_flip($tmp)); // Move the matching elements to their position in $arrOrder foreach ($arrExternal as $k => $v) { if (array_key_exists($v, $arrOrder)) { $arrOrder[$v] = $v; unset($arrExternal[$k]); } } // Append the left-over style sheets at the end if (!empty($arrExternal)) { $arrOrder = array_merge($arrOrder, array_values($arrExternal)); } // Remove empty (unreplaced) entries $arrExternal = array_values(array_filter($arrOrder)); unset($arrOrder); } } // Get the file entries from the database $objFiles = \FilesModel::findMultipleByUuids($arrExternal); if ($objFiles !== null) { $arrFiles = array(); while ($objFiles->next()) { if (file_exists(TL_ROOT . '/' . $objFiles->path)) { $arrFiles[] = $objFiles->path . '|static'; } } // Inject the external style sheets before or after the internal ones (see #6937) if ($objLayout->loadingOrder == 'external_first') { array_splice($GLOBALS['TL_USER_CSS'], 0, 0, $arrFiles); } else { array_splice($GLOBALS['TL_USER_CSS'], count($GLOBALS['TL_USER_CSS']), 0, $arrFiles); } } } // Add a placeholder for dynamic style sheets (see #4203) $strStyleSheets .= '[[TL_CSS]]'; // Always add conditional style sheets at the end $strStyleSheets .= $strCcStyleSheets; $newsfeeds = deserialize($objLayout->newsfeeds); $calendarfeeds = deserialize($objLayout->calendarfeeds); // Add newsfeeds if (!empty($newsfeeds) && is_array($newsfeeds)) { $objFeeds = \NewsFeedModel::findByIds($newsfeeds); if ($objFeeds !== null) { while ($objFeeds->next()) { $strStyleSheets .= \Template::generateFeedTag(($objFeeds->feedBase ?: \Environment::get('base')) . 'share/' . $objFeeds->alias . '.xml', $objFeeds->format, $objFeeds->title) . "\n"; } } } // Add calendarfeeds if (!empty($calendarfeeds) && is_array($calendarfeeds)) { $objFeeds = \CalendarFeedModel::findByIds($calendarfeeds); if ($objFeeds !== null) { while ($objFeeds->next()) { $strStyleSheets .= \Template::generateFeedTag(($objFeeds->feedBase ?: \Environment::get('base')) . 'share/' . $objFeeds->alias . '.xml', $objFeeds->format, $objFeeds->title) . "\n"; } } } // Add a placeholder for dynamic <head> tags (see #4203) $strHeadTags = '[[TL_HEAD]]'; // Add the user <head> tags if (($strHead = trim($objLayout->head)) != false) { $strHeadTags .= $strHead . "\n"; } $this->Template->stylesheets = $strStyleSheets; $this->Template->head = $strHeadTags; }
/** * Replace the dynamic script tags (see #4203) * * @param string $strBuffer The string with the tags to be replaced * * @return string The string with the replaced tags */ public static function replaceDynamicScriptTags($strBuffer) { // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags']) && is_array($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'])) { foreach ($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'] as $callback) { $strBuffer = static::importStatic($callback[0])->{$callback}[1]($strBuffer); } } $arrReplace = array(); $strScripts = ''; // Add the internal jQuery scripts if (!empty($GLOBALS['TL_JQUERY']) && is_array($GLOBALS['TL_JQUERY'])) { foreach (array_unique($GLOBALS['TL_JQUERY']) as $script) { $strScripts .= "\n" . trim($script) . "\n"; } } $arrReplace['[[TL_JQUERY]]'] = $strScripts; $strScripts = ''; // Add the internal MooTools scripts if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) { foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) { $strScripts .= "\n" . trim($script) . "\n"; } } $arrReplace['[[TL_MOOTOOLS]]'] = $strScripts; $strScripts = ''; // Add the internal <body> tags if (!empty($GLOBALS['TL_BODY']) && is_array($GLOBALS['TL_BODY'])) { foreach (array_unique($GLOBALS['TL_BODY']) as $script) { $strScripts .= trim($script) . "\n"; } } // Command scheduler if (!\Config::get('disableCron')) { $strScripts .= "\n" . \Template::generateInlineScript('setTimeout(function(){var e=function(e,t){try{var n=new XMLHttpRequest}catch(r){return}n.open("GET",e,!0),n.onreadystatechange=function(){this.readyState==4&&this.status==200&&typeof t=="function"&&t(this.responseText)},n.send()},t="system/cron/cron.";e(t+"txt",function(n){parseInt(n||0)<Math.round(+(new Date)/1e3)-' . \Frontend::getCronTimeout() . '&&e(t+"php")})},5e3);') . "\n"; } $arrReplace['[[TL_BODY]]'] = $strScripts; $strScripts = ''; $objCombiner = new \Combiner(); // Add the CSS framework style sheets if (!empty($GLOBALS['TL_FRAMEWORK_CSS']) && is_array($GLOBALS['TL_FRAMEWORK_CSS'])) { foreach (array_unique($GLOBALS['TL_FRAMEWORK_CSS']) as $stylesheet) { $objCombiner->add($stylesheet); } } // Add the internal style sheets if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) { foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) { $options = \StringUtil::resolveFlaggedUrl($stylesheet); if ($options->static) { $objCombiner->add($stylesheet, $options->mtime, $options->media); } else { $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n"; } } } // Add the user style sheets if (!empty($GLOBALS['TL_USER_CSS']) && is_array($GLOBALS['TL_USER_CSS'])) { foreach (array_unique($GLOBALS['TL_USER_CSS']) as $stylesheet) { $options = \StringUtil::resolveFlaggedUrl($stylesheet); if ($options->static) { $objCombiner->add($stylesheet, $options->mtime, $options->media); } else { $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n"; } } } // Create the aggregated style sheet if ($objCombiner->hasEntries()) { $strScripts .= \Template::generateStyleTag($objCombiner->getCombinedFile(), 'all') . "\n"; } $arrReplace['[[TL_CSS]]'] = $strScripts; $strScripts = ''; // Add the internal scripts if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) { $objCombiner = new \Combiner(); $objCombinerAsync = new \Combiner(); foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) { $options = \StringUtil::resolveFlaggedUrl($javascript); if ($options->static) { if ($options->async) { $objCombinerAsync->add($javascript, $options->mtime); } else { $objCombiner->add($javascript, $options->mtime); } } else { $strScripts .= \Template::generateScriptTag(static::addStaticUrlTo($javascript), $options->async) . "\n"; } } // Create the aggregated script and add it before the non-static scripts (see #4890) if ($objCombiner->hasEntries()) { $strScripts = \Template::generateScriptTag($objCombiner->getCombinedFile()) . "\n" . $strScripts; } if ($objCombinerAsync->hasEntries()) { $strScripts = \Template::generateScriptTag($objCombinerAsync->getCombinedFile(), true) . "\n" . $strScripts; } } // Add the internal <head> tags if (!empty($GLOBALS['TL_HEAD']) && is_array($GLOBALS['TL_HEAD'])) { foreach (array_unique($GLOBALS['TL_HEAD']) as $head) { $strScripts .= trim($head) . "\n"; } } $arrReplace['[[TL_HEAD]]'] = $strScripts; return str_replace(array_keys($arrReplace), array_values($arrReplace), $strBuffer); }
/** * Replace the dynamic script tags (see #4203) * * @param string $strBuffer The string with the tags to be replaced * * @return string The string with the replaced tags */ public static function replaceDynamicScriptTags($strBuffer) { // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags']) && is_array($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'])) { foreach ($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'] as $callback) { $strBuffer = static::importStatic($callback[0])->{$callback[1]}($strBuffer); } } $arrReplace = array(); $strScripts = ''; // Add the internal jQuery scripts if (!empty($GLOBALS['TL_JQUERY']) && is_array($GLOBALS['TL_JQUERY'])) { foreach (array_unique($GLOBALS['TL_JQUERY']) as $script) { $strScripts .= "\n" . trim($script) . "\n"; } } $arrReplace['[[TL_JQUERY]]'] = $strScripts; $strScripts = ''; // Add the internal MooTools scripts if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) { foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) { $strScripts .= "\n" . trim($script) . "\n"; } } $arrReplace['[[TL_MOOTOOLS]]'] = $strScripts; $strScripts = ''; // Add the internal <body> tags if (!empty($GLOBALS['TL_BODY']) && is_array($GLOBALS['TL_BODY'])) { foreach (array_unique($GLOBALS['TL_BODY']) as $script) { $strScripts .= trim($script) . "\n"; } } $arrReplace['[[TL_BODY]]'] = $strScripts; $strScripts = ''; $objCombiner = new \Combiner(); // Add the CSS framework style sheets if (!empty($GLOBALS['TL_FRAMEWORK_CSS']) && is_array($GLOBALS['TL_FRAMEWORK_CSS'])) { foreach (array_unique($GLOBALS['TL_FRAMEWORK_CSS']) as $stylesheet) { $objCombiner->add($stylesheet); } } // Add the internal style sheets if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) { foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) { $options = \StringUtil::resolveFlaggedUrl($stylesheet); if ($options->static) { if ($options->mtime === null) { $options->mtime = filemtime(TL_ROOT . '/' . $stylesheet); } $objCombiner->add($stylesheet, $options->mtime, $options->media); } else { $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n"; } } } // Add the user style sheets if (!empty($GLOBALS['TL_USER_CSS']) && is_array($GLOBALS['TL_USER_CSS'])) { foreach (array_unique($GLOBALS['TL_USER_CSS']) as $stylesheet) { $options = \StringUtil::resolveFlaggedUrl($stylesheet); if ($options->static) { $objCombiner->add($stylesheet, $options->mtime, $options->media); } else { $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n"; } } } // Create the aggregated style sheet if ($objCombiner->hasEntries()) { $strScripts .= \Template::generateStyleTag($objCombiner->getCombinedFile(), 'all') . "\n"; } $arrReplace['[[TL_CSS]]'] = $strScripts; $strScripts = ''; // Add the internal scripts if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) { $objCombiner = new \Combiner(); $objCombinerAsync = new \Combiner(); foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) { $options = \StringUtil::resolveFlaggedUrl($javascript); if ($options->static) { if ($options->mtime === null) { $options->mtime = filemtime(TL_ROOT . '/' . $javascript); } $options->async ? $objCombinerAsync->add($javascript, $options->mtime) : $objCombiner->add($javascript, $options->mtime); } else { $strScripts .= \Template::generateScriptTag(static::addStaticUrlTo($javascript), $options->async) . "\n"; } } // Create the aggregated script and add it before the non-static scripts (see #4890) if ($objCombiner->hasEntries()) { $strScripts = \Template::generateScriptTag($objCombiner->getCombinedFile()) . "\n" . $strScripts; } if ($objCombinerAsync->hasEntries()) { $strScripts = \Template::generateScriptTag($objCombinerAsync->getCombinedFile(), true) . "\n" . $strScripts; } } // Add the internal <head> tags if (!empty($GLOBALS['TL_HEAD']) && is_array($GLOBALS['TL_HEAD'])) { foreach (array_unique($GLOBALS['TL_HEAD']) as $head) { $strScripts .= trim($head) . "\n"; } } $arrReplace['[[TL_HEAD]]'] = $strScripts; return str_replace(array_keys($arrReplace), array_values($arrReplace), $strBuffer); }
/** * Compile the template * * @throws \UnusedArgumentsException If there are unused $_GET parameters * * @internal Do not call this method in your code. It will be made private in Contao 5.0. */ protected function compile() { $this->keywords = ''; $arrKeywords = \StringUtil::trimsplit(',', $GLOBALS['TL_KEYWORDS']); // Add the meta keywords if (strlen($arrKeywords[0])) { $this->keywords = str_replace(array("\n", "\r", '"'), array(' ', '', ''), implode(', ', array_unique($arrKeywords))); } // Parse the template $this->strBuffer = $this->parse(); // HOOK: add custom output filters if (isset($GLOBALS['TL_HOOKS']['outputFrontendTemplate']) && is_array($GLOBALS['TL_HOOKS']['outputFrontendTemplate'])) { foreach ($GLOBALS['TL_HOOKS']['outputFrontendTemplate'] as $callback) { $this->import($callback[0]); $this->strBuffer = $this->{$callback[0]}->{$callback[1]}($this->strBuffer, $this->strTemplate); } } // Replace insert tags $this->strBuffer = $this->replaceInsertTags($this->strBuffer); $this->strBuffer = $this->replaceDynamicScriptTags($this->strBuffer); // see #4203 // HOOK: allow to modify the compiled markup (see #4291) if (isset($GLOBALS['TL_HOOKS']['modifyFrontendPage']) && is_array($GLOBALS['TL_HOOKS']['modifyFrontendPage'])) { foreach ($GLOBALS['TL_HOOKS']['modifyFrontendPage'] as $callback) { $this->import($callback[0]); $this->strBuffer = $this->{$callback[0]}->{$callback[1]}($this->strBuffer, $this->strTemplate); } } // Check whether all $_GET parameters have been used (see #4277) if ($this->blnCheckRequest && \Input::hasUnusedGet()) { throw new \UnusedArgumentsException(); } parent::compile(); }