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); } }
public function before(PHPTAL_Php_CodeWriter $codewriter) { // number or variable name followed by time unit // optional per expression if (!preg_match('/^\\s*([0-9]+\\s*|[a-zA-Z][\\/a-zA-Z0-9_]*\\s+)([dhms])\\s*(?:\\;?\\s*per\\s+([^;]+)|)\\s*$/', $this->expression, $matches)) { throw new PHPTAL_ParserException("Cache attribute syntax error: " . $this->expression, $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); } $cache_len = $matches[1]; if (!is_numeric($cache_len)) { $cache_len = $codewriter->evaluateExpression($cache_len); if (is_array($cache_len)) { throw new PHPTAL_ParserException("Chained expressions in cache length are not supported", $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); } } switch ($matches[2]) { case 'd': $cache_len .= '*24'; /* no break */ /* no break */ case 'h': $cache_len .= '*60'; /* no break */ /* no break */ case 'm': $cache_len .= '*60'; /* no break */ } $cache_tag = '"' . addslashes($this->phpelement->getQualifiedName() . ':' . $this->phpelement->getSourceLine()) . '"'; $cache_per_expression = isset($matches[3]) ? trim($matches[3]) : null; if ($cache_per_expression == 'url') { $cache_tag .= '.$_SERVER["REQUEST_URI"]'; } elseif ($cache_per_expression == 'nothing') { /* do nothing */ } elseif ($cache_per_expression) { $code = $codewriter->evaluateExpression($cache_per_expression); if (is_array($code)) { throw new PHPTAL_ParserException("Chained expressions in per-cache directive are not supported", $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); } $cache_tag = '(' . $code . ')."@".' . $cache_tag; } $this->cache_filename_var = $codewriter->createTempVariable(); $codewriter->doSetVar($this->cache_filename_var, $codewriter->str($codewriter->getCacheFilesBaseName()) . '.md5(' . $cache_tag . ')'); $cond = '!file_exists(' . $this->cache_filename_var . ') || time() - ' . $cache_len . ' >= filemtime(' . $this->cache_filename_var . ')'; $codewriter->doIf($cond); $codewriter->doEval('ob_start()'); }
/** * 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); }
public function before(PHPTAL_Php_CodeWriter $codewriter) { if (trim($this->expression) == '') { $this->phpelement->headFootDisabled = true; } else { $this->varname = $codewriter->createTempVariable(); // print tag header/foot only if condition is false $cond = $codewriter->evaluateExpression($this->expression); $this->phpelement->headPrintCondition = '(' . $this->varname . ' = !(' . $cond . '))'; $this->phpelement->footPrintCondition = $this->varname; } }
public function before(PHPTAL_Php_CodeWriter $codewriter) { $this->var = $codewriter->createTempVariable(); // alias to repeats handler to avoid calling extra getters on each variable access $codewriter->doSetVar($this->var, '$ctx->repeat'); list($varName, $expression) = $this->parseSetExpression($this->expression); $code = $codewriter->evaluateExpression($expression); // instantiate controller using expression $codewriter->doSetVar($this->var . '->' . $varName, 'new PHPTAL_RepeatController(' . $code . ')' . "\n"); $codewriter->pushContext(); // Lets loop the iterator with a foreach construct $codewriter->doForeach('$ctx->' . $varName, $this->var . '->' . $varName); }
public function before(PHPTAL_Php_CodeWriter $codewriter) { $code = $codewriter->evaluateExpression($this->expression); // If it's a chained expression build a new code path if (is_array($code)) { $this->expressions = array(); $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $code, $this); return; } // Force a falsy condition if the nothing keyword is active if ($code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { $code = 'false'; } $codewriter->doIf($code); }
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); }
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') . ');'); }
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); }
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); }