Done dynamic extends

This commit is contained in:
Ivan Shalganov
2014-02-26 23:57:29 +04:00
parent 9c0cea5934
commit 000009a5d3
8 changed files with 72 additions and 20 deletions

View File

@@ -483,8 +483,9 @@ class Compiler
} else {
$tpl->dynamic_extends = $cname;
}
if(!$tpl->extended) {
if(!$tpl->extend_body) {
$tpl->addPostCompile(__CLASS__ . "::extendBody");
$tpl->extend_body = true;
}
}
@@ -496,18 +497,23 @@ class Compiler
public static function extendBody($tpl, &$body)
{
if($tpl->dynamic_extends) {
$body = "";
foreach($tpl->blocks as $name => $block) {
$body .= '<?php $tpl->blocks["'.$name.'"] = function ($var, $tpl) { ?>'.PHP_EOL.$block['block'].'<?php } ?>'.PHP_EOL.PHP_EOL;
if(!$tpl->ext_stack) {
$tpl->ext_stack[] = $tpl->getName();
}
$body .= '<?php $tpl->getStorage()->getTemplate('.$tpl->dynamic_extends.', \Fenom\Template::DYNAMIC_EXTEND)->display($var); ?>';
foreach($tpl->ext_stack as &$t) {
$stack[] = "'$t'";
}
$stack[] = $tpl->dynamic_extends;
$body = '<?php $tpl->getStorage()->display(array('.implode(', ', $stack).'), $var); ?>';
} else {
$child = $tpl;
while($child && $child->extends) {
$parent = $tpl->extend($child->extends);
$child = $parent->extends ? $parent : false;
}
$tpl->extends = false;
}
$tpl->extend_body = false;
}
/**

View File

@@ -89,6 +89,7 @@ class Render extends \ArrayObject
$this->_time = $props["time"];
$this->_depends = $props["depends"];
$this->_macros = $props["macros"];
// $this->_blocks = $props["blocks"];
$this->_code = $code;
}

View File

@@ -74,7 +74,14 @@ class Template extends Render
* @var string|null
*/
public $extended;
// public $_compatible;
/**
* Stack of extended templates
* @var array
*/
public $ext_stack = array();
public $extend_body = false;
/**
* Template PHP code
@@ -465,7 +472,6 @@ class Template extends Render
// evaluate template's code
eval("\$this->_code = " . $this->_getClosureSource() . ";\n\$this->_macros = " . $this->_getMacrosArray() . ';');
if (!$this->_code) {
var_dump($this->getBody());exit;
throw new CompileException("Fatal error while creating the template");
}
}
@@ -501,7 +507,8 @@ class Template extends Render
* Import block from another template
* @param string $tpl
*/
public function importBlocks($tpl) {
public function importBlocks($tpl)
{
$donor = $this->_fenom->compile($tpl, false);
foreach($donor->blocks as $name => $block) {
if(!isset($this->blocks[ $name ])) {
@@ -517,14 +524,20 @@ class Template extends Render
* @param string $tpl
* @return \Fenom\Template parent
*/
public function extend($tpl) {
public function extend($tpl)
{
if(!$this->_body) {
$this->compile();
}
$parent = $this->_fenom->getRawTemplate()->load($tpl, false);
$parent->blocks = &$this->blocks;
$parent->extended = $this->getName();
if(!$this->ext_stack) {
$this->ext_stack[] = $this->getName();
}
$this->ext_stack[] = $parent->getName();
$parent->_options = $this->_options;
$parent->ext_stack = $this->ext_stack;
$parent->compile();
$this->_body = $parent->_body;
$this->_src = $parent->_src;