Fix extends with dynamic blocks

This commit is contained in:
bzick
2013-05-17 22:20:29 +04:00
parent 5a1097d5a6
commit 12a0c9589f
8 changed files with 81 additions and 108 deletions

View File

@ -391,7 +391,10 @@ class Compiler {
}*/
if($name) { // static extends
$tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false);
$tpl->_compatible = &$tpl->_extends->_compatible;
// $tpl->_compatible = &$tpl->_extends->_compatible;
if(!isset($tpl->_compatible)) {
$tpl->_compatible = &$tpl->_extends->_compatible;;
}
$tpl->addDepend($tpl->_extends);
return "";
} else { // dynamic extends
@ -411,6 +414,7 @@ class Compiler {
$t = $t->_extends;
if(is_object($t)) {
$t->_extended = true;
$tpl->addDepend($t);
$t->_compatible = &$tpl->_compatible;
$t->blocks = &$tpl->blocks;
$t->compile();
@ -451,11 +455,6 @@ class Compiler {
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;
}
}
@ -482,28 +481,26 @@ class Compiler {
$tpl = $scope->tpl;
if(isset($tpl->_extends)) { // is child
if($scope["name"]) { // is scalar name
if(!isset($tpl->blocks[ $scope["name"] ])) { // is block still doesn't preset
if($tpl->_compatible) { // is compatible mode
$scope->replaceContent(
'<?php if(empty($tpl->blocks['.$scope["cname"].'])) { '.
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
$scope->getContent().
"<?php };".
"} ?>".PHP_EOL
);
} else {
$tpl->blocks[ $scope["name"] ] = $scope->getContent();
$scope->replaceContent(
'<?php $tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
if($tpl->_compatible) { // is compatible mode
$scope->replaceContent(
'<?php /* Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(empty($tpl->b['.$scope["cname"].'])) { '.
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
$scope->getContent().
"<?php }; ?>".PHP_EOL
);
}
"<?php };".
"} ?>".PHP_EOL
);
} elseif(!isset($tpl->blocks[ $scope["name"] ])) { // is block not registered
$tpl->blocks[ $scope["name"] ] = $scope->getContent();
$scope->replaceContent(
'<?php /* Block '.$tpl.': '.$scope["cname"].' '.$tpl->_compatible.' */'.PHP_EOL.' $tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
$scope->getContent().
"<?php }; ?>".PHP_EOL
);
}
} else { // dynamic name
$tpl->_compatible = true; // enable compatible mode
$scope->replaceContent(
'<?php if(empty($tpl->b['.$scope["cname"].'])) { '.
'<?php /* Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(empty($tpl->b['.$scope["cname"].'])) { '.
'$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL.
$scope->getContent().
"<?php };".
@ -514,7 +511,7 @@ class Compiler {
if(isset($tpl->blocks[ $scope["name"] ])) { // has block
if($tpl->_compatible) { // compatible mode enabled
$scope->replaceContent(
'<?php if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
'<?php /* Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
$tpl->blocks[ $scope["name"] ].
'<?php } ?>'.PHP_EOL
);
@ -524,7 +521,7 @@ class Compiler {
}
} elseif(isset($tpl->_extended) && $tpl->_compatible || empty($tpl->_extended)) {
$scope->replaceContent(
'<?php if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
'<?php /* Block '.$tpl.': '.$scope["cname"].' */'.PHP_EOL.' if(isset($tpl->b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL.
$scope->getContent().
'<?php } ?>'.PHP_EOL
);

View File

@ -80,8 +80,8 @@ class Modifier {
}
/**
* Crop string by length
* UTF8 support
* Crop string by length (support unicode)
*
* @param string $string text witch will be truncate
* @param int $length maximum symbols of result string
* @param string $etc place holder truncated symbols

View File

@ -13,7 +13,7 @@ use Cytro;
/**
* Template compiler
*
* @package aspect
* @package Cytro
* @author Ivan Shalganov <owner@bzick.net>
*/
class Template extends Render {
@ -64,6 +64,8 @@ class Template extends Render {
*/
private $_ignore = false;
private $_filter = array();
/**
* Just factory
*
@ -129,12 +131,12 @@ class Template extends Render {
* @throws CompileException
*/
public function compile() {
if(!isset($this->_src)) {
if(!isset($this->_src)) { // already compiled
return;
}
$pos = 0;
$frag = "";
while(($start = strpos($this->_src, '{', $pos)) !== false) { // search open-char of tags
while(($start = strpos($this->_src, '{', $pos)) !== false) { // search open-symbol of tags
switch($this->_src[$start + 1]) { // check next char
case "\n": case "\r": case "\t": case " ": case "}": // ignore the tag
$pos = $start + 1; // try find tags after the current char
@ -174,7 +176,7 @@ class Template extends Render {
$this->_appendText($_frag);
$tokens = new Tokenizer($_tag);
$this->_appendCode($this->_tag($tokens), $tag);
if($tokens->key()) { // if tokenizer still have tokens
if($tokens->key()) { // if tokenizer have tokens - throws exceptions
throw new CompileException("Unexpected token '".$tokens->current()."' in {$this} line {$this->_line}, near '{".$tokens->getSnippetAsString(0,0)."' <- there", 0, E_ERROR, $this->_name, $this->_line);
}
@ -217,11 +219,17 @@ class Template extends Render {
private function _appendText($text) {
if($this->_filter) {
if(strpos($text, "<?") === false) {
foreach(explode("<?", $text) as $fragment) {
}
} else {
$this->_body .= $text;
} else {
$fragments = explode("<?", $text);
foreach($fragments as &$fragment) {
if($fragment) {
foreach($this->_filter as $filter) {
$fragment = call_user_func($filter, $fragment);
}
}
}
$this->_body .= implode('<?php echo "<?"; ?>', $fragments);
}
} else {
$this->_body .= str_replace("<?", '<?php echo "<?"; ?>'.PHP_EOL, $text);