Fix #241 + tests

This commit is contained in:
bzick 2016-10-09 23:40:37 +03:00
parent 05cda5426d
commit b92e0bbad3
7 changed files with 95 additions and 117 deletions

View File

@ -5,73 +5,12 @@ require_once __DIR__.'/../tests/tools.php';
\Fenom::registerAutoload();
$vars = [
[
"id" => 1,
"name" => "Блаблабла",
"hidden_url" => "/"
],
[
"id" => 2,
"name" => "Каталог",
"hidden_url" => "/catalog"
],
[
"id" => 3,
"name" => "Сыромолочная группа",
"hidden_url" => "/catalog/cat_1.html"
],
[
"id" => 4,
"name" => "Сыры",
"hidden_url" => "/catalog/cat_2.html"
],
];
$fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled');
$fenom->setOptions(Fenom::AUTO_RELOAD | Fenom::FORCE_VERIFY);
$fenom->setOptions(Fenom::AUTO_RELOAD | Fenom::FORCE_VERIFY | Fenom::FORCE_INCLUDE);
//var_dump($fenom->compile("nested.tpl", [])->getTemplateCode());
//exit;
var_dump($fenom->fetch('bug249/bread.tpl', ["arr" => $vars]));
var_dump($fenom->compile('bug241/recursive.tpl', false)->getBody());
//var_dump($fenom->compile('bug249/bread.tpl', false)->getBody());
//var_dump($fenom->compile("bug158/main.tpl", [])->getTemplateCode());
//var_dump($fenom->display("bug158/main.tpl", []));
// $fenom->getTemplate("problem.tpl");
/*
*
* Array
(
[0] => Array
(
[id] => 1
[name] => Блаблабла
[hidden_url] => /
)
[1] => Array
(
[id] => 2
[name] => Каталог
[hidden_url] => /catalog/
)
[2] => Array
(
[orig_id] => 1
[hidden_url] => /catalog/cat_1.html
[name] => Сыромолочная группа
)
[3] => Array
(
[orig_id] => 2
[hidden_url] => /catalog/cat_2.html
[name] => Сыры
)
[4] => Array
(
[orig_id] => 6
[hidden_url] => /catalog/cat_6.html
[name] => Сыр плавленый
)
)
*/

View File

@ -0,0 +1,3 @@
{if $n < 10}
{include 'bug241/recursive.tpl' n=$n - 1}
{/if}

View File

