diff options
author | emkael <emkael@tlen.pl> | 2016-10-31 21:58:33 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-10-31 21:59:22 +0100 |
commit | d216b3147bc3f37cf2337acab5767c6a4f74aa2e (patch) | |
tree | 6090989e5071db101a1112131e2b075a02dccbc4 /lib/phptal/PHPTAL/Php/Attribute/TAL | |
parent | b23bfbb17d1d5f6852a1690f246a84c2d38ae969 (diff) |
* PHPTAL library
Diffstat (limited to 'lib/phptal/PHPTAL/Php/Attribute/TAL')
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php | 213 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php | 30 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php | 93 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php | 95 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php | 193 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php | 70 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php | 73 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php | 99 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php | 117 |
9 files changed, 983 insertions, 0 deletions
diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php new file mode 100644 index 0000000..158d079 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php @@ -0,0 +1,213 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** + * TAL Specifications 1.4 + * + * argument ::= attribute_statement [';' attribute_statement]* + * attribute_statement ::= attribute_name expression + * attribute_name ::= [namespace ':'] Name + * namespace ::= Name + * + * examples: + * + * <a href="/sample/link.html" + * tal:attributes="href here/sub/absolute_url"> + * <textarea rows="80" cols="20" + * tal:attributes="rows request/rows;cols request/cols"> + * + * IN PHPTAL: attributes will not work on structured replace. + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Attributes +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + /** before creates several variables that need to be freed in after */ + private $vars_to_recycle = array(); + + /** + * value for default keyword + */ + private $_default_escaped; + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // split attributes using ; delimiter + $attrs = $codewriter->splitExpression($this->expression); + foreach ($attrs as $exp) { + list($qname, $expression) = $this->parseSetExpression($exp); + if ($expression) { + $this->prepareAttribute($codewriter, $qname, $expression); + } + } + } + + 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); + } + } + + /** + * attribute will be output regardless of its evaluated value. NULL behaves just like "". + */ + private function prepareAttributeUnconditional(PHPTAL_Php_CodeWriter $codewriter, $qname, $code) + { + // regular attribute which value is the evaluation of $code + $attkey = $this->getVarName($qname, $codewriter); + if ($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE) { + $value = $codewriter->stringifyCode($code); + } else { + $value = $codewriter->escapeCode($code); + } + $codewriter->doSetVar($attkey, $value); + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteValueWithVariable($attkey); + } + + /** + * If evaluated value of attribute is NULL, it will not be output at all. + */ + private function prepareAttributeConditional(PHPTAL_Php_CodeWriter $codewriter, $qname, $code) + { + // regular attribute which value is the evaluation of $code + $attkey = $this->getVarName($qname, $codewriter); + + $codewriter->doIf("null !== ($attkey = ($code))"); + + if ($this->_echoType !== PHPTAL_Php_Attribute::ECHO_STRUCTURE) + $codewriter->doSetVar($attkey, $codewriter->str(" $qname=\"").".".$codewriter->escapeCode($attkey).".'\"'"); + else + $codewriter->doSetVar($attkey, $codewriter->str(" $qname=\"").".".$codewriter->stringifyCode($attkey).".'\"'"); + + $codewriter->doElse(); + $codewriter->doSetVar($attkey, "''"); + $codewriter->doEnd('if'); + + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey); + } + + private function prepareChainedAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $chain) + { + $this->_default_escaped = false; + $this->_attribute = $qname; + if ($default_attr = $this->phpelement->getAttributeNode($qname)) { + $this->_default_escaped = $default_attr->getValueEscaped(); + } + $this->_attkey = $this->getVarName($qname, $codewriter); + $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $chain, $this); + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($this->_attkey); + } + + private function prepareBooleanAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $code) + { + $attkey = $this->getVarName($qname, $codewriter); + + if ($codewriter->getOutputMode() === PHPTAL::HTML5) { + $value = "' $qname'"; + } else { + $value = "' $qname=\"$qname\"'"; + } + $codewriter->doIf($code); + $codewriter->doSetVar($attkey, $value); + $codewriter->doElse(); + $codewriter->doSetVar($attkey, '\'\''); + $codewriter->doEnd('if'); + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey); + } + + private function getVarName($qname, PHPTAL_Php_CodeWriter $codewriter) + { + $var = $codewriter->createTempVariable(); + $this->vars_to_recycle[] = $var; + return $var; + } + + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + foreach ($this->vars_to_recycle as $var) $codewriter->recycleTempVariable($var); + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $codewriter = $executor->getCodeWriter(); + $executor->doElse(); + $codewriter->doSetVar( + $this->_attkey, + "''" + ); + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $codewriter = $executor->getCodeWriter(); + $executor->doElse(); + $attr_str = ($this->_default_escaped !== false) + ? ' '.$this->_attribute.'='.$codewriter->quoteAttributeValue($this->_default_escaped) // default value + : ''; // do not print attribute + $codewriter->doSetVar($this->_attkey, $codewriter->str($attr_str)); + $executor->breakChain(); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + $codewriter = $executor->getCodeWriter(); + + if (!$islast) { + $condition = "!phptal_isempty($this->_attkey = ($exp))"; + } else { + $condition = "null !== ($this->_attkey = ($exp))"; + } + $executor->doIf($condition); + + if ($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE) + $value = $codewriter->stringifyCode($this->_attkey); + else + $value = $codewriter->escapeCode($this->_attkey); + + $codewriter->doSetVar($this->_attkey, $codewriter->str(" {$this->_attribute}=\"").".$value.'\"'"); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php new file mode 100644 index 0000000..4e5896e --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php @@ -0,0 +1,30 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * @package PHPTAL + * @subpackage Php.attribute.tal + */ +class PHPTAL_Php_Attribute_TAL_Comment extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doComment($this->expression); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php new file mode 100644 index 0000000..d86b94b --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php @@ -0,0 +1,93 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= expression + * + * Example: + * + * <p tal:condition="here/copyright" + * tal:content="here/copyright">(c) 2000</p> + * + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Condition +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + private $expressions = array(); + + 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('phptal_true(' . $code . ')'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEnd('if'); + } + + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + // check if the expression is empty + if ($exp !== 'false') { + $this->expressions[] = '!phptal_isempty(' . $exp . ')'; + } + + if ($islast) { + // for the last one in the chain build a ORed condition + $executor->getCodeWriter()->doIf( implode(' || ', $this->expressions ) ); + // The executor will always end an if so we output a dummy if + $executor->doIf('false'); + } + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + // end the chain + $this->talesChainPart($executor, 'false', true); + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + throw new PHPTAL_ParserException('\'default\' keyword not allowed on conditional expressions', + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php new file mode 100644 index 0000000..aef5865 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php @@ -0,0 +1,95 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** TAL Specifications 1.4 + * + * argument ::= (['text'] | 'structure') expression + * + * Example: + * + * <p tal:content="user/name">Fred Farkas</p> + * + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Content +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + 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 after(PHPTAL_Php_CodeWriter $codewriter) + { + } + + private function generateDefault(PHPTAL_Php_CodeWriter $codewriter) + { + $this->phpelement->generateContent($codewriter, true); + } + + protected function generateChainedContent(PHPTAL_Php_CodeWriter $codewriter, $code) + { + $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $code, $this); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + if (!$islast) { + $var = $executor->getCodeWriter()->createTempVariable(); + $executor->doIf('!phptal_isempty('.$var.' = '.$exp.')'); + $this->doEchoAttribute($executor->getCodeWriter(), $var); + $executor->getCodeWriter()->recycleTempVariable($var); + } else { + $executor->doElse(); + $this->doEchoAttribute($executor->getCodeWriter(), $exp); + } + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->doElse(); + $this->generateDefault($executor->getCodeWriter()); + $executor->breakChain(); + } +} diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php new file mode 100644 index 0000000..f5c074b --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php @@ -0,0 +1,193 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL spec 1.4 for tal:define content + * + * argument ::= define_scope [';' define_scope]* + * define_scope ::= (['local'] | 'global') define_var + * define_var ::= variable_name expression + * variable_name ::= Name + * + * Note: If you want to include a semi-colon (;) in an expression, it must be escaped by doubling it (;;).* + * + * examples: + * + * tal:define="mytitle template/title; tlen python:len(mytitle)" + * tal:define="global company_name string:Digital Creations, Inc." + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Define +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + private $tmp_content_var; + private $_buffered = false; + private $_defineScope = null; + private $_defineVar = null; + private $_pushedContext = false; + /** + * Prevents generation of invalid PHP code when given invalid TALES + */ + private $_chainPartGenerated=false; + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $expressions = $codewriter->splitExpression($this->expression); + $definesAnyNonGlobalVars = false; + + foreach ($expressions as $exp) { + list($defineScope, $defineVar, $expression) = $this->parseExpression($exp); + if (!$defineVar) { + continue; + } + + $this->_defineScope = $defineScope; + + // <span tal:define="global foo" /> should be invisible, but <img tal:define="bar baz" /> not + if ($defineScope != 'global') $definesAnyNonGlobalVars = true; + + if ($this->_defineScope != 'global' && !$this->_pushedContext) { + $codewriter->pushContext(); + $this->_pushedContext = true; + } + + $this->_defineVar = $defineVar; + if ($expression === null) { + // no expression give, use content of tag as value for newly defined var. + $this->bufferizeContent($codewriter); + continue; + } + + $code = $codewriter->evaluateExpression($expression); + if (is_array($code)) { + $this->chainedDefine($codewriter, $code); + } elseif ( $code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { + $this->doDefineVarWith($codewriter, 'null'); + } else { + $this->doDefineVarWith($codewriter, $code); + } + } + + // if the content of the tag was buffered or the tag has nothing to tell, we hide it. + if ($this->_buffered || (!$definesAnyNonGlobalVars && !$this->phpelement->hasRealContent() && !$this->phpelement->hasRealAttributes())) { + $this->phpelement->hidden = true; + } + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + if ($this->tmp_content_var) $codewriter->recycleTempVariable($this->tmp_content_var); + if ($this->_pushedContext) { + $codewriter->popContext(); + } + } + + private function chainedDefine(PHPTAL_Php_CodeWriter $codewriter, $parts) + { + $executor = new PHPTAL_Php_TalesChainExecutor( + $codewriter, $parts, $this + ); + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + if (!$this->_chainPartGenerated) throw new PHPTAL_TemplateException("Invalid expression in tal:define", $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + + $executor->doElse(); + $this->doDefineVarWith($executor->getCodeWriter(), 'null'); + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + if (!$this->_chainPartGenerated) throw new PHPTAL_TemplateException("Invalid expression in tal:define", $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + + $executor->doElse(); + $this->bufferizeContent($executor->getCodeWriter()); + $executor->breakChain(); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + $this->_chainPartGenerated=true; + + if ($this->_defineScope == 'global') { + $var = '$tpl->getGlobalContext()->'.$this->_defineVar; + } else { + $var = '$ctx->'.$this->_defineVar; + } + + $cw = $executor->getCodeWriter(); + + if (!$islast) { + // must use temp variable, because expression could refer to itself + $tmp = $cw->createTempVariable(); + $executor->doIf('('.$tmp.' = '.$exp.') !== null'); + $cw->doSetVar($var, $tmp); + $cw->recycleTempVariable($tmp); + } else { + $executor->doIf('('.$var.' = '.$exp.') !== null'); + } + } + + /** + * Parse the define expression, already splitted in sub parts by ';'. + */ + public function parseExpression($exp) + { + $defineScope = false; // (local | global) + $defineVar = false; // var to define + + // extract defineScope from expression + $exp = trim($exp); + if (preg_match('/^(local|global)\s+(.*?)$/ism', $exp, $m)) { + list(, $defineScope, $exp) = $m; + $exp = trim($exp); + } + + // extract varname and expression from remaining of expression + list($defineVar, $exp) = $this->parseSetExpression($exp); + if ($exp !== null) $exp = trim($exp); + return array($defineScope, $defineVar, $exp); + } + + private function bufferizeContent(PHPTAL_Php_CodeWriter $codewriter) + { + if (!$this->_buffered) { + $this->tmp_content_var = $codewriter->createTempVariable(); + $codewriter->pushCode( 'ob_start()' ); + $this->phpelement->generateContent($codewriter); + $codewriter->doSetVar($this->tmp_content_var, 'ob_get_clean()'); + $this->_buffered = true; + } + $this->doDefineVarWith($codewriter, $this->tmp_content_var); + } + + 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); + } + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php new file mode 100644 index 0000000..d7530b3 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php @@ -0,0 +1,70 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= [expression] + * + * Example: + * + * <div tal:omit-tag="" comment="This tag will be removed"> + * <i>...but this text will remain.</i> + * </div> + * + * <b tal:omit-tag="not:bold">I may not be bold.</b> + * + * To leave the contents of a tag in place while omitting the surrounding + * start and end tag, use the omit-tag statement. + * + * If its expression evaluates to a false value, then normal processing + * of the element continues. + * + * If the expression evaluates to a true value, or there is no + * expression, the statement tag is replaced with its contents. It is up to + * the interface between TAL and the expression engine to determine the + * value of true and false. For these purposes, the value nothing is false, + * and cancellation of the action has the same effect as returning a + * false value. + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_OmitTag extends PHPTAL_Php_Attribute +{ + private $varname; + 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.' = !phptal_unravel_closure('.$cond.'))'; + $this->phpelement->footPrintCondition = $this->varname; + } + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + if ($this->varname) $codewriter->recycleTempVariable($this->varname); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php new file mode 100644 index 0000000..382d387 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php @@ -0,0 +1,73 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= (['text'] | 'structure') expression + * + * Example: + * + * <p tal:on-error="string: Error! This paragraph is buggy!"> + * My name is <span tal:replace="here/SlimShady" />.<br /> + * (My login name is + * <b tal:on-error="string: Username is not defined!" + * tal:content="user">Unknown</b>) + * </p> + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_OnError extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doTry(); + $codewriter->pushCode('ob_start()'); + } + + 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->doEcho($var); + $codewriter->pushHTML('</pre>'); + break; + + default: + $this->doEchoAttribute($codewriter, $code); + break; + } + $codewriter->doEnd('catch'); + + $codewriter->recycleTempVariable($var); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php new file mode 100644 index 0000000..d0e4c2d --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php @@ -0,0 +1,99 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= variable_name expression + * variable_name ::= Name + * + * Example: + * + * <p tal:repeat="txt python:'one', 'two', 'three'"> + * <span tal:replace="txt" /> + * </p> + * <table> + * <tr tal:repeat="item here/cart"> + * <td tal:content="repeat/item/index">1</td> + * <td tal:content="item/description">Widget</td> + * <td tal:content="item/price">$1.50</td> + * </tr> + * </table> + * + * The following information is available from an Iterator: + * + * * index - repetition number, starting from zero. + * * number - repetition number, starting from one. + * * even - true for even-indexed repetitions (0, 2, 4, ...). + * * odd - true for odd-indexed repetitions (1, 3, 5, ...). + * * start - true for the starting repetition (index 0). + * * end - true for the ending, or final, repetition. + * * length - length of the sequence, which will be the total number of repetitions. + * + * * letter - count reps with lower-case letters: "a" - "z", "aa" - "az", "ba" - "bz", ..., "za" - "zz", "aaa" - "aaz", and so forth. + * * Letter - upper-case version of letter. + * * roman - count reps with lower-case roman numerals: "i", "ii", "iii", "iv", "v", "vi" ... + * * Roman - upper-case version of roman numerals. + * * first - true for the first item in a group - see note below + * * lasst - true for the last item in a group - see note below + * + * Note: first and last are intended for use with sorted sequences. They try to + * divide the sequence into group of items with the same value. If you provide + * a path, then the value obtained by following that path from a sequence item + * is used for grouping, otherwise the value of the item is used. You can + * provide the path by appending it to the path from the repeat variable, + * as in "repeat/item/first/color". + * + * PHPTAL: index, number, even, etc... will be stored in the + * $ctx->repeat->'item' object. Thus $ctx->repeat->item->odd + * + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Repeat extends PHPTAL_Php_Attribute +{ + private $var; + 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 after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEnd('foreach'); + $codewriter->popContext(); + + $codewriter->recycleTempVariable($this->var); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php new file mode 100644 index 0000000..b72cafa --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php @@ -0,0 +1,117 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * TAL Specifications 1.4 + * + * argument ::= (['text'] | 'structure') expression + * + * Default behaviour : text + * + * <span tal:replace="template/title">Title</span> + * <span tal:replace="text template/title">Title</span> + * <span tal:replace="structure table" /> + * <span tal:replace="nothing">This element is a comment.</span> + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Replace +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + 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) + { + } + + /** + * support expressions like "foo | bar" + */ + private function replaceByChainedExpression(PHPTAL_Php_CodeWriter $codewriter, $expArray) + { + $executor = new PHPTAL_Php_TalesChainExecutor( + $codewriter, $expArray, $this + ); + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->continueChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->doElse(); + $this->generateDefault($executor->getCodeWriter()); + $executor->breakChain(); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + if (!$islast) { + $var = $executor->getCodeWriter()->createTempVariable(); + $executor->doIf('!phptal_isempty('.$var.' = '.$exp.')'); + $this->doEchoAttribute($executor->getCodeWriter(), $var); + $executor->getCodeWriter()->recycleTempVariable($var); + } else { + $executor->doElse(); + $this->doEchoAttribute($executor->getCodeWriter(), $exp); + } + } + + /** + * don't replace - re-generate default content + */ + private function generateDefault(PHPTAL_Php_CodeWriter $codewriter) + { + $this->phpelement->generateSurroundHead($codewriter); + $this->phpelement->generateHead($codewriter); + $this->phpelement->generateContent($codewriter); + $this->phpelement->generateFoot($codewriter); + $this->phpelement->generateSurroundFoot($codewriter); + } +} + |