summaryrefslogtreecommitdiff
path: root/lib/phptal/PHPTAL/Php/Attribute.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-10-31 21:58:33 +0100
committeremkael <emkael@tlen.pl>2016-10-31 21:59:22 +0100
commitd216b3147bc3f37cf2337acab5767c6a4f74aa2e (patch)
tree6090989e5071db101a1112131e2b075a02dccbc4 /lib/phptal/PHPTAL/Php/Attribute.php
parentb23bfbb17d1d5f6852a1690f246a84c2d38ae969 (diff)
* PHPTAL library
Diffstat (limited to 'lib/phptal/PHPTAL/Php/Attribute.php')
-rw-r--r--lib/phptal/PHPTAL/Php/Attribute.php98
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/phptal/PHPTAL/Php/Attribute.php b/lib/phptal/PHPTAL/Php/Attribute.php
new file mode 100644
index 0000000..ceb8a12
--- /dev/null
+++ b/lib/phptal/PHPTAL/Php/Attribute.php
@@ -0,0 +1,98 @@
+<?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/
+ */
+
+/**
+ * Base class for all PHPTAL attributes.
+ *
+ * Attributes are first ordered by PHPTAL then called depending on their
+ * priority before and after the element printing.
+ *
+ * An attribute must implements start() and end().
+ *
+ * @package PHPTAL
+ * @subpackage Php
+ * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
+ */
+abstract class PHPTAL_Php_Attribute
+{
+ const ECHO_TEXT = 'text';
+ const ECHO_STRUCTURE = 'structure';
+
+ /** Attribute value specified by the element. */
+ protected $expression;
+
+ /** Element using this attribute (PHPTAL's counterpart of XML node) */
+ protected $phpelement;
+
+ /**
+ * Called before element printing.
+ */
+ abstract function before(PHPTAL_Php_CodeWriter $codewriter);
+
+ /**
+ * Called after element printing.
+ */
+ abstract function after(PHPTAL_Php_CodeWriter $codewriter);
+
+ function __construct(PHPTAL_Dom_Element $phpelement, $expression)
+ {
+ $this->expression = $expression;
+ $this->phpelement = $phpelement;
+ }
+
+ /**
+ * Remove structure|text keyword from expression and stores it for later
+ * doEcho() usage.
+ *
+ * $expression = 'stucture my/path';
+ * $expression = $this->extractEchoType($expression);
+ *
+ * ...
+ *
+ * $this->doEcho($code);
+ */
+ protected function extractEchoType($expression)
+ {
+ $echoType = self::ECHO_TEXT;
+ $expression = trim($expression);
+ if (preg_match('/^(text|structure)\s+(.*?)$/ism', $expression, $m)) {
+ list(, $echoType, $expression) = $m;
+ }
+ $this->_echoType = strtolower($echoType);
+ return trim($expression);
+ }
+
+ protected function doEchoAttribute(PHPTAL_Php_CodeWriter $codewriter, $code)
+ {
+ if ($this->_echoType === self::ECHO_TEXT)
+ $codewriter->doEcho($code);
+ else
+ $codewriter->doEchoRaw($code);
+ }
+
+ protected function parseSetExpression($exp)
+ {
+ $exp = trim($exp);
+ // (dest) (value)
+ if (preg_match('/^([a-z0-9:\-_]+)\s+(.*?)$/si', $exp, $m)) {
+ return array($m[1], trim($m[2]));
+ }
+ // (dest)
+ return array($exp, null);
+ }
+
+ protected $_echoType = PHPTAL_Php_Attribute::ECHO_TEXT;
+}
+