@ -944,9 +944,9 @@ class Fenom
*
* @return Fenom\Template
*/
public function getRawTemplate()
public function getRawTemplate(Template $parent = null)
{
return new Template($this, $this->_options);
return new Template($this, $this->_options, $parent);
}
/**

View File

@ -38,13 +38,24 @@ class Compiler
$p = $tpl->parseParams($tokens);
if ($name) {
if ($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE) {
$inc = $tpl->getStorage()->compile($name, false);
$tpl->addDepend($inc);
$var = $tpl->tmpVar();
if ($p) {
return $var . ' = $var; $var = ' . self::toArray($p) . ' + $var; ?>' . $inc->getBody() . '<?php $var = ' . $var . '; unset(' . $var . ');';
} else {
return $var . ' = $var; ?>' . $inc->getBody() . '<?php $var = ' . $var . '; unset(' . $var . ');';
$_t = $tpl;
$recursion = false;
while($_t->parent) {
if($_t->parent->getName() == $name) { // recursion detected
$recursion = true;
}
$_t = $_t->parent;
}
if(!$recursion) {
$inc = $tpl->getStorage()->getRawTemplate($tpl);
$inc->load($name, true);
$tpl->addDepend($inc);
$var = $tpl->tmpVar();
if ($p) {
return $var . ' = $var; $var = ' . self::toArray($p) . ' + $var; ?>' . $inc->getBody() . '<?php $var = ' . $var . '; unset(' . $var . ');';
} else {
return $var . ' = $var; ?>' . $inc->getBody() . '<?php $var = ' . $var . '; unset(' . $var . ');';
}
}
} elseif (!$tpl->getStorage()->templateExists($name)) {
throw new \LogicException("Template $name not found");

View File

@ -81,6 +81,12 @@ class Template extends Render
public $extend_body = false;
/**
* Parent template
* @var Template
*/
public $parent;
/**
* Template PHP code
* @var string
@ -121,10 +127,11 @@ class Template extends Render
/**
* @param Fenom $fenom Template storage
* @param int $options
* @return \Fenom\Template
* @param Template $parent
*/
public function __construct(Fenom $fenom, $options)
public function __construct(Fenom $fenom, $options, Template $parent = null)
{
$this->parent = $parent;
$this->_fenom = $fenom;
$this->_options = $options;
$this->_filters = $this->_fenom->getFilters();

View File

@ -116,6 +116,12 @@ class TestCase extends \PHPUnit_Framework_TestCase
return filemtime(FENOM_RESOURCES . '/template/' . $name);
}
public function tpls(array $list) {
foreach($list as $name => $tpl) {
$this->tpl($name, $tpl);
}
}
/**
* Compile and execute template
*

View File

@ -268,48 +268,6 @@ class TemplateTest extends TestCase
);
}
public static function providerInclude()
{
$a = array(
"name" => "welcome",
"tpl" => "welcome.tpl",
"fragment" => "come",
"pr_fragment" => "Come",
"pr_name" => "Welcome",
"username" => "Master",
"email" => "dev@null.net"
);
$result = 'Include <b>Welcome, Master (dev@null.net)</b> template';
$result2 = 'Include <b>Welcome, Flame (dev@null.net)</b> template';
$result3 = 'Include <b>Welcome, Master (flame@dev.null)</b> template';
$result4 = 'Include <b>Welcome, Flame (flame@dev.null)</b> template';
return array(
array('Include {include "welcome.tpl"} template', $a, $result),
array('Include {include "welcome.tpl"} template', $a, $result, Fenom::FORCE_INCLUDE),
array('Include {include $tpl} template', $a, $result),
array('Include {include "$tpl"} template', $a, $result),
array('Include {include "{$tpl}"} template', $a, $result),
array('Include {include "$name.tpl"} template', $a, $result),
array('Include {include "{$name}.tpl"} template', $a, $result),
array('Include {include "{$pr_name|lower}.tpl"} template', $a, $result),
array('Include {include "wel{$fragment}.tpl"} template', $a, $result),
array('Include {include "wel{$pr_fragment|lower}.tpl"} template', $a, $result),
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2),
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2, Fenom::FORCE_INCLUDE),
array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3),
array(
'Include {include "welcome.tpl" email="flame@dev.null"} template',
$a,
$result3,
Fenom::FORCE_INCLUDE
),
array(
'Include {include "welcome.tpl" username="Flame" email="flame@dev.null"} template',
$a,
$result4
),
);
}
public static function providerIncludeInvalid()
{
@ -1195,12 +1153,66 @@ class TemplateTest extends TestCase
$this->execError($code, $exception, $message, $options);
}
public static function providerInclude()
{
$a = array(
"name" => "welcome",
"tpl" => "welcome.tpl",
"fragment" => "come",
"pr_fragment" => "Come",
"pr_name" => "Welcome",
"username" => "Master",
"email" => "dev@null.net"
);
$result = 'Include <b>Welcome, Master (dev@null.net)</b> template';
$result2 = 'Include <b>Welcome, Flame (dev@null.net)</b> template';
$result3 = 'Include <b>Welcome, Master (flame@dev.null)</b> template';
$result4 = 'Include <b>Welcome, Flame (flame@dev.null)</b> template';
$recursive_result = 'Include <b>Hello, Master (dev@null.net)</b> template';
$recursive_result2 = 'Include <b>Hello, Flame (dev@null.net)</b> template';
return array(
array('Include {include "welcome.tpl"} template', $a, $result),
array('Include {include "welcome.tpl"} template', $a, $result, Fenom::FORCE_INCLUDE),
array('Include {include "recursive.tpl"} template', $a, $recursive_result, Fenom::FORCE_INCLUDE),
array('Include {include $tpl} template', $a, $result),
array('Include {include "$tpl"} template', $a, $result),
array('Include {include "{$tpl}"} template', $a, $result),
array('Include {include "$name.tpl"} template', $a, $result),
array('Include {include "{$name}.tpl"} template', $a, $result),
array('Include {include "{$pr_name|lower}.tpl"} template', $a, $result),
array('Include {include "wel{$fragment}.tpl"} template', $a, $result),
array('Include {include "wel{$pr_fragment|lower}.tpl"} template', $a, $result),
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2),
array('Include {include "welcome.tpl" username="Flame"} template', $a, $result2, Fenom::FORCE_INCLUDE),
array('Include {include "recursive.tpl" username="Flame"} template', $a, $recursive_result2, Fenom::FORCE_INCLUDE),
array('Include {include "welcome.tpl" email="flame@dev.null"} template', $a, $result3),
array(
'Include {include "welcome.tpl" email="flame@dev.null"} template',
$a,
$result3,
Fenom::FORCE_INCLUDE
),
array(
'Include {include "welcome.tpl" username="Flame" email="flame@dev.null"} template',
$a,
$result4,
),
);
}
/**
* @group include
* @group dev
* @dataProvider providerInclude
*/
public function testInclude($code, $vars, $result, $options = 0)
{
$this->tpls(array(
'welcome.tpl' => '<b>Welcome, {$username} ({$email})</b>',
'recursive.tpl' => '<b>Hello, {$username} ({$email}){if false}{include "recursive.tpl"}{/if}</b>'
));
$this->exec($code, $vars, $result, $options);
}