public static function tableFromArray($aParams, $sClassName = 'doc_types') { if (count($aParams) === 0) { return; } $sOutput = "<table class=\"" . Template::htmlEncode($sClassName) . "\">" . PHP_EOL; $sOutput .= "\t<tr class=\"header\">" . PHP_EOL; foreach ($aParams[0] as $sKey => $sValue) { $sOutput .= "\t\t<td>" . Template::htmlEncode($sKey) . "</td>" . PHP_EOL; } $sOutput .= "\t</tr>" . PHP_EOL; foreach ($aParams as $aValues) { $sOutput .= "\t<tr>" . PHP_EOL; foreach ($aValues as $sValue) { $sOutput .= "\t\t<td>" . Template::htmlEncode($sValue) . "</td>" . PHP_EOL; } $sOutput .= "\t</tr>" . PHP_EOL; } $sOutput .= "</table>" . PHP_EOL; return new Template($sOutput, null, true); }
public static function getString($sKey, $sLanguageId = null, $sDefaultValue = null, $aParameters = null, $bMayReturnTemplate = false, $iFlags = 0) { $bNoHTMLInReturnedString = $bMayReturnTemplate === null; if (Settings::getSetting('frontend', 'display_string_keys', false)) { return $sKey; } if (!is_string($sDefaultValue)) { $sDefaultValue = "Translation missing: {$sKey}"; } if ($sLanguageId === null) { $sLanguageId = Session::language(); } $oString = self::retrieveByPK($sLanguageId, $sKey); $sString = ''; if ($oString === null) { $sString = self::getStaticString($sKey, $sLanguageId); if ($sString === null) { return $sDefaultValue; } } else { $sString = $oString->getText(); } if (is_array($aParameters) && count($aParameters) > 0 || strpos($sString, TEMPLATE_IDENTIFIER_START) !== false) { if (!is_array($aParameters)) { $aParameters = array(); } if (!$bMayReturnTemplate) { //NO_HTML_ESCAPE works in any case: //Case $bMayReturnTemplate === false: strings will always be included in templates and thus always be encoded correctly in a second step. //Case $bMayReturnTemplate === null: strings should not contain any HTML-characteristics, including entities. {{br}}-Tags will be stripped as well. $iFlags = $iFlags | Template::NO_HTML_ESCAPE; } if ($bMayReturnTemplate) { //If returned item is a template, it is assumed that nothing will run htmlentities on it later on so we have to run it now. $sString = Template::htmlEncode($sString); } if ($bNoHTMLInReturnedString) { $sString = str_replace('{{br}}', "\n", $sString); } $tmpl = new Template($sString, "db", true, false, Settings::getSetting("encoding", "db", "utf-8"), null, $iFlags); foreach ($aParameters as $sKey => $sValue) { $tmpl->replaceIdentifier($sKey, $sValue); } if ($bMayReturnTemplate) { return $tmpl; } $sString = $tmpl->render(); } return $sString; }
/** * Does the actual parsing of the tag to produce valid XHTML output. The default implementation uses a TagWriter to produce the tag contents, passing it the name, parameters and parsed children (as string). * * If a parse callback is set, it relies on its implementation to return valid output given the parsed children. * Most callbacks will want to use TagWriter as well but transform the output a bit before doing so. */ public function __toString() { $sParsedChildren = ""; foreach ($this->aChildren as $mChild) { if ($mChild instanceof HtmlTag) { $sParsedChildren .= $mChild->__toString(false); } else { $sParsedChildren .= Template::htmlEncode($mChild); } } $sResult = ""; if ($this->mParseCallback === null) { $oTagWriter = new TagWriter($this->sName, $this->aParameters, $sParsedChildren); $sResult = $oTagWriter->parse(true)->render(); } else { $sResult = call_user_func($this->mParseCallback, $this, $sParsedChildren); } return $sResult; }
public function adminGetContainers() { $oTemplate = $this->oPage->getTemplate(); foreach ($oTemplate->identifiersMatching('container', Template::$ANY_VALUE) as $oIdentifier) { $oInheritedFrom = null; $sContainerName = $oIdentifier->getValue(); if (BooleanParser::booleanForString($oIdentifier->getParameter('inherit'))) { $oInheritedFrom = $this->oPage; $iInheritedObjectCount = 0; while ($iInheritedObjectCount === 0 && ($oInheritedFrom = $oInheritedFrom->getParent()) !== null) { $iInheritedObjectCount = $oInheritedFrom->countObjectsForContainer($sContainerName); } } $sInheritedFrom = $oInheritedFrom ? $oInheritedFrom->getName() : ''; $aTagParams = array('class' => 'template-container template-container-' . $sContainerName, 'data-container-name' => $sContainerName, 'data-container-string' => TranslationPeer::getString('container_name.' . $sContainerName, null, $sContainerName), 'data-inherited-from' => $sInheritedFrom); $oContainerTag = TagWriter::quickTag('ol', $aTagParams); $mInnerTemplate = new Template(TemplateIdentifier::constructIdentifier('content'), null, true); //Replace container info //…name $mInnerTemplate->replaceIdentifierMultiple('content', TagWriter::quickTag('div', array('class' => 'template-container-description'), TranslationPeer::getString('wns.page.template_container', null, null, array('container' => TranslationPeer::getString('template_container.' . $sContainerName, null, $sContainerName)), true))); //…additional info $mInnerTemplate->replaceIdentifierMultiple('content', TagWriter::quickTag('div', array('class' => 'template-container-info'))); //…tag $mInnerTemplate->replaceIdentifierMultiple('content', $oContainerTag); //Replace actual container $oTemplate->replaceIdentifier($oIdentifier, $mInnerTemplate); } $bUseParsedCss = Settings::getSetting('admin', 'use_parsed_css_in_config', true); $oStyle = null; if ($bUseParsedCss) { $sTemplateName = $this->oPage->getTemplateNameUsed() . Template::$SUFFIX; $sCacheKey = 'parsed-css-' . $sTemplateName; $oCssCache = new Cache($sCacheKey, DIRNAME_PRELOAD); $sCssContents = ""; if (!$oCssCache->entryExists() || $oCssCache->isOutdated(ResourceFinder::create(array(DIRNAME_TEMPLATES, $sTemplateName)))) { $oIncluder = new ResourceIncluder(); foreach ($oTemplate->identifiersMatching('addResourceInclude', Template::$ANY_VALUE) as $oIdentifier) { $oIncluder->addResourceFromTemplateIdentifier($oIdentifier); } foreach ($oIncluder->getAllIncludedResources() as $sIdentifier => $aResource) { if ($aResource['resource_type'] === ResourceIncluder::RESOURCE_TYPE_CSS && !isset($aResource['ie_condition']) && !isset($aResource['frontend_specific'])) { if (isset($aResource['media'])) { $sCssContents .= "@media {$aResource['media']} {"; } if (isset($aResource['file_resource'])) { $sCssContents .= file_get_contents($aResource['file_resource']->getFullPath()); } else { // Absolute link, requires fopen wrappers $sCssContents .= file_get_contents($aResource['location']); } if (isset($aResource['media'])) { $sCssContents .= "}"; } } } $oParser = new Sabberworm\CSS\Parser($sCssContents, Sabberworm\CSS\Settings::create()->withDefaultCharset(Settings::getSetting("encoding", "browser", "utf-8"))); $oCss = $oParser->parse(); $this->cleanupCSS($oCss); $sCssContents = Template::htmlEncode($oCss->render(Sabberworm\CSS\OutputFormat::createCompact())); $oCssCache->setContents($sCssContents); } else { $sCssContents = $oCssCache->getContentsAsString(); } $oStyle = new HtmlTag('style'); $oStyle->addParameters(array('scoped' => 'scoped')); $oStyle->appendChild($sCssContents); } $sTemplate = $oTemplate->render(); $sTemplate = substr($sTemplate, strpos($sTemplate, '<body') + 5); $sTemplate = substr($sTemplate, strpos($sTemplate, '>') + 1); $sTemplate = substr($sTemplate, 0, strpos($sTemplate, '</body')); $oParser = new TagParser("<body>{$sTemplate}</body>"); $oTag = $oParser->getTag(); $this->cleanupContainerStructure($oTag); if ($bUseParsedCss) { $oTag->appendChild($oStyle); } $sResult = $oTag->__toString(); $sResult = substr($sResult, strpos($sResult, '<body>') + 6); $sResult = substr($sResult, 0, strrpos($sResult, '</body>')); return array('html' => $sResult, 'css_parsed' => $bUseParsedCss); }