diff options
Diffstat (limited to 'lib/phptal/PHPTAL/Php/Attribute/METAL')
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php | 67 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.php | 70 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php | 148 | ||||
-rw-r--r-- | lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php | 135 |
4 files changed, 420 insertions, 0 deletions
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); + } + } +} |