Example #1
0
 protected function doEchoAttribute(PHPTAL_Php_CodeWriter $codewriter, $code)
 {
     if ($this->_echoType === self::ECHO_TEXT) {
         $codewriter->doEcho($code);
     } else {
         $codewriter->doEchoRaw($code);
     }
 }
Example #2
0
 public function generateCode(PHPTAL_Php_CodeWriter $codewriter)
 {
     if ($codewriter->getOutputMode() === PHPTAL::HTML5) {
         $codewriter->setDocType('<!DOCTYPE html>');
     } else {
         $codewriter->setDocType($this->getValueEscaped());
     }
     $codewriter->doDoctype();
 }
Example #3
0
 public function generateCode(PHPTAL_Php_CodeWriter $codewriter)
 {
     if (preg_match('/^<\\?(?:php|[=\\s])/i', $this->getValueEscaped())) {
         // block will be executed as PHP
         $codewriter->pushHTML($this->getValueEscaped());
     } else {
         $codewriter->doEchoRaw("'<'");
         $codewriter->pushHTML(substr($codewriter->interpolateHTML($this->getValueEscaped()), 1));
     }
 }
Example #4
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $codewriter->doEval('file_put_contents(' . $codewriter->str($codewriter->getCacheFilesBaseName()) . '.md5(' . $this->cache_tag . '), ob_get_flush())');
     $codewriter->doElse();
     $codewriter->doEval('readfile(' . $codewriter->str($codewriter->getCacheFilesBaseName()) . '.md5(' . $this->cache_tag . '))');
     $codewriter->doEnd('if');
 }
Example #5
0
 public function before(PHPTAL_Php_CodeWriter $codewriter)
 {
     $expression = $this->extractEchoType($this->expression);
     $code = $codewriter->evaluateExpression($expression);
     if (is_array($code)) {
         return $this->generateChainedContent($codewriter, $code);
     }
     if ($code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) {
         return;
     }
     if ($code == PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD) {
         return $this->generateDefault($codewriter);
     }
     $this->doEchoAttribute($codewriter, $code);
 }
Example #6
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $codewriter->doEval('file_put_contents(' . $this->cache_filename_var . ', ob_get_flush())');
     $codewriter->doElse();
     $codewriter->doEval('readfile(' . $this->cache_filename_var . ')');
     $codewriter->doEnd('if');
     $codewriter->recycleTempVariable($this->cache_filename_var);
 }
Example #7
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     if ($this->function_name !== null) {
         $codewriter->doEnd();
         $codewriter->pushCode('$ctx->fillSlotCallback(' . $codewriter->str($this->expression) . ', ' . $codewriter->str($this->function_name) . ', $_thistpl, clone $tpl)');
     } else {
         $codewriter->pushCode('$ctx->fillSlot(' . $codewriter->str($this->expression) . ', ob_get_clean())');
     }
 }
    function testFunctionsGeneration()
    {
        $state = new PHPTAL_Php_State($this->newPHPTAL());
        $codewriter = new PHPTAL_Php_CodeWriter($state);
        $codewriter->doFunction('test1', '$tpl');
        $codewriter->pushHTML($codewriter->interpolateHTML('test1'));
        $codewriter->doFunction('test2', '$tpl');
        $codewriter->pushHTML('test2');
        $codewriter->doEnd();
        $codewriter->pushHTML('test1');
        $codewriter->doEnd();
        $res = $codewriter->getResult();
        $exp = <<<EOS
<?php function test2(\$tpl) {?>test2<?php}?>
<?php function test1(\$tpl) {?>test1test1<?php}?>
EOS;
        $res = normalize_phpsource($res, true);
        $exp = normalize_phpsource($exp, true);
        $this->assertEquals($exp, $res);
    }
Example #9
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     // end of if PROCEED
     $codewriter->doEnd();
     // if trigger found, notify the end of the node
     $codewriter->doIf($this->var);
     $codewriter->pushCode($this->var . '->end(' . $codewriter->str($this->expression) . ', $tpl)');
     $codewriter->doEnd();
     $codewriter->recycleTempVariable($this->var);
 }
