public function testStringWithoutHTMLAsTemplate() { $sString1 = TranslationPeer::getString('name', 'de'); $sString2 = TranslationPeer::getString('key', 'de'); $oTemplate = new Template(TemplateIdentifier::constructIdentifier('string'), null, true); $oTemplate->replaceIdentifierMultiple('string', $sString1); $oTemplate->replaceIdentifierMultiple('string', $sString2); $this->assertSame("Name\nSchlüssel\n", $oTemplate->render()); }
public function renderFrontend() { $aData = unserialize($this->getData()); $oTemplate = new Template($aData['template']); $oItemTemplatePrototype = new Template($aData['template'] . '_item'); $bItemFound = false; // FIXME: Keep track of output $oCorrespondingItems and refuse output if already done foreach ($aData['tags'] as $iTagID) { $oTag = TagQuery::create()->findPk($iTagID); if ($oTag === null) { continue; } $aCorrespondingItems = $oTag->getAllCorrespondingDataEntries($aData['types']); foreach ($aCorrespondingItems as $i => $oCorrespondingItem) { if (!method_exists($oCorrespondingItem, 'renderListItem')) { return; } if (!$oCorrespondingItem->shouldBeIncludedInList(Session::language(), FrontendManager::$CURRENT_PAGE)) { continue; } $bItemFound = true; $oItemTemplate = clone $oItemTemplatePrototype; $oItemTemplate->replaceIdentifier('model', get_class($oCorrespondingItem)); $oItemTemplate->replaceIdentifier('counter', $i + 1); $oCorrespondingItem->renderListItem($oItemTemplate); $oTemplate->replaceIdentifierMultiple("items", $oItemTemplate); } } if (!$bItemFound) { return null; } return $oTemplate; }
public function renderFrontend() { $oCriteria = DocumentQuery::create()->filterByDocumentKind('image'); if (!Session::getSession()->isAuthenticated()) { $oCriteria->filterByIsProtected(false); } if ($this->iCategoryId !== null) { $oCriteria->filterByDocumentCategoryId($this->iCategoryId); } $aDocuments = $oCriteria->find(); $sTemplateName = 'helpers/gallery'; try { $oListTemplate = new Template($sTemplateName); foreach ($aDocuments as $i => $oDocument) { $oItemTemplate = new Template($sTemplateName . DocumentListFrontendModule::LIST_ITEM_POSTFIX); $oItemTemplate->replaceIdentifier('model', 'Document'); $oItemTemplate->replaceIdentifier('counter', $i + 1); $oDocument->renderListItem($oItemTemplate); $oListTemplate->replaceIdentifierMultiple('items', $oItemTemplate); } } catch (Exception $e) { $oListTemplate = new Template("", null, true); } return $oListTemplate; }
public function testDirectOutputMultiple() { $sTemplateText = <<<EOT test{{id}}test2 EOT; $this->expectOutputString('test string'); $oTemplate = new Template($sTemplateText, null, true, true); $oTemplate->replaceIdentifierMultiple('id', ' string', null, Template::NO_NEWLINE); }
private function content(Template $oTemplate, $sWidgetName) { $sWidgetClass = WidgetModule::getClassNameByName($sWidgetName); if (is_callable(array($sWidgetClass, 'testWidget'))) { $oWidget = $sWidgetClass::testWidget(); } else { $oWidget = WidgetModule::getWidget($sWidgetName, null); } $oTemplate->replaceIdentifierMultiple('main_content', $oWidget->doWidget()); }
public function display(Template $oTemplate, $bIsPreview = false) { $sTemplateName = $this->oPage->getTemplateNameUsed(); $sLanguageId = Session::language(); $oListTemplate = null; $oItemTemplatePrototype = null; try { $oListTemplate = new Template("search_results/{$sTemplateName}"); $oItemTemplatePrototype = new Template("search_results/{$sTemplateName}_item"); } catch (Exception $e) { $oListTemplate = new Template("search_results/default"); $oItemTemplatePrototype = new Template("search_results/default_item"); } $aResults = array(); $sWords = isset($_REQUEST['q']) ? $_REQUEST['q'] : ''; if ($sWords) { $aWords = StringUtil::getWords($sWords, false, '%'); $oSearchWordQuery = SearchIndexWordQuery::create(); foreach ($aWords as $sWord) { $sWord = Synonyms::rootFor($sWord); $sComparison = Criteria::EQUAL; if (strpos($sWord, '%') !== false) { $sComparison = Criteria::LIKE; } $oSearchWordQuery->addOr(SearchIndexWordPeer::WORD, $sWord, $sComparison); } $oSearchWordQuery->joinSearchIndex()->useQuery('SearchIndex')->joinPage()->useQuery('Page')->active(true)->filterByIsProtected(false)->endUse()->endUse(); foreach ($oSearchWordQuery->find() as $oSearchIndexWord) { $iId = $oSearchIndexWord->getSearchIndexId(); if (isset($aResults[$iId])) { $aResults[$iId] += $oSearchIndexWord->getCount(); } else { $aResults[$iId] = $oSearchIndexWord->getCount(); } } arsort($aResults); } $oListTemplate->replaceIdentifier('count', count($aResults)); $oListTemplate->replaceIdentifier('search_string', $sWords); if (count($aResults) === 0) { $oListTemplate->replaceIdentifier('no_results', TranslationPeer::getString('wns.search.no_results', null, null, array('search_string' => $sWords))); } foreach ($aResults as $iIndexId => $iCount) { $oIndex = SearchIndexQuery::create()->findPk(array($iIndexId, $sLanguageId)); if (!$oIndex || !$oIndex->getPage()) { continue; } $oItemTemplate = clone $oItemTemplatePrototype; $oIndex->renderListItem($oItemTemplate); $oItemTemplate->replaceIdentifier('count', $iCount); $oListTemplate->replaceIdentifierMultiple('items', $oItemTemplate); } $oTemplate->replaceIdentifier('search_results', $oListTemplate); }
public function renderFrontend() { $aOptions = @unserialize($this->getData()); try { $oListTemplate = new Template($aOptions['list_template']); $oItemPrototype = new Template($aOptions['list_template'] . self::LIST_ITEM_POSTFIX); foreach (self::listQuery($aOptions)->find() as $i => $oDocument) { $oItemTemplate = clone $oItemPrototype; $oDocument->renderListItem($oItemTemplate); $oListTemplate->replaceIdentifierMultiple('items', $oItemTemplate); } } catch (Exception $e) { $oListTemplate = new Template("", null, true); } return $oListTemplate; }
public function renderFile() { $iTemplateFlags = 0; $oResourceFinder = ResourceFinder::create(array(DIRNAME_MODULES, $this->sModuleType, $this->sModuleName, DIRNAME_TEMPLATES))->returnObjects(); $sFileName = "{$this->sModuleName}.{$this->sModuleType}.{$this->sResourceType}"; $oResourceFinder->addPath($sFileName . Template::$SUFFIX); if ($this->sResourceType === ResourceIncluder::RESOURCE_TYPE_CSS) { header("Content-Type: text/css;charset=utf-8"); $oResourceFinder->all(); } else { if ($this->sResourceType === ResourceIncluder::RESOURCE_TYPE_JS) { header("Content-Type: text/javascript;charset=utf-8"); $iTemplateFlags = Template::ESCAPE | Template::NO_HTML_ESCAPE; } else { header("Content-Type: text/html;charset=utf-8"); } } $oCache = new Cache('template_resource-' . $sFileName . '-' . Session::language(), 'resource'); $oTemplate = null; if ($oCache->entryExists() && !$oCache->isOutdated($oResourceFinder)) { $oCache->sendCacheControlHeaders(); $oTemplate = $oCache->getContentsAsVariable(); } else { $oTemplate = new Template(TemplateIdentifier::constructIdentifier('contents'), null, true, false, null, $sFileName); $aResources = $oResourceFinder->find(); if (!$aResources) { $aResources = array(); } if ($aResources instanceof FileResource) { $aResources = array($aResources); } foreach ($aResources as $oResource) { $oSubTemplate = new Template($oResource, null, false, false, null, null, $iTemplateFlags); $oTemplate->replaceIdentifierMultiple('contents', $oSubTemplate, null, Template::LEAVE_IDENTIFIERS); } $oCache->setContents($oTemplate); $oCache->sendCacheControlHeaders(); } print $oTemplate->render(); }
/** * Returns a Template containing all of the necessary HTML code for the browser to load the included resources. * @param $bPrintNewlines Whether to put each include on a new line. Turn this off for “location_only”-type includes. * @param $bConsolidate Whether to consolidate CSS and JS includes into a single tag which will point to a new location which will serve all the scripts of one type concatenated. * Valid values — true: Consolidate all css/js resources, false: Don’t consolidate, 'internal': Only consolidate internal scripts, but not the ones loaded from external servers, null: Use the default value from the general/consolidate_resources configuration seting from resource_includer.yml. * Note that all concatenated scripts will have to be in the same charset, namely the one defined in the encoding/browser configuration setting. * Also note that a value of "internal" for $bConsolidate will only have an effect on js libraries if they’re not being locally cached (use_local_library_cache is false) */ public function getIncludes($bPrintNewlines = true, $bConsolidate = null) { $bConsolidateSetting = Settings::getSetting('general', 'consolidate_resources', false, 'resource_includer'); if ($bConsolidate === null) { $bConsolidate = $bConsolidateSetting; } if ($bConsolidate === 'never' || $bConsolidateSetting === 'never') { //In the “never” case, $bConsolidateSetting overrides a local $bConsolidate $bConsolidate = false; } if ($bConsolidate && !ini_get('allow_url_fopen')) { // Never consolidate external files if fopen_wrappers are disabled $bConsolidate = 'internal'; } if ($bConsolidate) { $this->replaceContentsWithConsolidated($bConsolidate === 'internal'); } $this->cleanupReverseDependencies(); $iTemplateFlags = 0; if (!$bPrintNewlines) { $iTemplateFlags = Template::NO_NEWLINE; } $oTemplate = new Template(TemplateIdentifier::constructIdentifier('includes'), null, true, false, null, null, $iTemplateFlags); $aTemplateMasters = array(); foreach ($this->aIncludedResources as $iPriority => $aIncludedResourcesOfType) { if (count($aIncludedResourcesOfType) === 0) { continue; } foreach ($aIncludedResourcesOfType as $aResourceInfo) { if (!isset($aTemplateMasters[$aResourceInfo['template']])) { $aTemplateMasters[$aResourceInfo['template']] = new Template($aResourceInfo['template'], array(DIRNAME_TEMPLATES, 'resource_includers')); } $oIncludeTemplate = clone $aTemplateMasters[$aResourceInfo['template']]; foreach ($aResourceInfo as $sResourceInfoKey => $sResourceInfoValue) { $oIncludeTemplate->replaceIdentifier($sResourceInfoKey, $sResourceInfoValue); } if (isset($aResourceInfo['ie_condition'])) { $oIeConditionalTemplate = $this->ieConditionalTemplate(); $oIeConditionalTemplate->replaceIdentifier('condition', $aResourceInfo['ie_condition']); $oIeConditionalTemplate->replaceIdentifier('content', $oIncludeTemplate); $oIncludeTemplate = $oIeConditionalTemplate; } $oTemplate->replaceIdentifierMultiple('includes', $oIncludeTemplate); } } return $oTemplate; }
public function testCombinedInlineFlags() { $sTemplateText = <<<EOT {{test;templateFlag=LEAVE_IDENTIFIERS|ESCAPE}} EOT; $oTemplate = new Template($sTemplateText, null, true, false, null, null, Template::NO_NEWLINE); $oTemplate->replaceIdentifierMultiple('test', new Template('"{{test2}}"', null, true)); $oTemplate->replaceIdentifierMultiple('test2', 1); $this->assertSame('\\"1\\"', $oTemplate->render()); }
private static function compileSchemaXml() { $sSchemaTemplate = <<<EOT <?xml version="1.0" encoding="UTF-8"?> <!-- {{comment}} --> <database name="{{name}}" defaultIdMethod="native"> \t{{schema_content}} </database> EOT; foreach (self::groupedSchemaXml() as $sSchemaName => $aSchemas) { $oSchemaTemplate = new Template($sSchemaTemplate, null, true); $oSchemaTemplate->replaceIdentifier('comment', "This file is generated by the generate-model.sh script, edit schema.xml in the config dir or the plugins or site's schema.xml file instead", null, Template::NO_HTML_ESCAPE); $oSchemaTemplate->replaceIdentifier('name', $sSchemaName); foreach ($aSchemas as $oSchemaFile) { $oSchemaTemplate->replaceIdentifierMultiple('schema_content', file_get_contents($oSchemaFile->getFullPath()), null, Template::NO_HTML_ESCAPE); } $sSchemaOutputPath = MAIN_DIR . '/' . DIRNAME_GENERATED . "/{$sSchemaName}.schema.xml"; file_put_contents($sSchemaOutputPath, $oSchemaTemplate->render()); } }
/** * renderGallery() * * description: display image gallery * @return Template object */ private function renderGallery(JournalEntry $oEntry) { $oEntryTemplate = $this->constructTemplate('journal_gallery'); $oListTemplate = new Template('helpers/gallery'); $oListTemplate->replaceIdentifier('title', $this->oEntry->getTitle()); foreach ($this->oEntry->getImages() as $oJournalEntryImage) { $oDocument = $oJournalEntryImage->getDocument(); $oItemTemplate = new Template('helpers/gallery_item'); $oItemTemplate->replaceIdentifier('jounal_entry_id', $this->oEntry->getId()); $oDocument->renderListItem($oItemTemplate); $oListTemplate->replaceIdentifierMultiple('items', $oItemTemplate); } $oEntryTemplate->replaceIdentifier('gallery', $oListTemplate); return $oEntryTemplate; }
public function testContextsWithNamespaces() { $sTemplateText = <<<EOT {{identifierContext=start;name=id}}{{id.help}}: {{id.kill}}{{identifierContext=end;name=id}} EOT; $oTemplate = new Template($sTemplateText, null, true); $oTemplate->replaceIdentifierMultiple('id', array('help' => 'me', 'kill' => 'no-one')); $oTemplate->replaceIdentifierMultiple('id', array('help' => 'yourself', 'kill' => 'everyone')); $this->assertSame(<<<EOT me: no-one yourself: everyone EOT , $oTemplate->render()); }
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); }
public function testReplaceIdentifierMultipleNoNewContext1() { $sTemplateText = <<<EOT {{identifierContext=start;name=test}}{{test}} GAGA{{identifierContext=end;name=test}} EOT; $oTemplate = new Template($sTemplateText, null, true); $oTemplate->setDefaultFlags(Template::NO_NEWLINE | Template::NO_NEW_CONTEXT); $oTemplate->replaceIdentifierMultiple('test', 'string'); $oTemplate->replaceIdentifierMultiple('test', 1); $oTemplate->replaceIdentifierMultiple('test', true); $oTemplate->replaceIdentifierMultiple('test', 0xff); $oTemplate->replaceIdentifierMultiple('test', array("list", "item")); $this->assertSame("string1true255listitem GAGA", $oTemplate->render()); }
public static function listItemsFromArray($aArray) { if (count($aArray) === 0) { return ""; } $oTemplate = new Template(TemplateIdentifier::constructIdentifier('result'), null, true); foreach ($aArray as $sListItem) { $oOptionTemplate = new Template('<li>' . TemplateIdentifier::constructIdentifier('value') . '</li>', null, true); $oOptionTemplate->replaceIdentifier("value", $sListItem); $oTemplate->replaceIdentifierMultiple("result", $oOptionTemplate); } return $oTemplate; }
/** * @param main template * description: * - @see config.yml section language_chooser * - use parameter replaced in method * @return Template The rendered language chooser */ public static function getLanguageChooser($oMainTemplate) { $oTemplate = new Template(TemplateIdentifier::constructIdentifier('languages'), null, true); $oLanguageTemplate = new Template(Settings::getSetting("language_chooser", 'template', 'language'), array(DIRNAME_TEMPLATES, DIRNAME_NAVIGATION)); $sLinkSeparator = Settings::getSetting("language_chooser", 'link_separator', ' | '); $oLanguageActiveTemplate = null; $bShowActiveLanguage = Settings::getSetting("language_chooser", 'show_active_language', false); $bIsPreview = Manager::getCurrentManager() instanceof PreviewManager; if ($bShowActiveLanguage) { if (Settings::getSetting("language_chooser", 'template_active', false) !== false) { $oLanguageActiveTemplate = new Template(Settings::getSetting("language_chooser", 'template_active', 'language_active'), array(DIRNAME_TEMPLATES, DIRNAME_NAVIGATION)); } else { $oLanguageActiveTemplate = clone $oLanguageTemplate; } } // Find request variables $aParameters = array_diff_assoc($_REQUEST, $_COOKIE); unset($aParameters['path']); unset($aParameters['content_language']); // Check whether manager needs language to be included $bCurrentPathIncludesLanguage = call_user_func(array(Manager::getManagerClassNormalized(null), 'shouldIncludeLanguageInLink')); $aRequestPath = explode("/", Manager::getRequestedPath()); $aLanguages = LanguageQuery::create()->filterByIsActive(true)->exclude($bShowActiveLanguage ? false : ($bIsPreview ? 'edit' : 'current'))->orderBySort()->find(); foreach ($aLanguages as $i => $oLanguage) { $oLangTemplate = null; $oPageString = null; if ($oLanguage->getId() === Session::language()) { $oLangTemplate = $oLanguageActiveTemplate; $oLangTemplate->replaceIdentifier('class', 'active'); } else { $oPageString = PageStringQuery::create()->filterByPage(FrontendManager::$CURRENT_PAGE)->filterByLanguageId($oLanguage->getId())->filterByIsInactive(false)->findOne(); if ($oPageString === null) { continue; } $oLangTemplate = clone $oLanguageTemplate; } // If language is included, replace it by language id and set include_language param to false if ($bCurrentPathIncludesLanguage) { $aRequestPath[0] = $oLanguage->getPathPrefix(); $sLink = LinkUtil::link($aRequestPath, null, $aParameters, false); } else { $sLink = LinkUtil::link($aRequestPath, null, array_merge($aParameters, array('content_language' => $oLanguage->getId()))); } $oLangTemplate->replaceIdentifier('link', $sLink); // Add alternate language links if ($oPageString) { ResourceIncluder::metaIncluder()->addCustomResource(array('template' => 'link', 'rel' => 'alternate', 'lang' => $oLanguage->getId(), 'location' => $sLink, 'title' => $oPageString->getPageTitle())); } $oLangTemplate->replaceIdentifier('id', $oLanguage->getId()); $oLangTemplate->replaceIdentifier('name', $oLanguage->getLanguageName($oLanguage->getId())); $oLangTemplate->replaceIdentifier('name_in_current_lang', $oLanguage->getLanguageName()); $oTemplate->replaceIdentifierMultiple('languages', $oLangTemplate, null, Template::NO_NEWLINE); if ($i + 1 < count($aLanguages)) { $oTemplate->replaceIdentifierMultiple('languages', $sLinkSeparator, null, Template::NO_HTML_ESCAPE | Template::NO_NEWLINE); } } return $oTemplate; }