summaryrefslogtreecommitdiff
path: root/lib/phptal/PHPTAL/Php/Attribute
diff options
context:
space:
mode:
Diffstat (limited to 'lib/phptal/PHPTAL/Php/Attribute')
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/I18N/Attributes.php118
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/I18N/Data.php36
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/I18N/Domain.php50
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/I18N/Name.php47
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/I18N/Source.php48
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/I18N/Target.php43
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/I18N/Translate.php130
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php67
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.php70
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php148
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php135
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Cache.php97
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Debug.php34
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Id.php53
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Tales.php45
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php213
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php30
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php93
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php95
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php193
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php70
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php73
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php99
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php117
24 files changed, 2104 insertions, 0 deletions
diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Attributes.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Attributes.php
new file mode 100644
index 0000000..5eb3fac
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Attributes.php
@@ -0,0 +1,118 @@
+<?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/
+ */
+
+/**
+ * i18n:attributes
+ *
+ * This attribute will allow us to translate attributes of HTML tags, such
+ * as the alt attribute in the img tag. The i18n:attributes attribute
+ * specifies a list of attributes to be translated with optional message
+ * IDs? for each; if multiple attribute names are given, they must be
+ * separated by semi-colons. Message IDs? used in this context must not
+ * include whitespace.
+ *
+ * Note that the value of the particular attributes come either from the
+ * HTML attribute value itself or from the data inserted by tal:attributes.
+ *
+ * If an attibute is to be both computed using tal:attributes and translated,
+ * the translation service is passed the result of the TALES expression for
+ * that attribute.
+ *
+ * An example:
+ *
+ * <img src="http://foo.com/logo" alt="Visit us"
+ * tal:attributes="alt here/greeting"
+ * i18n:attributes="alt"
+ * />
+ *
+ *
+ * In this example, let tal:attributes set the value of the alt attribute to
+ * the text "Stop by for a visit!". This text will be passed to the
+ * translation service, which uses the result of language negotiation to
+ * translate "Stop by for a visit!" into the requested language. The example
+ * text in the template, "Visit us", will simply be discarded.
+ *
+ * Another example, with explicit message IDs:
+ *
+ * <img src="../icons/uparrow.png" alt="Up"
+ * i18n:attributes="src up-arrow-icon; alt up-arrow-alttext"
+ * >
+ *
+ * Here, the message ID up-arrow-icon will be used to generate the link to
+ * an icon image file, and the message ID up-arrow-alttext will be used for
+ * the "alt" text.
+ *
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.i18n
+ */
+class PHPTAL_Php_Attribute_I18N_Attributes extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ // split attributes to translate
+ foreach ($codewriter->splitExpression($this->expression) as $exp) {
+ list($qname, $key) = $this->parseSetExpression($exp);
+
+ // if the translation key is specified and not empty (but may be '0')
+ if (strlen($key)) {
+ // we use it and replace the tag attribute with the result of the translation
+ $code = $this->_getTranslationCode($codewriter, $key);
+ } else {
+ $attr = $this->phpelement->getAttributeNode($qname);
+ if (!$attr) throw new PHPTAL_TemplateException("Unable to translate attribute $qname, because there is no translation key specified",
+ $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine());
+
+ if ($attr->getReplacedState() === PHPTAL_Dom_Attr::NOT_REPLACED) {
+ $code = $this->_getTranslationCode($codewriter, $attr->getValue());
+ } elseif ($attr->getReplacedState() === PHPTAL_Dom_Attr::VALUE_REPLACED && $attr->getOverwrittenVariableName()) {
+ // sadly variables won't be interpolated in this translation
+ $code = 'echo '.$codewriter->escapeCode($codewriter->getTranslatorReference(). '->translate('.$attr->getOverwrittenVariableName().', false)');
+ } else {
+ throw new PHPTAL_TemplateException("Unable to translate attribute $qname, because other TAL attributes are using it",
+ $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine());
+ }
+ }
+ $this->phpelement->getOrCreateAttributeNode($qname)->overwriteValueWithCode($code);
+ }
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ }
+
+ /**
+ * @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".$codewriter->getTranslatorReference(). '->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($codewriter->getTranslatorReference().'->translate('.$codewriter->str($key).', false)');
+ return $code;
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Data.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Data.php
new file mode 100644
index 0000000..bad310f
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Data.php
@@ -0,0 +1,36 @@
+<?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/
+ */
+
+/**
+ * i18n:data
+ *
+ * Since TAL always returns strings, we need a way in ZPT to translate
+ * objects, the most obvious case being DateTime objects. The data attribute
+ * will allow us to specify such an object, and i18n:translate will provide
+ * us with a legal format string for that object. If data is used,
+ * i18n:translate must be used to give an explicit message ID, rather than
+ * relying on a message ID computed from the content.
+ *
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.i18n
+ */
+class PHPTAL_Php_Attribute_I18N_Data extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter){}
+ public function after(PHPTAL_Php_CodeWriter $codewriter){}
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Domain.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Domain.php
new file mode 100644
index 0000000..92ece11
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Domain.php
@@ -0,0 +1,50 @@
+<?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/
+ */
+
+/**
+ * i18n:domain
+ *
+ * The i18n:domain attribute is used to specify the domain to be used to get
+ * the translation. If not specified, the translation services will use a
+ * default domain. The value of the attribute is used directly; it is not
+ * a TALES expression.
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.i18n
+ */
+class PHPTAL_Php_Attribute_I18N_Domain extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ // ensure a domain stack exists or create it
+ $codewriter->doIf('!isset($_i18n_domains)');
+ $codewriter->pushCode('$_i18n_domains = array()');
+ $codewriter->doEnd('if');
+
+ $expression = $codewriter->interpolateTalesVarsInString($this->expression);
+
+ // push current domain and use new domain
+ $code = '$_i18n_domains[] = '.$codewriter->getTranslatorReference().'->useDomain('.$expression.')';
+ $codewriter->pushCode($code);
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ // restore domain
+ $code = $codewriter->getTranslatorReference().'->useDomain(array_pop($_i18n_domains))';
+ $codewriter->pushCode($code);
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Name.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Name.php
new file mode 100644
index 0000000..8a8f4e7
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Name.php
@@ -0,0 +1,47 @@
+<?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/
+ */
+
+/** i18n:name
+ *
+ * Name the content of the current element for use in interpolation within
+ * translated content. This allows a replaceable component in content to be
+ * re-ordered by translation. For example:
+ *
+ * <span i18n:translate=''>
+ * <span tal:replace='here/name' i18n:name='name' /> was born in
+ * <span tal:replace='here/country_of_birth' i18n:name='country' />.
+ * </span>
+ *
+ * would cause this text to be passed to the translation service:
+ *
+ * "${name} was born in ${country}."
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.i18n
+ */
+class PHPTAL_Php_Attribute_I18N_Name extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $codewriter->pushCode('ob_start()');
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $codewriter->pushCode($codewriter->getTranslatorReference().'->setVar('.$codewriter->str($this->expression).', ob_get_clean())');
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Source.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Source.php
new file mode 100644
index 0000000..9575fae
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Source.php
@@ -0,0 +1,48 @@
+<?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/
+ */
+
+
+/**
+ * i18n:source
+ *
+ * The i18n:source attribute specifies the language of the text to be
+ * translated. The default is "nothing", which means we don't provide
+ * this information to the translation services.
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.i18n
+ */
+class PHPTAL_Php_Attribute_I18N_Source extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ // ensure that a sources stack exists or create it
+ $codewriter->doIf('!isset($_i18n_sources)');
+ $codewriter->pushCode('$_i18n_sources = array()');
+ $codewriter->end();
+
+ // push current source and use new one
+ $codewriter->pushCode('$_i18n_sources[] = ' . $codewriter->getTranslatorReference(). '->setSource('.$codewriter->str($this->expression).')');
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ // restore source
+ $code = $codewriter->getTranslatorReference().'->setSource(array_pop($_i18n_sources))';
+ $codewriter->pushCode($code);
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Target.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Target.php
new file mode 100644
index 0000000..9cf2a67
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Target.php
@@ -0,0 +1,43 @@
+<?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/
+ */
+
+/**
+ * i18n:target
+ *
+ * The i18n:target attribute specifies the language of the translation we
+ * want to get. If the value is "default", the language negotiation services
+ * will be used to choose the destination language. If the value is
+ * "nothing", no translation will be performed; this can be used to suppress
+ * translation within a larger translated unit. Any other value must be a
+ * language code.
+ *
+ * The attribute value is a TALES expression; the result of evaluating the
+ * expression is the language code or one of the reserved values.
+ *
+ * Note that i18n:target is primarily used for hints to text extraction
+ * tools and translation teams. If you had some text that should only be
+ * translated to e.g. German, then it probably shouldn't be wrapped in an
+ * i18n:translate span.
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.i18n
+ */
+class PHPTAL_Php_Attribute_I18N_Target extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter){}
+ public function after(PHPTAL_Php_CodeWriter $codewriter){}
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Translate.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Translate.php
new file mode 100644
index 0000000..a0e26c2
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Translate.php
@@ -0,0 +1,130 @@
+<?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/
+ */
+
+/**
+ * ZPTInternationalizationSupport
+ *
+ * i18n:translate
+ *
+ * This attribute is used to mark units of text for translation. If this
+ * attribute is specified with an empty string as the value, the message ID
+ * is computed from the content of the element bearing this attribute.
+ * Otherwise, the value of the element gives the message ID.
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.i18n
+ */
+class PHPTAL_Php_Attribute_I18N_Translate extends PHPTAL_Php_Attribute_TAL_Content
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $escape = true;
+ $this->_echoType = PHPTAL_Php_Attribute::ECHO_TEXT;
+ if (preg_match('/^(text|structure)(?:\s+(.*)|\s*$)/', $this->expression, $m)) {
+ if ($m[1]=='structure') { $escape=false; $this->_echoType = PHPTAL_Php_Attribute::ECHO_STRUCTURE; }
+ $this->expression = isset($m[2])?$m[2]:'';
+ }
+
+ $this->_prepareNames($codewriter, $this->phpelement);
+
+ // 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));
+ if ('' === trim($key)) {
+ throw new PHPTAL_TemplateException("Empty translation key",
+ $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine());
+ }
+ $code = $codewriter->str($key);
+ } else {
+ $code = $codewriter->evaluateExpression($this->expression);
+ if (is_array($code))
+ return $this->generateChainedContent($codewriter, $code);
+
+ $code = $codewriter->evaluateExpression($this->expression);
+ }
+
+ $codewriter->pushCode('echo '.$codewriter->getTranslatorReference().'->translate('.$code.','.($escape ? 'true':'false').');');
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ }
+
+ public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast)
+ {
+ $codewriter = $executor->getCodeWriter();
+
+ $escape = !($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE);
+ $exp = $codewriter->getTranslatorReference()."->translate($exp, " . ($escape ? 'true':'false') . ')';
+ if (!$islast) {
+ $var = $codewriter->createTempVariable();
+ $executor->doIf('!phptal_isempty('.$var.' = '.$exp.')');
+ $codewriter->pushCode("echo $var");
+ $codewriter->recycleTempVariable($var);
+ } else {
+ $executor->doElse();
+ $codewriter->pushCode("echo $exp");
+ }
+ }
+
+ private function _getTranslationKey(PHPTAL_Dom_Node $tag, $preserve_tags, $encoding)
+ {
+ $result = '';
+ foreach ($tag->childNodes as $child) {
+ if ($child instanceof PHPTAL_Dom_Text) {
+ if ($preserve_tags) {
+ $result .= $child->getValueEscaped();
+ } else {
+ $result .= $child->getValue($encoding);
+ }
+ } elseif ($child instanceof PHPTAL_Dom_Element) {
+ if ($attr = $child->getAttributeNodeNS('http://xml.zope.org/namespaces/i18n', 'name')) {
+ $result .= '${' . $attr->getValue() . '}';
+ } else {
+
+ if ($preserve_tags) {
+ $result .= '<'.$child->getQualifiedName();
+ foreach ($child->getAttributeNodes() as $attr) {
+ if ($attr->getReplacedState() === PHPTAL_Dom_Attr::HIDDEN) continue;
+
+ $result .= ' '.$attr->getQualifiedName().'="'.$attr->getValueEscaped().'"';
+ }
+ $result .= '>'.$this->_getTranslationKey($child, $preserve_tags, $encoding) . '</'.$child->getQualifiedName().'>';
+ } else {
+ $result .= $this->_getTranslationKey($child, $preserve_tags, $encoding);
+ }
+ }
+ }
+ }
+ return $result;
+ }
+
+ private function _prepareNames(PHPTAL_Php_CodeWriter $codewriter, PHPTAL_Dom_Node $tag)
+ {
+ foreach ($tag->childNodes as $child) {
+ if ($child instanceof PHPTAL_Dom_Element) {
+ if ($child->hasAttributeNS('http://xml.zope.org/namespaces/i18n', 'name')) {
+ $child->generateCode($codewriter);
+ } else {
+ $this->_prepareNames($codewriter, $child);
+ }
+ }
+ }
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php
new file mode 100644
index 0000000..ef04840
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php
@@ -0,0 +1,67 @@
+<?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/
+ */
+
+/**
+ * METAL Specification 1.0
+ *
+ * argument ::= Name
+ *
+ * Example:
+ *
+ * <p metal:define-macro="copyright">
+ * Copyright 2001, <em>Foobar</em> Inc.
+ * </p>
+ *
+ * PHPTAL:
+ *
+ * <?php function XXX_macro_copyright($tpl) { ? >
+ * <p>
+ * Copyright 2001, <em>Foobar</em> Inc.
+ * </p>
+ * <?php } ? >
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.metal
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+class PHPTAL_Php_Attribute_METAL_DefineMacro extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $macroname = strtr(trim($this->expression), '-', '_');
+ if (!preg_match('/^[a-z0-9_]+$/i', $macroname)) {
+ throw new PHPTAL_ParserException('Bad macro name "'.$macroname.'"',
+ $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine());
+ }
+
+ if ($codewriter->functionExists($macroname)) {
+ throw new PHPTAL_TemplateException("Macro $macroname is defined twice",
+ $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine());
+ }
+
+ $codewriter->doFunction($macroname, 'PHPTAL $_thistpl, PHPTAL $tpl');
+ $codewriter->doSetVar('$tpl', 'clone $tpl');
+ $codewriter->doSetVar('$ctx', '$tpl->getContext()');
+ $codewriter->doInitTranslator();
+ $codewriter->doXmlDeclaration(true);
+ $codewriter->doDoctype(true);
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $codewriter->doEnd('function');
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.php
new file mode 100644
index 0000000..010849a
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.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/
+ */
+
+/**
+ * METAL Specification 1.0
+ *
+ * argument ::= Name
+ *
+ * Example:
+ *
+ * <table metal:define-macro="sidebar">
+ * <tr><th>Links</th></tr>
+ * <tr><td metal:define-slot="links">
+ * <a href="/">A Link</a>
+ * </td></tr>
+ * </table>
+ *
+ * PHPTAL: (access to slots may be renamed)
+ *
+ * <?php function XXXX_macro_sidebar($tpl) { ? >
+ * <table>
+ * <tr><th>Links</th></tr>
+ * <tr>
+ * <?php if (isset($tpl->slots->links)): ? >
+ * <?php echo $tpl->slots->links ? >
+ * <?php else: ? >
+ * <td>
+ * <a href="/">A Link</a>
+ * </td></tr>
+ * </table>
+ * <?php } ? >
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.metal
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+class PHPTAL_Php_Attribute_METAL_DefineSlot extends PHPTAL_Php_Attribute
+{
+ private $tmp_var;
+
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $this->tmp_var = $codewriter->createTempVariable();
+
+ $codewriter->doSetVar($this->tmp_var, $codewriter->interpolateTalesVarsInString($this->expression));
+ $codewriter->doIf('$ctx->hasSlot('.$this->tmp_var.')');
+ $codewriter->pushCode('$ctx->echoSlot('.$this->tmp_var.')');
+ $codewriter->doElse();
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $codewriter->doEnd('if');
+
+ $codewriter->recycleTempVariable($this->tmp_var);
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php
new file mode 100644
index 0000000..2dfda3a
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php
@@ -0,0 +1,148 @@
+<?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/
+ */
+
+/**
+ * METAL Specification 1.0
+ *
+ * argument ::= Name
+ *
+ * Example:
+ *
+ * <table metal:use-macro="here/doc1/macros/sidebar">
+ * <tr><th>Links</th></tr>
+ * <tr><td metal:fill-slot="links">
+ * <a href="http://www.goodplace.com">Good Place</a><br>
+ * <a href="http://www.badplace.com">Bad Place</a><br>
+ * <a href="http://www.otherplace.com">Other Place</a>
+ * </td></tr>
+ * </table>
+ *
+ * PHPTAL:
+ *
+ * 1. evaluate slots
+ *
+ * <?php ob_start(); ? >
+ * <td>
+ * <a href="http://www.goodplace.com">Good Place</a><br>
+ * <a href="http://www.badplace.com">Bad Place</a><br>
+ * <a href="http://www.otherplace.com">Other Place</a>
+ * </td>
+ * <?php $tpl->slots->links = ob_get_contents(); ob_end_clean(); ? >
+ *
+ * 2. call the macro (here not supported)
+ *
+ * <?php echo phptal_macro($tpl, 'master_page.html/macros/sidebar'); ? >
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.metal
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+class PHPTAL_Php_Attribute_METAL_FillSlot extends PHPTAL_Php_Attribute
+{
+ private static $uid = 0;
+ private $function_name;
+
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ if ($this->shouldUseCallback()) {
+ $function_base_name = 'slot_'.preg_replace('/[^a-z0-9]/', '_', $this->expression).'_'.(self::$uid++);
+ $codewriter->doFunction($function_base_name, 'PHPTAL $_thistpl, PHPTAL $tpl');
+ $this->function_name = $codewriter->getFunctionPrefix().$function_base_name;
+
+ $codewriter->doSetVar('$ctx', '$tpl->getContext()');
+ $codewriter->doInitTranslator();
+ } else {
+ $codewriter->pushCode('ob_start()');
+ $this->function_name = null;
+ }
+ }
+
+ 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())');
+ }
+ }
+
+ // rough guess
+ const CALLBACK_THRESHOLD = 10000;
+
+ /**
+ * inspects contents of the element to decide whether callback makes sense
+ */
+ private function shouldUseCallback()
+ {
+ // since callback is slightly slower than buffering,
+ // use callback only for content that is large to offset speed loss by memory savings
+ return $this->estimateNumberOfBytesOutput($this->phpelement, false) > self::CALLBACK_THRESHOLD;
+ }
+
+ /**
+ * @param bool $is_nested_in_repeat true if any parent element has tal:repeat
+ *
+ * @return rough guess
+ */
+ private function estimateNumberOfBytesOutput(PHPTAL_Dom_Element $element, $is_nested_in_repeat)
+ {
+ // macros don't output anything on their own
+ if ($element->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'define-macro')) {
+ return 0;
+ }
+
+ $estimated_bytes = 2*(3+strlen($element->getQualifiedName()));
+
+ foreach ($element->getAttributeNodes() as $attr) {
+ $estimated_bytes += 4+strlen($attr->getQualifiedName());
+ if ($attr->getReplacedState() === PHPTAL_Dom_Attr::NOT_REPLACED) {
+ $estimated_bytes += strlen($attr->getValueEscaped()); // this is shoddy for replaced attributes
+ }
+ }
+
+ $has_repeat_attr = $element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'repeat');
+
+ if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'content') ||
+ $element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'replace')) {
+ // assume that output in loops is shorter (e.g. table rows) than outside (main content)
+ $estimated_bytes += ($has_repeat_attr || $is_nested_in_repeat) ? 500 : 2000;
+ } else {
+ foreach ($element->childNodes as $node) {
+ if ($node instanceof PHPTAL_Dom_Element) {
+ $estimated_bytes += $this->estimateNumberOfBytesOutput($node, $has_repeat_attr || $is_nested_in_repeat);
+ } else {
+ $estimated_bytes += strlen($node->getValueEscaped());
+ }
+ }
+ }
+
+ if ($element->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'use-macro')) {
+ $estimated_bytes += ($has_repeat_attr || $is_nested_in_repeat) ? 500 : 2000;
+ }
+
+ if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'condition')) {
+ $estimated_bytes /= 2; // naively assuming 50% chance, that works well with if/else pattern
+ }
+
+ if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'repeat')) {
+ // assume people don't write big nested loops
+ $estimated_bytes *= $is_nested_in_repeat ? 5 : 10;
+ }
+
+ return $estimated_bytes;
+ }
+}
diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php
new file mode 100644
index 0000000..83e8144
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php
@@ -0,0 +1,135 @@
+<?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/
+ */
+
+/**
+ * METAL Specification 1.0
+ *
+ * argument ::= expression
+ *
+ * Example:
+ *
+ * <hr />
+ * <p metal:use-macro="here/master_page/macros/copyright">
+ * <hr />
+ *
+ * PHPTAL: (here not supported)
+ *
+ * <?php echo phptal_macro( $tpl, 'master_page.html/macros/copyright'); ? >
+ *
+ *
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.metal
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+class PHPTAL_Php_Attribute_METAL_UseMacro extends PHPTAL_Php_Attribute
+{
+ static $ALLOWED_ATTRIBUTES = array(
+ 'fill-slot'=>'http://xml.zope.org/namespaces/metal',
+ 'define-macro'=>'http://xml.zope.org/namespaces/metal',
+ 'define'=>'http://xml.zope.org/namespaces/tal',
+ );
+
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $this->pushSlots($codewriter);
+
+ foreach ($this->phpelement->childNodes as $child) {
+ $this->generateFillSlots($codewriter, $child);
+ }
+
+ $macroname = strtr($this->expression, '-', '_');
+
+ // throw error if attempting to define and use macro at same time
+ // [should perhaps be a TemplateException? but I don't know how to set that up...]
+ if ($defineAttr = $this->phpelement->getAttributeNodeNS(
+ 'http://xml.zope.org/namespaces/metal', 'define-macro')) {
+ if ($defineAttr->getValue() == $macroname)
+ throw new PHPTAL_TemplateException("Cannot simultaneously define and use macro '$macroname'",
+ $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine());
+ }
+
+ // local macro (no filename specified) and non dynamic macro name
+ // can be called directly if it's a known function (just generated or seen in previous compilation)
+ if (preg_match('/^[a-z0-9_]+$/i', $macroname) && $codewriter->functionExists($macroname)) {
+ $code = $codewriter->getFunctionPrefix() . $macroname . '($_thistpl, $tpl)';
+ $codewriter->pushCode($code);
+ }
+ // external macro or ${macroname}, use PHPTAL at runtime to resolve it
+ else {
+ $code = $codewriter->interpolateTalesVarsInString($this->expression);
+ $codewriter->pushCode('$tpl->_executeMacroOfTemplate('.$code.', $_thistpl)');
+ }
+
+ $this->popSlots($codewriter);
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ }
+
+ /**
+ * reset template slots on each macro call ?
+ *
+ * NOTE: defining a macro and using another macro on the same tag
+ * means inheriting from the used macro, thus slots are shared, it
+ * is a little tricky to understand but very natural to use.
+ *
+ * For example, we may have a main design.html containing our main
+ * website presentation with some slots (menu, content, etc...) then
+ * we may define a member.html macro which use the design.html macro
+ * for the general layout, fill the menu slot and let caller templates
+ * fill the parent content slot without interfering.
+ */
+ private function pushSlots(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ if (!$this->phpelement->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'define-macro')) {
+ $codewriter->pushCode('$ctx->pushSlots()');
+ }
+ }
+
+ /**
+ * 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()');
+ }
+ }
+
+ /**
+ * recursively generates code for slots
+ */
+ private function generateFillSlots(PHPTAL_Php_CodeWriter $codewriter, PHPTAL_Dom_Node $phpelement)
+ {
+ if (false == ($phpelement instanceof PHPTAL_Dom_Element)) {
+ return;
+ }
+
+ // if the tag contains one of the allowed attribute, we generate it
+ foreach (self::$ALLOWED_ATTRIBUTES as $qname => $uri) {
+ if ($phpelement->hasAttributeNS($uri, $qname)) {
+ $phpelement->generateCode($codewriter);
+ return;
+ }
+ }
+
+ foreach ($phpelement->childNodes as $child) {
+ $this->generateFillSlots($codewriter, $child);
+ }
+ }
+}
diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Cache.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Cache.php
new file mode 100644
index 0000000..3504a22
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Cache.php
@@ -0,0 +1,97 @@
+<?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/
+ */
+
+/**
+ * phptal:cache (note that's not tal:cache) caches element's HTML for a given time. Time is a number with 'd', 'h', 'm' or 's' suffix.
+ * There's optional parameter that defines how cache should be shared. By default cache is not sensitive to template's context at all
+ * - it's shared between all pages that use that template.
+ * You can add per url to have separate copy of given element for every URL.
+ *
+ * You can add per expression to have different cache copy for every different value of an expression (which MUST evaluate to a string).
+ * Expression cannot refer to variables defined using tal:define on the same element.
+ *
+ * NB:
+ * * phptal:cache blocks can be nested, but outmost block will cache other blocks regardless of their freshness.
+ * * you cannot use metal:fill-slot inside elements with phptal:cache
+ *
+ * Examples:
+ * <div phptal:cache="3h">...</div> <!-- <div> to be evaluated at most once per 3 hours. -->
+ * <ul phptal:cache="1d per object/id">...</ul> <!-- <ul> be cached for one day, separately for each object. -->
+ *
+ * @package PHPTAL
+ * @subpackage Php.attribute.phptal
+*/
+class PHPTAL_Php_Attribute_PHPTAL_Cache extends PHPTAL_Php_Attribute
+{
+ private $cache_filename_var;
+
+ 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 */
+ case 'h': $cache_len .= '*60'; /* 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()');
+ }
+
+ 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);
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Debug.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Debug.php
new file mode 100644
index 0000000..d0e9c9b
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Debug.php
@@ -0,0 +1,34 @@
+<?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.phptal
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+class PHPTAL_Php_Attribute_PHPTAL_Debug extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $this->_oldMode = $codewriter->setDebug(true);
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $codewriter->setDebug($this->_oldMode);
+ }
+
+ private $_oldMode;
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Id.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Id.php
new file mode 100644
index 0000000..dbee2a9
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Id.php
@@ -0,0 +1,53 @@
+<?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.phptal
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+class PHPTAL_Php_Attribute_PHPTAL_ID extends PHPTAL_Php_Attribute
+{
+ private $var;
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ // retrieve trigger
+ $this->var = $codewriter->createTempVariable();
+
+ $codewriter->doSetVar(
+ $this->var,
+ '$tpl->getTrigger('.$codewriter->str($this->expression).')'
+ );
+
+ // if trigger found and trigger tells to proceed, we execute
+ // the node content
+ $codewriter->doIf($this->var.' &&
+ '.$this->var.'->start('.$codewriter->str($this->expression).', $tpl) === PHPTAL_Trigger::PROCEED');
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ // end of if PROCEED
+ $codewriter->doEnd('if');
+
+ // 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('if');
+ $codewriter->recycleTempVariable($this->var);
+ }
+}
+
diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Tales.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Tales.php
new file mode 100644
index 0000000..f1fb4d7
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Tales.php
@@ -0,0 +1,45 @@
+<?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.phptal
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+class PHPTAL_Php_Attribute_PHPTAL_TALES extends PHPTAL_Php_Attribute
+{
+ public function before(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $mode = trim($this->expression);
+ $mode = strtolower($mode);
+
+ if ($mode == '' || $mode == 'default')
+ $mode = 'tales';
+
+ if ($mode != 'php' && $mode != 'tales') {
+ throw new PHPTAL_TemplateException("Unsupported TALES mode '$mode'",
+ $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine());
+ }
+
+ $this->_oldMode = $codewriter->setTalesMode($mode);
+ }
+
+ public function after(PHPTAL_Php_CodeWriter $codewriter)
+ {
+ $codewriter->setTalesMode($this->_oldMode);
+ }
+
+ private $_oldMode;
+}
+
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);
+ }
+}
+