Example #10
0
 public function generateCode(PHPTAL_Php_CodeWriter $codewriter)
 {
     $mode = $codewriter->getOutputMode();
     $value = $this->getValueEscaped();
     $inCDATAelement = PHPTAL_Dom_Defs::getInstance()->isCDATAElementInHTML($this->parentNode->getNamespaceURI(), $this->parentNode->getLocalName());
     // in HTML5 must limit it to <script> and <style>
     if ($mode === PHPTAL::HTML5 && $inCDATAelement) {
         $codewriter->pushHTML($codewriter->interpolateCDATA(str_replace('</', '<\\/', $value)));
     } elseif ($mode === PHPTAL::XHTML && $inCDATAelement || $mode === PHPTAL::XML && preg_match('/[<>&]/', $value) || $mode !== PHPTAL::HTML5 && preg_match('/<\\?|\\${structure/', $value)) {
         // in text/html "</" is dangerous and the only sensible way to escape is ECMAScript string escapes.
         if ($mode === PHPTAL::XHTML) {
             $value = str_replace('</', '<\\/', $value);
         }
         $codewriter->pushHTML($codewriter->interpolateCDATA('<![CDATA[' . $value . ']]>'));
     } else {
         $codewriter->pushHTML($codewriter->interpolateHTML(htmlspecialchars($value, ENT_QUOTES, $codewriter->getEncoding())));
     }
 }
Example #11
0
 public function generateCode(PHPTAL_Dom_Element $treeGen)
 {
     $codewriter = new PHPTAL_Php_CodeWriter($this->_state);
     $codewriter->doComment("\n*** DO NOT EDIT THIS FILE ***\n\nGenerated by PHPTAL from " . $this->_sourceFile . " (edit that file instead)");
     $codewriter->doFunction($this->_functionName, '$tpl, $ctx');
     $codewriter->setFunctionPrefix($this->_functionName . "_");
     $codewriter->doSetVar('$_thistpl', '$tpl');
     $codewriter->doSetVar('$_translator', '$tpl->getTranslator()');
     $treeGen->generateCode($codewriter);
     $codewriter->doEnd();
     return $codewriter->getResult();
 }
Example #12
0
 public function before(PHPTAL_Php_CodeWriter $codewriter)
 {
     // tal:replace="" => do nothing and ignore node
     if (trim($this->expression) == "") {
         return;
     }
     $expression = $this->extractEchoType($this->expression);
     $code = $codewriter->evaluateExpression($expression);
     // chained expression
     if (is_array($code)) {
         return $this->replaceByChainedExpression($codewriter, $code);
     }
     // nothing do nothing
     if ($code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) {
         return;
     }
     // default generate default tag content
     if ($code == PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD) {
         return $this->generateDefault($codewriter);
     }
     // replace tag with result of expression
     $this->doEchoAttribute($codewriter, $code);
 }
Example #13
0
    /**
     * Attach html5 data attributes to element.
     *
     * Called before generating the compiled php for an attribute.
     *
     * Example (where 'element' is a Zend_Form_Element):
     *
     * <input ztal:data-attributes="element" />
     *
     * Within a Zend_Form implementation:
     *
     * $this->addElement('text', 'example', array(
     * 		'data' => array(
     * 			'hike' => 'foo',
     * 			'bar' => '1337',
     * 		)
     * ));
     *
     * @param PHPTAL_Php_CodeWriter $codewriter The code writer.
     *
     * @return void
     */
    public function before(\PHPTAL_Php_CodeWriter $codewriter)
    {
        $args = explode(',', $this->expression);
        $zendFormElement = $codewriter->evaluateExpression(trim($args[0]));
        $dataAttributes = $codewriter->createTempVariable();
        $tmp = $codewriter->createTempVariable();
        $assignment = $tmp . ' = ' . $zendFormElement . ';' . PHP_EOL;
        $attributes = '$attributes = ' . $tmp . '->getAttribs();';
        if (count($args) > 1) {
            $option = $codewriter->evaluateExpression(trim($args[1]));
            $optionVar = $codewriter->createTempVariable();
            $assignment .= $optionVar . ' = ' . $option . ';' . PHP_EOL;
            $attributes = '$attributes = ' . $tmp . '->getAttribsForOption(' . $optionVar . ');';
        }
        // PHPTAL changed the method signature for phptal_escape in v1.2.3.
        if (defined('PHPTAL_VERSION') && version_compare(PHPTAL_VERSION, '1.2.3', '>=')) {
            $escapeFunctionCall = 'phptal_escape($value, \'UTF-8\')';
        } else {
            $escapeFunctionCall = 'phptal_escape($value)';
        }
        /**
         * Compiled code will loop through a Zend_Form_Element attributes
         * looking for the 'data' key, and assign it to a known temporary
         * variable.
         */
        $source = '
		' . $assignment . '
		if ((is_object(' . $tmp . ')
			&& ' . $tmp . ' instanceof Zend_Form_Element)
		) {
			' . $attributes . PHP_EOL . '
			' . $dataAttributes . ' = " ";

			if (isset($attributes["data"]) && is_array($attributes["data"])) {
				foreach ($attributes["data"] as $key => $value) {
					' . $dataAttributes . ' .= "data-{$key}=\\"" . ' . $escapeFunctionCall . ' . "\\" ";
				}
				' . $dataAttributes . ' = rtrim(' . $dataAttributes . ');
			}
		}';
        // persist the code for compilation
        $codewriter->pushCode($source);
        // get the current DOM element to pull in the attributes
        $this->phpelement->getOrCreateAttributeNode('ztal:data-attributes')->overwriteFullWithVariable($dataAttributes);
    }
Example #14
0
 public function before(PHPTAL_Php_CodeWriter $codewriter)
 {
     $escape = true;
     if (preg_match('/^(text|structure)(?:\\s+(.*)|\\s*$)/', $this->expression, $m)) {
         if ($m[1] == 'structure') {
             $escape = false;
         }
         $this->expression = isset($m[2]) ? $m[2] : '';
     }
     // if no expression is given, the content of the node is used as
     // a translation key
     if (strlen(trim($this->expression)) == 0) {
         $key = $this->_getTranslationKey($this->phpelement, !$escape, $codewriter->getEncoding());
         $key = trim(preg_replace('/\\s+/sm' . ($codewriter->getEncoding() == 'UTF-8' ? 'u' : ''), ' ', $key));
         $code = $codewriter->str($key);
     } else {
         $code = $codewriter->evaluateExpression($this->expression);
     }
     $this->_prepareNames($codewriter, $this->phpelement);
     $codewriter->pushCode('echo $_translator->translate(' . $code . ',' . ($escape ? 'true' : 'false') . ');');
 }
Example #15
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     // restore source
     $code = $codewriter->getTranslatorReference() . '->setSource(array_pop($_i18n_sources))';
     $codewriter->pushCode($code);
 }
Example #16
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $codewriter->doEnd();
 }
Example #17
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $codewriter->setDebug($this->_oldMode);
 }
Example #18
0
 /**
  * Parse currently set template, prefilter and generate PHP code.
  *
  * @return string (compiled PHP code)
  */
 protected function parse()
 {
     $data = $this->_source->getData();
     $prefilters = $this->getPreFilterInstances();
     foreach ($prefilters as $prefilter) {
         $data = $prefilter->filter($data);
     }
     $realpath = $this->_source->getRealPath();
     $parser = new PHPTAL_Dom_SaxXmlParser($this->_encoding);
     $builder = new PHPTAL_Dom_PHPTALDocumentBuilder();
     $tree = $parser->parseString($builder, $data, $realpath)->getResult();
     foreach ($prefilters as $prefilter) {
         if ($prefilter instanceof PHPTAL_PreFilter) {
             if ($prefilter->filterDOM($tree) !== NULL) {
                 throw new PHPTAL_ConfigurationException("Don't return value from filterDOM()");
             }
         }
     }
     $state = new PHPTAL_Php_State($this);
     $codewriter = new PHPTAL_Php_CodeWriter($state);
     $codewriter->doTemplateFile($this->getFunctionName(), $tree);
     return $codewriter->getResult();
 }
Example #19
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $codewriter->pushCode($codewriter->getTranslatorReference() . '->setVar(' . $codewriter->str($this->expression) . ', ob_get_clean())');
 }
Example #20
0
 private function doDefineVarWith(PHPTAL_Php_CodeWriter $codewriter, $code)
 {
     if ($this->_defineScope == 'global') {
         $codewriter->doSetVar('$tpl->getGlobalContext()->' . $this->_defineVar, $code);
     } else {
         $codewriter->doSetVar('$ctx->' . $this->_defineVar, $code);
     }
 }
Example #21
0
 public function generateCode(PHPTAL_Php_CodeWriter $codewriter)
 {
     if (!preg_match('/^\\s*!/', $this->getValueEscaped())) {
         $codewriter->pushHTML('<!--' . $this->getValueEscaped() . '-->');
     }
 }
