private function prepareAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $expression) { $tales_code = $this->extractEchoType($expression); $code = $codewriter->evaluateExpression($tales_code); // XHTML boolean attribute does not appear when empty or false if (PHPTAL_Dom_Defs::getInstance()->isBooleanAttribute($qname)) { // I don't want to mix code for boolean with chained executor // so compile it again to simple expression if (is_array($code)) { $code = PHPTAL_Php_TalesInternal::compileToPHPExpression($tales_code); } return $this->prepareBooleanAttribute($codewriter, $qname, $code); } // if $code is an array then the attribute value is decided by a // tales chained expression if (is_array($code)) { return $this->prepareChainedAttribute($codewriter, $qname, $code); } // i18n needs to read replaced value of the attribute, which is not possible if attribute is completely replaced with conditional code if ($this->phpelement->hasAttributeNS('http://xml.zope.org/namespaces/i18n', 'attributes')) { $this->prepareAttributeUnconditional($codewriter, $qname, $code); } else { $this->prepareAttributeConditional($codewriter, $qname, $code); } }
/** * returns PHP code that generates given string, including dynamic replacements * * It's almost unused. */ public function interpolateTalesVarsInString($string) { return PHPTAL_Php_TalesInternal::parseString($string, false, $this->getTalesMode() === 'tales' ? '' : 'php:'); }
/** * @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; }
/** * PHPTAL templating engine * * PHP Version 5 * * @category HTML * @package PHPTAL * @author Laurent Bedubourg <*****@*****.**> * @author Kornel Lesiński <*****@*****.**> * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id$ * @link http://phptal.org/ */ function phptal_tales_custom($src, $nothrow) { return 'sprintf("%01.2f", ' . PHPTAL_Php_TalesInternal::path($src, $nothrow) . ')'; }
/** * returns PHP code that generates given string, including dynamic replacements */ public function interpolateTalesVarsInString($string) { if ($this->_talesMode == 'tales') { return PHPTAL_Php_TalesInternal::string($string); } // replace ${var} found in expression while (preg_match('/(?<!\\$)\\$\\{([^\\}]+)\\}/s', $string, $m)) { list($ori, $exp) = $m; $php = PHPTAL_Php_TalesInternal::php($exp); $string = str_replace($ori, '\'.' . $php . '.\'', $string); // FIXME: that is not elegant } $string = str_replace('$${', '${', $string); // FIXME: that is not elegant return '\'' . $string . '\''; }
/** * returns PHP code that will evaluate given TALES expression. * e.g. "string:foo${bar}" may be transformed to "'foo'.phptal_escape($ctx->bar)" * * Expressions with alternatives ("foo | bar") will cause it to return array * Use phptal_tale() if you always want string. * * @param bool $nothrow if true, invalid expression will return NULL (at run time) rather than throwing exception * @return string or array */ function phptal_tales($expression, $nothrow = false) { return PHPTAL_Php_TalesInternal::compileToPHPStatements($expression, $nothrow); }
function testDoubleVar() { $res = PHPTAL_Php_TalesInternal::string('hello $foo $bar'); $this->assertRegExp('/ctx->foo/', $res, '$foo not interpolated'); $this->assertRegExp('/ctx->bar/', $res, '$bar not interpolated'); }