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 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); }