Done extends

This commit is contained in:
bzick 2013-03-04 12:40:32 +04:00
parent c00a2c708a
commit fdbb4dac6d
4 changed files with 65 additions and 44 deletions

View File

@ -320,6 +320,10 @@ class Aspect {
return $this; return $this;
} }
public function addCompilerSmart($class) {
return $this;
}
/** /**
* Add block compiler * Add block compiler
* *
@ -339,6 +343,16 @@ class Aspect {
return $this; return $this;
} }
/**
* @param string $class
* @param array $tags
* @param array $floats
* @return Aspect
*/
public function addBlockCompilerSmart($class, array $tags, array $floats = array()) {
return $this;
}
/** /**
* @param string $function * @param string $function
* @param callable $callback * @param callable $callback

View File

@ -375,7 +375,9 @@ class Compiler {
throw new ImproperUseException("Only one {extends} allowed"); throw new ImproperUseException("Only one {extends} allowed");
} }
$tpl_name = $tpl->parseFirstArg($tokens, $name); $tpl_name = $tpl->parseFirstArg($tokens, $name);
$tpl->addPostCompile(__CLASS__."::extendBody"); if(empty($tpl->_extended)) {
$tpl->addPostCompile(__CLASS__."::extendBody");
}
if($name) { // static extends if($name) { // static extends
$tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false); $tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false);
$tpl->_compatible = &$tpl->_extends->_compatible; $tpl->_compatible = &$tpl->_extends->_compatible;
@ -388,44 +390,32 @@ class Compiler {
} }
/** /**
* Post compile method for {extends ...} tag * Post compile action for {extends ...} tag
* @param $body * @param $body
* @param Template $tpl * @param Template $tpl
*/ */
public static function extendBody(&$body, $tpl) { public static function extendBody(&$body, $tpl) {
if(isset($tpl->_extends)) { // is child $t = $tpl;
if(is_object($tpl->_extends)) { // static extends while(isset($t->_extends)) {
/* @var Template $t */ $t = $t->_extends;
$tpl->_extends->_extended = true; if(is_object($t)) {
$tpl->_extends->blocks = &$tpl->blocks; $t->_extended = true;
$tpl->_extends->compile(); $t->_compatible = &$tpl->_compatible;
if($tpl->_compatible) { $t->blocks = &$tpl->blocks;
$body .= $tpl->_extends->_body; $t->compile();
if(!isset($t->_extends)) { // last item => parent
if(empty($tpl->_compatible)) {
$body = $t->getBody();
} else {
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); ?>'.$t->getBody();
}
return;
} else { } else {
$body = $tpl->_extends->_body; $body .= $t->getBody();
} }
/*if(empty($tpl->_dynamic)) { } else {
do {
$t->_blocks = &$tpl->_blocks;
$t->compile();
$tpl->addDepend($t);
if(!empty($t->_dynamic)) {
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); ?>'.$t->_body;
return;
} else {
$body .= $t->_body;
}
} while(isset($t->_extends) && $t = $t->_extends);
$body = $t->_body;
} else {
$t->_blocks = &$tpl->_blocks;
$t->_dyn = &$tpl->_dynamic;
$t->compile();
$tpl->addDepend($t);
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); ?>'.$t->_body;
}*/
} else { // dynamic extends
$body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); $parent->b = &$tpl->b; $parent->display((array)$tpl); unset($tpl->b, $parent->b); ?>'; $body = '<?php ob_start(); ?>'.$body.'<?php ob_end_clean(); $parent->b = &$tpl->b; $parent->display((array)$tpl); unset($tpl->b, $parent->b); ?>';
return;
} }
} }
} }
@ -433,11 +423,27 @@ class Compiler {
/** /**
* @param Tokenizer $tokens * @param Tokenizer $tokens
* @param Template $tpl * @param Template $tpl
* @throws ImproperUseException
* @return string
*/ */
public static function tagUse(Tokenizer $tokens, Template $tpl) { public static function tagUse(Tokenizer $tokens, Template $tpl) {
$p = $tpl->parseFirstArg($tokens, $scalar); $tpl->parseFirstArg($tokens, $name);
if(!$scalar) { if($name) {
$tpl->_static = false; $donor = $tpl->getStorage()->getRawTemplate()->load($name, false);
$donor->_extended = true;
$tpl->_compatible = &$donor->_compatible;
$donor->compile();
if(empty($tpl->_compatible)) {
$tpl->blocks += $donor->blocks;
}
return '?>'.$donor->getBody().'<?php ';
} else {
throw new ImproperUseException('template name must be given explicitly');
//return '';
//return '$donor = $tpl->getStorage()->getTemplate('.$cname.'); ';
//$tpl->_compatible = true;
//$tpl->_ = false;
} }
} }
@ -470,15 +476,15 @@ class Compiler {
'<?php if(empty($tpl->blocks['.$scope["cname"].'])) { '. '<?php if(empty($tpl->blocks['.$scope["cname"].'])) { '.
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL. '$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
$scope->getContent(). $scope->getContent().
"};". "<?php };".
"<?php } ?>".PHP_EOL "} ?>".PHP_EOL
); );
} else { } else {
$tpl->blocks[ $scope["name"] ] = $scope->getContent(); $tpl->blocks[ $scope["name"] ] = $scope->getContent();
$scope->replaceContent( $scope->replaceContent(
'<?php $tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL. '<?php $tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
$scope->getContent(). $scope->getContent().
"<?php }; ?php".PHP_EOL "<?php }; ?>".PHP_EOL
); );
} }
} }
@ -488,8 +494,8 @@ class Compiler {
'<?php if(empty($tpl->b['.$scope["cname"].'])) { '. '<?php if(empty($tpl->b['.$scope["cname"].'])) { '.
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL. '$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
$scope->getContent(). $scope->getContent().
"};". "<?php };".
"<?php } ?>".PHP_EOL "} ?>".PHP_EOL
); );
} }
} else { // is parent } else { // is parent
@ -497,9 +503,10 @@ class Compiler {
if($tpl->_compatible) { // compatible mode enabled if($tpl->_compatible) { // compatible mode enabled
$scope->replaceContent( $scope->replaceContent(
'<?php if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL. '<?php if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
$tpl->blocks[ $scope["body"] ]. $tpl->blocks[ $scope["name"] ].
'<?php } ?>'.PHP_EOL '<?php } ?>'.PHP_EOL
); );
} else { } else {
$scope->replaceContent($tpl->blocks[ $scope["name"] ]); $scope->replaceContent($tpl->blocks[ $scope["name"] ]);
} }

View File

@ -195,7 +195,7 @@ class Template extends Render {
call_user_func_array($cb, array(&$this->_body, $this)); call_user_func_array($cb, array(&$this->_body, $this));
} }
} }
$this->_body = str_replace(array('?>'.PHP_EOL.'<?php ', '?><?php'), array(PHP_EOL, ' '), $this->_body); /*$this->_body = str_replace(array('?>'.PHP_EOL.'<?php ', '?><?php'), array(PHP_EOL, ' '), $this->_body);*/
} }
/** /**

View File

@ -43,10 +43,10 @@ class ExtendsTemplateTest extends TestCase {
* @param $vars * @param $vars
* @param $result * @param $result
*/ */
public function _testDynamicExtends($name, $code, $vars, $result) { public function testDynamicExtends($name, $code, $vars, $result) {
static $i = 0; static $i = 0;
$vars["iteration"] = $i++; $vars["iteration"] = $i++;
$this->execTpl($name, $code, $vars, $result); $this->execTpl($name, $code, $vars, $result, 0);
} }
/** /**