Add 'how it work'

This commit is contained in:
bzick 2013-08-22 00:03:20 +04:00
parent 25975a6782
commit cb803f5d9c
6 changed files with 60 additions and 20 deletions

View File

@ -1,6 +1,14 @@
CHANGELOG
=========
## 1.3.0
- Feature #41: Add system variable `$`.
- Fix bug when recursive macros doesn't work in Template
- Recognize variable parser
- Tests++
- Docs--
### 1.2.2 (2013-08-07)
- Fix bug in setOptions method

View File

@ -4,3 +4,5 @@ Develop
If you want to discuss the enhancement of the Fenom, create an issue on Github or submit a pull request.
For questions: a.cobest@gmail.com (English, Russian languages)
[./git.md](Git conversation)

View File

@ -17,7 +17,7 @@ use Fenom\Template;
*/
class Fenom
{
const VERSION = '1.2';
const VERSION = '1.3';
/* Actions */
const INLINE_COMPILER = 1;

View File

@ -9,6 +9,7 @@
*/
namespace Fenom;
use Fenom;
use Symfony\Component\Yaml\Exception\RuntimeException;
/**
* Primitive template
@ -69,6 +70,11 @@ class Render extends \ArrayObject
*/
protected $_provider;
/**
* @var \Closure[]
*/
protected $_macros;
/**
* @param Fenom $fenom
* @param callable $code template body
@ -192,6 +198,10 @@ class Render extends \ArrayObject
*/
public function getMacro($name)
{
if(empty($this->_macros[$name])) {
var_dump($this->_macros[$name]); exit;
throw new RuntimeException('macro not found');
}
return $this->_macros[$name];
}

View File

@ -397,18 +397,6 @@ class Template extends Render
*/
public function getTemplateCode()
{
if ($this->macros) {
$macros = array();
foreach ($this->macros as $m) {
if ($m["recursive"]) {
$macros[] = "\t\t'" . $m["name"] . "' => function (\$tpl) {\n?>" . $m["body"] . "<?php\n}";
}
}
$macros = "\n" . implode(",\n", $macros);
} else {
$macros = "";
}
$before = $this->_before ? $this->_before . "\n" : "";
return "<?php \n" .
"/** Fenom template '" . $this->_name . "' compiled at " . date('Y-m-d H:i:s') . " */\n" .
@ -420,10 +408,29 @@ class Template extends Render
"\t'base_name' => " . var_export($this->_base_name, true) . ",\n" .
"\t'time' => {$this->_time},\n" .
"\t'depends' => " . var_export($this->_base_name, true) . ",\n" .
"\t'macros' => array({$macros}),
"\t'macros' => " . $this->_getMacrosArray() . ",\n
));\n";
}
/**
* Make array with macros code
* @return string
*/
private function _getMacrosArray()
{
if ($this->macros) {
$macros = array();
foreach ($this->macros as $m) {
if ($m["recursive"]) {
$macros[] = "\t\t'" . $m["name"] . "' => function (\$tpl) {\n?>" . $m["body"] . "<?php\n}";
}
}
return "array(\n" . implode(",\n", $macros).")";
} else {
return 'array()';
}
}
/**
* Return closure code
* @return string
@ -444,7 +451,7 @@ class Template extends Render
{
if (!$this->_code) {
// evaluate template's code
eval("\$this->_code = " . $this->_getClosureSource() . ";");
eval("\$this->_code = " . $this->_getClosureSource() . ";\n\$this->_macros = ".$this->_getMacrosArray() .';');
if (!$this->_code) {
throw new CompileException("Fatal error while creating the template");
}

View File

@ -51,16 +51,21 @@ class MacrosTest extends TestCase
{/if}
{/macro}
{macro.factorial num=10}');
XXXX: {macro.factorial num=10}');
$this->tpl("macro_recursive_import.tpl", '
{import "macro_recursive.tpl" as math}
YYYY: {math.factorial num=10}');
}
public function _testSandbox()
public function testSandbox()
{
try {
$this->fenom->compile("macro_recursive.tpl");
// $this->fenom->compile("macro_recursive.tpl")->display([]);
// $this->fenom->flush();
// var_dump($this->fenom->fetch("macro_recursive.tpl", []));
var_dump( $this->fenom->compile("macro_recursive.tpl")->getTemplateCode());
var_dump( $this->fenom->compile("macro_recursive_import.tpl")->getTemplateCode([]));
} catch (\Exception $e) {
var_dump($e->getMessage() . ": " . $e->getTraceAsString());
}
@ -107,4 +112,12 @@ class MacrosTest extends TestCase
$tpl = $this->fenom->getTemplate('macro_recursive.tpl');
$this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true));
}
public function testImportRecursive()
{
$this->fenom->compile('macro_recursive_import.tpl');
$this->fenom->flush();
$tpl = $this->fenom->getTemplate('macro_recursive.tpl');
$this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true));
}
}