/** * A special method that belongs to the LoadSubTplTag class but needs none * static properties from this class and is called from the cached template * files. * @param string $file The full filepath to include (OR magic {this}) * @param TemplateEngine $tplEngine */ public static function requireFile($file, TemplateEngine $tplEngine) { $tplPath = explode(DIRECTORY_SEPARATOR, $tplEngine->getCurrentTemplateFile()); array_pop($tplPath); $tplPathStr = implode(DIRECTORY_SEPARATOR, $tplPath) . DIRECTORY_SEPARATOR; echo $tplEngine->getResultAsHtml($tplPathStr . $file, (array) $tplEngine->getAllData()); }
public static function render($name, $chosenSelector, $poolSelector, TemplateEngine $tplEngine) { $chosenEntries = $tplEngine->getDataFromSelector($chosenSelector); $poolEntries = array(); if ($poolSelector !== null) { $poolEntries = $tplEngine->getDataFromSelector($poolSelector); } $html = '<div class="add-remove" name="' . $name . '">'; // Choosen $html .= '<ul class="option-list chosen">'; foreach ($chosenEntries as $id => $title) { $html .= '<li id=\\"" . $name . "-' . $id . '\\">' . $title . '</li>'; } $html .= '</ul>'; if (count($poolEntries) > 0) { // left or right $html .= '<div class="between"> <a href="#" class="entries-add" title="add selected entries">←</a> <br> <a href="#" class="entries-remove" title="remove selected entries">→</a> </div>'; // Pool $html .= '<ul class="option-list pool">'; foreach ($poolEntries as $id => $title) { $html .= '<li id=\\"" . $name . "-' . $id . '\\">' . $title . '</li>'; } $html .= '</ul>'; } $html .= '</div>'; return $html; }
public function replaceNode(TemplateEngine $tplEngine, ElementNode $tagNode) { $format = $tagNode->getAttribute('format')->value; $replNode = new TextNode($tplEngine->getDomReader()); $replNode->content = '<?php echo date(\'' . $format . '\'); ?>'; $tagNode->parentNode->replaceNode($tagNode, $replNode); }
public static function render(TemplateEngine $tplEngine, $optionsSelector, $selectedSelector) { $options = $tplEngine->getDataFromSelector($optionsSelector); $selection = array(); if ($selectedSelector !== null) { $selection = (array) $tplEngine->getDataFromSelector($selectedSelector); } return self::renderOptions($options, $selection); }
public static function generateOutput(TemplateEngine $templateEngine, $selector) { $data = $templateEngine->getDataFromSelector($selector); if ($data instanceof \DateTime) { return $data->format('Y-m-d H:i:s'); } elseif (is_scalar($data) === false) { return print_r($data, true); } return $data; }
public static function getText(TemplateEngine $tplEngine, $key, $domain = null) { if (($translator = $tplEngine->getData('translator')) instanceof AbstractTranslator === false) { return $key; } /** @var AbstractTranslator $translator */ if ($domain === null) { return $translator->_($key); } return $translator->_d($domain, $key); }
public function replaceNode(TemplateEngine $tplEngine, ElementNode $node) { // DATA $value = $node->getAttribute('href')->value; $node->removeAttribute('href'); $node->namespace = null; $node->tagName = 'link'; $cssRevisionData = $tplEngine->getData('css_revision'); $cssRevision = isset($cssRevisionData) ? '?' . $cssRevisionData : null; $node->addAttribute(new HtmlAttribute('href', $value . $cssRevision)); }
public static function render(TemplateEngine $tplEngine, $fldName, $optionsSelector, $checkedSelector) { $options = $tplEngine->getDataFromSelector($optionsSelector); $selection = (array) $tplEngine->getDataFromSelector($checkedSelector); $html = '<ul>'; foreach ($options as $key => $val) { $checked = in_array($key, $selection) ? ' checked' : null; $html .= '<li><label><input type="radio" value="' . $key . '" name="' . $fldName . '"' . $checked . '> ' . $val . '</label></li>' . "\n"; } $html .= '</ul>'; return $html; }
public function replaceNode(TemplateEngine $tplEngine, ElementNode $node) { $var = $node->getAttribute('var')->value; $entryNoArr = explode(':', $var); $this->no = $entryNoArr[0]; $this->var = $entryNoArr[1]; $tplEngine->checkRequiredAttrs($node, array('var')); $replNode = new TextNode($tplEngine->getDomReader()); $varName = $this->var . $this->no; $replNode->content = "<?php \$tmpGrpVal = \$this->getDataFromSelector('{$varName}', true);\n"; $replNode->content .= " if(\$tmpGrpVal !== null) {\n"; $replNode->content .= "\$this->addData('{$this->var}', \$tmpGrpVal, true); ?>"; $replNode->content .= self::prepareHtml($node->getInnerHtml()); $replNode->content .= "<?php } ?>"; $node->getParentNode()->replaceNode($node, $replNode); }
public function replaceNode(TemplateEngine $tplEngine, ElementNode $tagNode) { $lastTplTag = $tplEngine->getLastTplTag(); if ($lastTplTag === null) { throw new TemplateEngineException('There is no custom tag that can be followed by an ElseTag'); } /*if($lastTplTag->isElseable() === false) throw new TemplateEngineException('The custom tag "' . get_class($lastTplTag) . '" can not be followed by an ElseTag');*/ $phpCode = '<?php else: ?>'; $phpCode .= $tagNode->getInnerHtml(); $phpCode .= '<?php endif; ?>'; $textNode = new TextNode($tplEngine->getDomReader()); $textNode->content = $phpCode; $tagNode->parentNode->replaceNode($tagNode, $textNode); $tagNode->parentNode->removeNode($tagNode); }
public function replaceNode(TemplateEngine $tplEngine, ElementNode $tagNode) { $tplEngine->checkRequiredAttrs($tagNode, 'cond'); $condAttr = $tagNode->getAttribute('cond')->value; $phpCode = '<?php '; $phpCode .= 'elseif(' . preg_replace_callback('/\\${(.*?)}/i', function ($m) { if (strlen($m[1]) === 0) { throw new TemplateEngineException('Empty template data reference'); } return '$this->getDataFromSelector(\'' . $m[1] . '\')'; }, $condAttr) . '): ?>'; $phpCode .= $tagNode->getInnerHtml(); if ($tplEngine->isFollowedBy($tagNode, array('else', 'elseif')) === false) { $phpCode .= '<?php endif; ?>'; } $textNode = new TextNode($tplEngine->getDomReader()); $textNode->content = $phpCode; $tagNode->parentNode->replaceNode($tagNode, $textNode); $tagNode->parentNode->removeNode($tagNode); }
public static function render($formSelector, $componentName, TemplateEngine $tplEngine) { $callback = array($tplEngine->getDataFromSelector($formSelector), 'getComponent'); $component = call_user_func($callback, $componentName); return call_user_func(array($component, 'render')); }
public function replaceNode(TemplateEngine $tplEngine, ElementNode $tagNode) { $compareAttr = $tagNode->getAttribute('compare')->value; $operatorAttr = $tagNode->getAttribute('operator')->value; $againstAttr = $tagNode->getAttribute('against')->value; $condAttr = $tagNode->getAttribute('cond')->value; if ($condAttr === null) { // Check required attrs $tplEngine->checkRequiredAttrs($tagNode, array('compare', 'operator', 'against')); if (strlen($againstAttr) === 0) { $againstAttr = "''"; } elseif (is_int($againstAttr) === true) { $againstAttr = intval($againstAttr); } elseif (is_float($againstAttr) === true) { $againstAttr = floatval($againstAttr); } elseif (is_string($againstAttr) === true) { if (strtolower($againstAttr) === 'null') { //$againstAttr = 'null'; } elseif (strtolower($againstAttr) === 'true' || strtolower($againstAttr) === 'false') { //$againstAttr = ($againstAttr === 'true')?true:false; } elseif (StringUtils::startsWith($againstAttr, '{') && StringUtils::endsWith($againstAttr, '}')) { $arr = substr(explode(',', $againstAttr), 1, -1); $againstAttr = array(); foreach ($arr as $a) { $againstAttr[] = trim($a); } } else { $againstAttr = "'" . $againstAttr . "'"; } } $operatorStr = '=='; switch (strtolower($operatorAttr)) { case 'gt': $operatorStr = '>'; break; case 'ge': $operatorStr = '>='; break; case 'lt': $operatorStr = '<'; break; case 'le': $operatorStr = '<='; break; case 'eq': $operatorStr = '=='; break; case 'ne': $operatorStr = '!='; break; } $phpCode = '<?php '; $phpCode .= 'if($this->getDataFromSelector(\'' . $compareAttr . '\') ' . $operatorStr . ' ' . $againstAttr . '): ?>'; $phpCode .= $tagNode->getInnerHtml(); if ($tplEngine->isFollowedBy($tagNode, array('else', 'elseif')) === false) { $phpCode .= '<?php endif; ?>'; } $textNode = new TextNode($tplEngine->getDomReader()); $textNode->content = $phpCode; $tagNode->parentNode->replaceNode($tagNode, $textNode); $tagNode->parentNode->removeNode($tagNode); } else { $phpCode = '<?php '; $phpCode .= 'if(' . preg_replace_callback('/\\${(.*?)}/i', function ($m) { if (strlen($m[1]) === 0) { throw new TemplateEngineException('Empty template data reference'); } return '$this->getDataFromSelector(\'' . $m[1] . '\')'; }, $condAttr) . '): ?>'; $phpCode .= $tagNode->getInnerHtml(); if ($tplEngine->isFollowedBy($tagNode, array('else', 'elseif')) === false) { $phpCode .= '<?php endif; ?>'; } $textNode = new TextNode($tplEngine->getDomReader()); $textNode->content = $phpCode; $tagNode->parentNode->replaceNode($tagNode, $textNode); $tagNode->parentNode->removeNode($tagNode); } }
public function replaceNode(TemplateEngine $tplEngine, ElementNode $node) { // DATA $tplEngine->checkRequiredAttrs($node, array('var', 'as')); $dataKeyAttr = $node->getAttribute('var')->value; $asVarAttr = $node->getAttribute('as')->value; $keyVarAttr = $node->getAttribute('key')->value; $counterAttr = $node->getAttribute('counter')->value; $oddEvenAttr = $node->getAttribute('odd-even')->value; $firstLastAttr = $node->getAttribute('first-last')->value; $stepIncrement = $node->getAttribute('step')->value; $grabCount = $node->getAttribute('grab')->value; if ($stepIncrement === null && $grabCount !== null) { $stepIncrement = $grabCount; } elseif ($stepIncrement === null) { $stepIncrement = 1; } if ($stepIncrement == 0) { throw new TemplateEngineException('Use a step value other than 0. This will end up in an endless loop'); } $for_i = '$for_i_' . $asVarAttr; $for_key = '$for_key_' . $asVarAttr; $for_val = '$for_val_' . $asVarAttr; $for_data = '$for_data_' . $asVarAttr; $for_data_count = '$for_data_count_' . $asVarAttr; $phpCode = '<?php ' . $for_data . ' = $this->getDataFromSelector(\'' . $dataKeyAttr . '\'); ' . $for_data_count . ' = count(' . $for_data . '); ' . $for_i . ' = 0; '; if ($tplEngine->isFollowedBy($node, 'else') === true) { $phpCode .= 'if(' . $for_data_count . ' > 0): '; } $phpCode .= 'for(' . $for_val . ' = current(' . $for_data . '), ' . $for_key . ' = key(' . $for_data . '); ' . $for_val . '; ' . $for_val . ' = next(' . $for_data . '), ' . $for_key . ' = key(' . $for_data . '), ' . $for_i . ' += ' . $stepIncrement . '): ' . ($counterAttr !== null ? '$this->addData(\'' . $counterAttr . '\', ' . $for_i . ', true);' : null) . ' ' . ($keyVarAttr !== null ? '$this->addData(\'' . $keyVarAttr . '\', ' . $for_key . ', true);' : null); if ($grabCount === null || $grabCount == 1) { $phpCode .= '$this->addData(\'' . $asVarAttr . '\', ' . $for_val . ', true);'; } else { $phpCode .= '$tmpGrabGroup = array( key(' . $for_data . ') => current(' . $for_data . ') ); for($i = 2; $i <= ' . $grabCount . '; ++$i) { if(($tmpNextEntry = next(' . $for_data . ')) === false) break; $tmpGrabGroup[key(' . $for_data . ')] = $tmpNextEntry; } $this->addData(\'' . $asVarAttr . '\', $tmpGrabGroup, true);'; } $phpCode .= ($oddEvenAttr !== null ? '$this->addData(\'' . $oddEvenAttr . '\', (((' . $for_i . '/' . $stepIncrement . ')%2 === 0)?\'odd\':\'even\'), true);' : null) . ' ' . ($firstLastAttr !== null ? '$this->addData(\'' . $firstLastAttr . '\', ((' . $for_i . ' === 0)?\'first\':(((' . $for_i . '/' . $stepIncrement . ') === ' . $for_data_count . '-1)?\'last\':null)), true);' : null) . ' ?> ' . $node->getInnerHtml() . ' <?php endfor; ?>'; $newNode = new TextNode($tplEngine->getDomReader()); $newNode->content = $phpCode; $node->parentNode->replaceNode($node, $newNode); }
/** * @param TemplateCacheStrategy $tplCacheInterface The template cache strategy * @param string $tplNsPrefix The prefix for custom tags in the template file */ public function __construct(TemplateCacheStrategy $tplCacheInterface, $tplNsPrefix) { parent::__construct($tplCacheInterface, $tplNsPrefix, array('formComponent' => '\\ch\\metanet\\customtags\\FormComponentTag', 'formAddRemove' => '\\ch\\metanet\\customtags\\FormAddRemoveTag', 'cssFile' => '\\ch\\metanet\\customtags\\CssFileTag', 'translate' => '\\ch\\metanet\\customtags\\TranslateTag')); }