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

@ -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");
}