Skip to content

Commit

Permalink
Fix #241 + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bzick committed Oct 9, 2016
1 parent 05cda54 commit b92e0bb
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 117 deletions.
65 changes: 2 additions & 63 deletions sandbox/fenom.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,12 @@

\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] => Сыр плавленый
)
)
*/
3 changes: 3 additions & 0 deletions sandbox/templates/bug241/recursive.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{if $n < 10}
{include 'bug241/recursive.tpl' n=$n - 1}
{/if}
4 changes: 2 additions & 2 deletions src/Fenom.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,9 +944,9 @@ public function getProvider($scm = false)
*
* @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);
}

/**
Expand Down
25 changes: 18 additions & 7 deletions src/Fenom/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,24 @@ public static function tagInclude(Tokenizer $tokens, Tag $tag)
$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");
Expand Down
11 changes: 9 additions & 2 deletions src/Fenom/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class Template extends Render

public $extend_body = false;

/**
* Parent template
* @var Template
*/
public $parent;

/**
* Template PHP code
* @var string
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public function tpl($name, $code)
return filemtime(FENOM_RESOURCES . '/template/' . $name);
}

public function tpls(array $list) {
foreach($list as $name => $tpl) {
$this->tpl($name, $tpl);
}
}

/**
* Compile and execute template
*
Expand Down
98 changes: 55 additions & 43 deletions tests/cases/Fenom/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,48 +268,6 @@ public static function providerExpressionsInvalid()
);
}

public static function providerInclude()
{
$a = array(
"name" => "welcome",
"tpl" => "welcome.tpl",
"fragment" => "come",
"pr_fragment" => "Come",
"pr_name" => "Welcome",
"username" => "Master",
"email" => "[email protected]"
);
$result = 'Include <b>Welcome, Master ([email protected])</b> template';
$result2 = 'Include <b>Welcome, Flame ([email protected])</b> template';
$result3 = 'Include <b>Welcome, Master ([email protected])</b> template';
$result4 = 'Include <b>Welcome, Flame ([email protected])</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="[email protected]"} template', $a, $result3),
array(
'Include {include "welcome.tpl" email="[email protected]"} template',
$a,
$result3,
Fenom::FORCE_INCLUDE
),
array(
'Include {include "welcome.tpl" username="Flame" email="[email protected]"} template',
$a,
$result4
),
);
}

public static function providerIncludeInvalid()
{
Expand Down Expand Up @@ -1195,12 +1153,66 @@ public function testExpressionsInvalid($code, $exception, $message, $options = 0
$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" => "[email protected]"
);

$result = 'Include <b>Welcome, Master ([email protected])</b> template';
$result2 = 'Include <b>Welcome, Flame ([email protected])</b> template';
$result3 = 'Include <b>Welcome, Master ([email protected])</b> template';
$result4 = 'Include <b>Welcome, Flame ([email protected])</b> template';

$recursive_result = 'Include <b>Hello, Master ([email protected])</b> template';
$recursive_result2 = 'Include <b>Hello, Flame ([email protected])</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="[email protected]"} template', $a, $result3),
array(
'Include {include "welcome.tpl" email="[email protected]"} template',
$a,
$result3,
Fenom::FORCE_INCLUDE
),
array(
'Include {include "welcome.tpl" username="Flame" email="[email protected]"} 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);
}

Expand Down

0 comments on commit b92e0bb

Please sign in to comment.