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 { } else {
$tpl->dynamic_extends = $cname; $tpl->dynamic_extends = $cname;
} }
if(!$tpl->extended) { if(!$tpl->extend_body) {
$tpl->addPostCompile(__CLASS__ . "::extendBody"); $tpl->addPostCompile(__CLASS__ . "::extendBody");
$tpl->extend_body = true;
} }
} }
@ -496,18 +497,23 @@ class Compiler
public static function extendBody($tpl, &$body) public static function extendBody($tpl, &$body)
{ {
if($tpl->dynamic_extends) { if($tpl->dynamic_extends) {
$body = ""; if(!$tpl->ext_stack) {
foreach($tpl->blocks as $name => $block) { $tpl->ext_stack[] = $tpl->getName();
$body .= '<?php $tpl->blocks["'.$name.'"] = function ($var, $tpl) { ?>'.PHP_EOL.$block['block'].'<?php } ?>'.PHP_EOL.PHP_EOL;
} }
$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 { } else {
$child = $tpl; $child = $tpl;
while($child && $child->extends) { while($child && $child->extends) {
$parent = $tpl->extend($child->extends); $parent = $tpl->extend($child->extends);
$child = $parent->extends ? $parent : false; $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->_time = $props["time"];
$this->_depends = $props["depends"]; $this->_depends = $props["depends"];
$this->_macros = $props["macros"]; $this->_macros = $props["macros"];
// $this->_blocks = $props["blocks"];
$this->_code = $code; $this->_code = $code;
} }

View File

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

View File

@ -10,13 +10,7 @@ class ExtendsTest extends TestCase
public function _testSandbox() public function _testSandbox()
{ {
try { try {
// var_dump($this->fenom->getTemplate("extends/static/nested/child.1.tpl")->fetch([])); var_dump($this->fenom->getTemplate('extends/dynamic/child.3.tpl')->getBody());
// var_dump($this->fenom->getTemplate("extends/dynamic/child.2.tpl")->getBody());
// var_dump($this->fenom->compileCode('{block "main"}a {parent} b{/block}')->getBody());
// $child = $this->fenom->getRawTemplate()->load('autoextends/child.1.tpl', false);
// $child->extend('autoextends/parent.tpl');
// $child->compile();
// print_r($child->getBody());
} catch (\Exception $e) { } catch (\Exception $e) {
echo "$e"; echo "$e";
} }
@ -75,6 +69,20 @@ Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/static/child.3.tpl', array())); $this->assertSame($result, $this->fenom->fetch('extends/static/child.3.tpl', array()));
} }
public function testAutoAndStaticExtend() {
$result = "Before header
Child 2 header
Before body
Child 3 content
Before footer
Footer from use";
$this->assertSame($result, $this->fenom->fetch(array(
'extends/auto/child.3.tpl',
'extends/auto/child.2.tpl',
'extends/auto/static/child.1.tpl'
), array()));
}
public function testStaticExtendNested() { public function testStaticExtendNested() {
$result = "Before body $result = "Before body
@ -86,15 +94,34 @@ Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/static/nested/child.1.tpl', array())); $this->assertSame($result, $this->fenom->fetch('extends/static/nested/child.1.tpl', array()));
} }
public function _testDynamicExtendLevel2() { public function testDynamicExtendLevel2() {
$result = "Before header $result = "Before header
Content of the header Child 2 header
Before body Before body
Child 1 Body Child 1 Body
Before footer Before footer
Content of the footer"; Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/dynamic/child.2.tpl', array())); $this->assertSame($result, $this->fenom->fetch('extends/dynamic/child.2.tpl', array()));
} }
public function testDynamicExtendLevel3() {
$result = "Before header
Child 2 header
Before body
Child 3 content
Before footer
Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/dynamic/child.3.tpl', array()));
}
public function testDynamicExtendLevel4() {
$result = "Before header
Child 2 header
Before body
Child 3 content
Before footer
Footer from use";
$this->assertSame($result, $this->fenom->fetch('extends/dynamic/child.4.tpl', array()));
}
} }

View File

@ -0,0 +1,2 @@
{extends 'extends/auto/parent.tpl'}
{block 'body'}Child 1 {parent}{/block}

View File

@ -1,4 +1,4 @@
{var $no = 1} {var $no = 1}
{extends "extends/dynamic/child.{$no}.tpl"} {extends "extends/dynamic/child.{$no = 1}.tpl"}
{use 'extends/dynamic/use.tpl'} {use 'extends/dynamic/use.tpl'}
{block 'header'}Child 2 header{/block} {block 'header'}Child 2 header{/block}

View File

@ -0,0 +1 @@
{extends "extends/dynamic/child.{$a = 3}.tpl"}

View File

@ -0,0 +1,2 @@
{extends 'extends/auto/child.2.tpl'}
{block 'body'}Child 3 content{/block}