Example #22
0
 /**
  * @param key - unescaped string (not PHP code) for the key
  */
 private function _getTranslationCode(PHPTAL_Php_CodeWriter $codewriter, $key)
 {
     $code = '';
     if (preg_match_all('/\\$\\{(.*?)\\}/', $key, $m)) {
         array_shift($m);
         $m = array_shift($m);
         foreach ($m as $name) {
             $code .= "\n" . '$_translator->setVar(' . $codewriter->str($name) . ',' . PHPTAL_Php_TalesInternal::compileToPHPExpression($name) . ');';
             // allow more complex TAL expressions
         }
         $code .= "\n";
     }
     // notice the false boolean which indicate that the html is escaped
     // elsewhere looks like an hack doesn't it ? :)
     $code .= 'echo ' . $codewriter->escapeCode('$_translator->translate(' . $codewriter->str($key) . ', false)');
     return $code;
 }
Example #23
0
 /**
  * generate code that pops macro slots
  * (restore slots if not inherited macro)
  */
 private function popSlots(PHPTAL_Php_CodeWriter $codewriter)
 {
     if (!$this->phpelement->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'define-macro')) {
         $codewriter->pushCode('$ctx->popSlots()');
     }
 }
Example #24
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $var = $codewriter->createTempVariable();
     $codewriter->pushCode('ob_end_flush()');
     $codewriter->doCatch('Exception ' . $var);
     $codewriter->pushCode('$tpl->addError(' . $var . ')');
     $codewriter->pushCode('ob_end_clean()');
     $expression = $this->extractEchoType($this->expression);
     $code = $codewriter->evaluateExpression($expression);
     switch ($code) {
         case PHPTAL_Php_TalesInternal::NOTHING_KEYWORD:
             break;
         case PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD:
             $codewriter->pushHTML('<pre class="phptalError"');
             $codewriter->doEchoRaw($var);
             $codewriter->pushHTML('</pre>');
             break;
         default:
             $this->doEchoAttribute($codewriter, $code);
             break;
     }
     $codewriter->doEnd();
     $codewriter->recycleTempVariable($var);
 }
Example #25
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     if ($this->varname) {
         $codewriter->recycleTempVariable($this->varname);
     }
 }
Example #26
0
 public function before(PHPTAL_Php_CodeWriter $codewriter)
 {
     $codewriter->doComment($this->expression);
 }
Example #27
0
 private function generateAttributes(PHPTAL_Php_CodeWriter $codewriter)
 {
     $html5mode = $codewriter->getOutputMode() === PHPTAL::HTML5;
     foreach ($this->getAttributeNodes() as $attr) {
         // xmlns:foo is not allowed in text/html
         if ($html5mode && $attr->isNamespaceDeclaration()) {
             continue;
         }
         switch ($attr->getReplacedState()) {
             case PHPTAL_Dom_Attr::NOT_REPLACED:
                 $codewriter->pushHTML(' ' . $attr->getQualifiedName());
                 if ($codewriter->getOutputMode() !== PHPTAL::HTML5 || !PHPTAL_Dom_Defs::getInstance()->isBooleanAttribute($attr->getQualifiedName())) {
                     $html = $codewriter->interpolateHTML($attr->getValueEscaped());
                     $codewriter->pushHTML('=' . $codewriter->quoteAttributeValue($html));
                 }
                 break;
             case PHPTAL_Dom_Attr::HIDDEN:
                 break;
             case PHPTAL_Dom_Attr::FULLY_REPLACED:
                 $codewriter->pushHTML($attr->getValueEscaped());
                 break;
             case PHPTAL_Dom_Attr::VALUE_REPLACED:
                 $codewriter->pushHTML(' ' . $attr->getQualifiedName() . '="');
                 $codewriter->pushHTML($attr->getValueEscaped());
                 $codewriter->pushHTML('"');
                 break;
         }
     }
 }
Example #28
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $code = '$ctx->fillSlot("' . $this->expression . '", ob_get_clean())';
     $codewriter->pushCode($code);
 }
Example #29
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     $codewriter->doEnd('if');
     $codewriter->recycleTempVariable($this->tmp_var);
 }
Example #30
0
 public function after(PHPTAL_Php_CodeWriter $codewriter)
 {
     foreach ($this->vars_to_recycle as $var) {
         $codewriter->recycleTempVariable($var);
     }
 }