Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created June 6, 2011 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakahisateru/1010418 to your computer and use it in GitHub Desktop.
Save tanakahisateru/1010418 to your computer and use it in GitHub Desktop.
PHPTALのnamespace拡張とりあえずTALES無視
<?php
require_once 'PHPTAL.php';
// namespace def
class MYNS_Namespace extends PHPTAL_Namespace
{
public function __construct()
{
// namespace
parent::__construct('myns', 'http://xml.zope.org/namespaces/mytal');
// attributes in namescape
$this->addAttribute(new PHPTAL_NamespaceAttributeContent('myattr', 5));
$this->addAttribute(new PHPTAL_NamespaceAttributeSurround('yourattr', 5));
}
public function createAttributeHandler(
PHPTAL_NamespaceAttribute $att,
PHPTAL_Dom_Element $tag,
$expression
) {
$attrNames = array(
'myattr' => 'MYNS_MyAttribute',
'yourattr' => 'MYNS_YourAttribute',
);
$class = $attrNames[$att->getLocalName()];
return new $class($tag, $expression);
}
}
// attributes def
class MYNS_MyAttribute extends PHPTAL_Php_Attribute {
public function __construct($tag, $expression)
{
$this->expression = $expression;
$this->_echoType = self::ECHO_TEXT;
}
public function before(PHPTAL_Php_CodeWriter $codewriter)
{
$this->doEchoAttribute($codewriter,
sprintf("'%s キュンキュン><'", strval($this->expression))
);
}
public function after(PHPTAL_Php_CodeWriter $codewriter)
{
}
}
class MYNS_YourAttribute extends PHPTAL_Php_Attribute {
public function __construct($tag, $expression)
{
$this->expression = $expression;
$this->_echoType = self::ECHO_STRUCTURE;
}
public function before(PHPTAL_Php_CodeWriter $codewriter)
{
$this->doEchoAttribute($codewriter,
sprintf('\'<div style="border:%dpx solid red;">\'', $this->expression)
);
}
public function after(PHPTAL_Php_CodeWriter $codewriter)
{
$this->doEchoAttribute($codewriter,
sprintf("'%s'", '</div>')
);
}
}
PHPTAL_Dom_Defs::getInstance()->registerNamespace(new MYNS_Namespace());
$tpl = new PHPTAL();
$tpl->setPhpCodeDestination(".");
$tpl->setForceReparse(true);
$src = <<<EOT
<html>
<head>
<title tal:content="title">title</title>
</head>
<body>
<h1 myns:myattr="hoge">hoge in h1</h1>
<div myns:yourattr="8">
<p>fuga in div&gt;p</p>
</div>
</body>
</html>
EOT;
$tpl->setSource($src);
$tpl->title = "this is my title";
$tpl->echoExecute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment