mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-04-17 03:37:23 +03:00
Merge branch 'master' of github.com:virtuecenter/handlebars.php
Conflicts: src/Handlebars/Template.php
This commit is contained in:
commit
8ed1ebd051
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "xamin/handlebars.php",
|
||||
"name": "virtuecenter/handlebars.php",
|
||||
"description": "Handlebars processor for php",
|
||||
"homepage": "https://github.com/XaminProject/handlebars.php",
|
||||
"type": "library",
|
||||
@ -10,10 +10,8 @@
|
||||
"email": "fzerorubigd@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
|
||||
},
|
||||
"require": {},
|
||||
"autoload": {
|
||||
"psr-0": { "Handlebars": "src/" }
|
||||
}
|
||||
}
|
||||
}
|
@ -28,7 +28,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
class Handlebars_Autoloader
|
||||
namespace Handlebars;
|
||||
|
||||
class Autoloader
|
||||
{
|
||||
|
||||
private $_baseDir;
|
||||
|
@ -29,7 +29,9 @@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
|
||||
interface Handlebars_Cache
|
||||
namespace Handlebars;
|
||||
|
||||
interface Cache
|
||||
{
|
||||
/**
|
||||
* Get cache for $name if exist.
|
||||
|
@ -26,8 +26,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
|
||||
class Handlebars_Cache_APC implements Handlebars_Cache
|
||||
namespace Handlebars\Cache;
|
||||
|
||||
class APC implements Cache
|
||||
{
|
||||
private $_cache = array();
|
||||
|
||||
|
@ -27,7 +27,9 @@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
|
||||
class Handlebars_Cache_Dummy implements Handlebars_Cache
|
||||
namespace Handlebars\Cache;
|
||||
use Handlebars\Cache;
|
||||
class Dummy implements Cache
|
||||
{
|
||||
private $_cache = array();
|
||||
|
||||
|
@ -28,7 +28,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
class Handlebars_Context
|
||||
namespace Handlebars;
|
||||
|
||||
class Context
|
||||
{
|
||||
/**
|
||||
* @var array stack for context only top stack is available
|
||||
@ -187,7 +189,7 @@ class Handlebars_Context
|
||||
}
|
||||
if (count($this->stack) < $level) {
|
||||
if ($strict) {
|
||||
throw new InvalidArgumentException('can not find variable in context');
|
||||
throw new \InvalidArgumentException('can not find variable in context');
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@ -199,7 +201,7 @@ class Handlebars_Context
|
||||
$current = current($this->stack);
|
||||
if (!$variableName) {
|
||||
if ($strict) {
|
||||
throw new InvalidArgumentException('can not find variable in context');
|
||||
throw new \InvalidArgumentException('can not find variable in context');
|
||||
}
|
||||
return '';
|
||||
} elseif ($variableName == '.' || $variableName == 'this') {
|
||||
@ -244,7 +246,7 @@ class Handlebars_Context
|
||||
} elseif ($inside === '.') {
|
||||
$value = $variable;
|
||||
} elseif ($strict) {
|
||||
throw new InvalidArgumentException('can not find variable in context');
|
||||
throw new \InvalidArgumentException('can not find variable in context');
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
@ -27,37 +27,48 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
namespace Handlebars;
|
||||
use Handlebars\Loader\StringLoader;
|
||||
use Handlebars\Cache\Dummy;
|
||||
|
||||
class Handlebars_Engine
|
||||
class Handlebars
|
||||
{
|
||||
private static $instance = false;
|
||||
const VERSION = '1.0.0';
|
||||
|
||||
public static function factory ($options=array()) {
|
||||
if (self::$instance === false) {
|
||||
self::$instance = new Handlebars($options);
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Handlebars_Tokenizer
|
||||
* @var Tokenizer
|
||||
*/
|
||||
private $_tokenizer;
|
||||
|
||||
/**
|
||||
* @var Handlebars_Parser
|
||||
* @var Parser
|
||||
*/
|
||||
private $_parser;
|
||||
/**
|
||||
* @var Handlebars_Helpers
|
||||
* @var Helpers
|
||||
*/
|
||||
private $_helpers;
|
||||
|
||||
/**
|
||||
* @var Handlebars_Loader
|
||||
* @var Loader
|
||||
*/
|
||||
private $_loader;
|
||||
|
||||
/**
|
||||
* @var Handlebars_Loader
|
||||
* @var Loader
|
||||
*/
|
||||
private $_partialsLoader;
|
||||
|
||||
/**
|
||||
* @var Handlebars_Cache
|
||||
* @var Cache
|
||||
*/
|
||||
private $_cache;
|
||||
/**
|
||||
@ -77,12 +88,12 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Handlebars engine constructor
|
||||
* $options array can contain :
|
||||
* helpers => Handlebars_Helpers object
|
||||
* helpers => Helpers object
|
||||
* escape => a callable function to escape values
|
||||
* escapeArgs => array to pass as extra parameter to escape function
|
||||
* loader => Handlebars_Loader object
|
||||
* partials_loader => Handlebars_Loader object
|
||||
* cache => Handlebars_Cache object
|
||||
* loader => Loader object
|
||||
* partials_loader => Loader object
|
||||
* cache => Cache object
|
||||
*
|
||||
* @param array $options array of options to set
|
||||
*/
|
||||
@ -106,7 +117,7 @@ class Handlebars_Engine
|
||||
|
||||
if (isset($options['escape'])) {
|
||||
if (!is_callable($options['escape'])) {
|
||||
throw new InvalidArgumentException('Handlebars Constructor "escape" option must be callable');
|
||||
throw new \InvalidArgumentException('Handlebars Constructor "escape" option must be callable');
|
||||
}
|
||||
|
||||
$this->_escape = $options['escape'];
|
||||
@ -136,8 +147,8 @@ class Handlebars_Engine
|
||||
* @param mixed $data data to use as context
|
||||
*
|
||||
* @return string Rendered template
|
||||
* @see Handlebars_Engine::loadTemplate
|
||||
* @see Handlebars_Template::render
|
||||
* @see Handlebars::loadTemplate
|
||||
* @see Template::render
|
||||
*/
|
||||
public function render($template, $data)
|
||||
{
|
||||
@ -147,11 +158,11 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Set helpers for current enfine
|
||||
*
|
||||
* @param Handlebars_Helpers $helpers handlebars helper
|
||||
* @param Helpers $helpers handlebars helper
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setHelpers(Handlebars_Helpers $helpers)
|
||||
public function setHelpers(Helpers $helpers)
|
||||
{
|
||||
$this->_helpers = $helpers;
|
||||
}
|
||||
@ -159,12 +170,12 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Get helpers, or create new one if ther is no helper
|
||||
*
|
||||
* @return Handlebars_Helpers
|
||||
* @return Helpers
|
||||
*/
|
||||
public function getHelpers()
|
||||
{
|
||||
if (!isset($this->_helpers)) {
|
||||
$this->_helpers = new Handlebars_Helpers();
|
||||
$this->_helpers = new Helpers();
|
||||
}
|
||||
return $this->_helpers;
|
||||
}
|
||||
@ -221,11 +232,11 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Set current loader
|
||||
*
|
||||
* @param Handlebars_Loader $loader handlebars loader
|
||||
* @param Loader $loader handlebars loader
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setLoader(Handlebars_Loader $loader)
|
||||
public function setLoader(Loader $loader)
|
||||
{
|
||||
$this->_loader = $loader;
|
||||
}
|
||||
@ -233,12 +244,12 @@ class Handlebars_Engine
|
||||
/**
|
||||
* get current loader
|
||||
*
|
||||
* @return Handlebars_Loader
|
||||
* @return Loader
|
||||
*/
|
||||
public function getLoader()
|
||||
{
|
||||
if (!isset($this->_loader)) {
|
||||
$this->_loader = new Handlebars_Loader_StringLoader();
|
||||
$this->_loader = new StringLoader();
|
||||
}
|
||||
return $this->_loader;
|
||||
}
|
||||
@ -246,11 +257,11 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Set current partials loader
|
||||
*
|
||||
* @param Handlebars_Loader $loader handlebars loader
|
||||
* @param Loader $loader handlebars loader
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setPartialsLoader(Handlebars_Loader $loader)
|
||||
public function setPartialsLoader(Loader $loader)
|
||||
{
|
||||
$this->_partialsLoader = $loader;
|
||||
}
|
||||
@ -258,12 +269,12 @@ class Handlebars_Engine
|
||||
/**
|
||||
* get current partials loader
|
||||
*
|
||||
* @return Handlebars_Loader
|
||||
* @return Loader
|
||||
*/
|
||||
public function getPartialsLoader()
|
||||
{
|
||||
if (!isset($this->_partialsLoader)) {
|
||||
$this->_partialsLoader = new Handlebars_Loader_StringLoader();
|
||||
$this->_partialsLoader = new StringLoader();
|
||||
}
|
||||
return $this->_partialsLoader;
|
||||
}
|
||||
@ -271,11 +282,11 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Set cache for current engine
|
||||
*
|
||||
* @param Handlebars_cache $cache handlebars cache
|
||||
* @param Cache $cache handlebars cache
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setCache(Handlebars_Cache $cache)
|
||||
public function setCache(Cache $cache)
|
||||
{
|
||||
$this->_cache = $cache;
|
||||
}
|
||||
@ -283,12 +294,12 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Get cache
|
||||
*
|
||||
* @return Handlebars_Cache
|
||||
* @return Cache
|
||||
*/
|
||||
public function getCache()
|
||||
{
|
||||
if (!isset($this->_cache)) {
|
||||
$this->_cache = new Handlebars_Cache_Dummy();
|
||||
$this->_cache = new Dummy();
|
||||
}
|
||||
return $this->_cache;
|
||||
}
|
||||
@ -312,7 +323,7 @@ class Handlebars_Engine
|
||||
public function setEscape($escape)
|
||||
{
|
||||
if (!is_callable($escape)) {
|
||||
throw new InvalidArgumentException('Escape function must be a callable');
|
||||
throw new \InvalidArgumentException('Escape function must be a callable');
|
||||
}
|
||||
$this->_escape = $escape;
|
||||
}
|
||||
@ -346,11 +357,11 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Set the Handlebars Tokenizer instance.
|
||||
*
|
||||
* @param Handlebars_Tokenizer $tokenizer tokenizer
|
||||
* @param Tokenizer $tokenizer tokenizer
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setTokenizer(Handlebars_Tokenizer $tokenizer)
|
||||
public function setTokenizer(Tokenizer $tokenizer)
|
||||
{
|
||||
$this->_tokenizer = $tokenizer;
|
||||
}
|
||||
@ -360,12 +371,12 @@ class Handlebars_Engine
|
||||
*
|
||||
* If no Tokenizer instance has been explicitly specified, this method will instantiate and return a new one.
|
||||
*
|
||||
* @return Handlebars_Tokenizer
|
||||
* @return Tokenizer
|
||||
*/
|
||||
public function getTokenizer()
|
||||
{
|
||||
if (!isset($this->_tokenizer)) {
|
||||
$this->_tokenizer = new Handlebars_Tokenizer();
|
||||
$this->_tokenizer = new Tokenizer();
|
||||
}
|
||||
|
||||
return $this->_tokenizer;
|
||||
@ -373,11 +384,11 @@ class Handlebars_Engine
|
||||
/**
|
||||
* Set the Handlebars Parser instance.
|
||||
*
|
||||
* @param Handlebars_Parser $parser parser object
|
||||
* @param Parser $parser parser object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setParser(Handlebars_Parser $parser)
|
||||
public function setParser(Parser $parser)
|
||||
{
|
||||
$this->_parser = $parser;
|
||||
}
|
||||
@ -387,12 +398,12 @@ class Handlebars_Engine
|
||||
*
|
||||
* If no Parser instance has been explicitly specified, this method will instantiate and return a new one.
|
||||
*
|
||||
* @return Handlebars_Parser
|
||||
* @return Parser
|
||||
*/
|
||||
public function getParser()
|
||||
{
|
||||
if (!isset($this->_parser)) {
|
||||
$this->_parser = new Handlebars_Parser();
|
||||
$this->_parser = new Parser();
|
||||
}
|
||||
|
||||
return $this->_parser;
|
||||
@ -402,13 +413,13 @@ class Handlebars_Engine
|
||||
*
|
||||
* @param string $name template name
|
||||
*
|
||||
* @return Handlebars_Template
|
||||
* @return Template
|
||||
*/
|
||||
public function loadTemplate($name)
|
||||
{
|
||||
$source = $this->getLoader()->load($name);
|
||||
$tree = $this->_tokenize($source);
|
||||
return new Handlebars_Template($this, $tree, $source);
|
||||
return new Template($this, $tree, $source);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -416,7 +427,7 @@ class Handlebars_Engine
|
||||
*
|
||||
* @param string $name partial name
|
||||
*
|
||||
* @return Handlebars_Template
|
||||
* @return Template
|
||||
*/
|
||||
public function loadPartial($name)
|
||||
{
|
||||
@ -425,7 +436,7 @@ class Handlebars_Engine
|
||||
}
|
||||
$source = $this->getPartialsLoader()->load($name);
|
||||
$tree = $this->_tokenize($source);
|
||||
return new Handlebars_Template($this, $tree, $source);
|
||||
return new Template($this, $tree, $source);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -460,12 +471,12 @@ class Handlebars_Engine
|
||||
*
|
||||
* @param string $source string to load
|
||||
*
|
||||
* @return Handlebars_Template
|
||||
* @return Template
|
||||
*/
|
||||
public function loadString($source)
|
||||
{
|
||||
$tree = $this->_tokenize($source);
|
||||
return new Handlebars_Template($this, $tree, $source);
|
||||
return new Template($this, $tree, $source);
|
||||
}
|
||||
|
||||
/**
|
@ -29,8 +29,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
namespace Handlebars;
|
||||
|
||||
class Handlebars_Helpers
|
||||
class Helpers
|
||||
{
|
||||
/**
|
||||
* @var array array of helpers
|
||||
@ -52,7 +53,7 @@ class Handlebars_Helpers
|
||||
}
|
||||
if ($helpers != null) {
|
||||
if (!is_array($helpers) && !$helpers instanceof Traversable) {
|
||||
throw new InvalidArgumentException('HelperCollection constructor expects an array of helpers');
|
||||
throw new \InvalidArgumentException('HelperCollection constructor expects an array of helpers');
|
||||
}
|
||||
foreach ($helpers as $name => $helper) {
|
||||
$this->add($name, $helpers);
|
||||
@ -188,28 +189,28 @@ class Handlebars_Helpers
|
||||
{
|
||||
$this->add(
|
||||
'if',
|
||||
array('Handlebars_Helpers', '_helper_if')
|
||||
array('Handlebars\Helpers', '_helper_if')
|
||||
);
|
||||
|
||||
$this->add(
|
||||
'each',
|
||||
array('Handlebars_Helpers', '_helper_each')
|
||||
array('Handlebars\Helpers', '_helper_each')
|
||||
);
|
||||
|
||||
$this->add(
|
||||
'unless',
|
||||
array('Handlebars_Helpers', '_helper_unless')
|
||||
array('Handlebars\Helpers', '_helper_unless')
|
||||
);
|
||||
|
||||
$this->add(
|
||||
'with',
|
||||
array('Handlebars_Helpers', '_helper_with')
|
||||
array('Handlebars\Helpers', '_helper_with')
|
||||
);
|
||||
|
||||
//Just for compatibility with ember
|
||||
$this->add(
|
||||
'bindAttr',
|
||||
array('Handlebars_Helpers', '_helper_bindAttr')
|
||||
array('Handlebars\Helpers', '_helper_bindAttr')
|
||||
);
|
||||
}
|
||||
|
||||
@ -225,7 +226,7 @@ class Handlebars_Helpers
|
||||
public function add($name ,$helper)
|
||||
{
|
||||
if (!is_callable($helper)) {
|
||||
throw new InvalidArgumentException("$name Helper is not a callable.");
|
||||
throw new \InvalidArgumentException("$name Helper is not a callable.");
|
||||
}
|
||||
$this->helpers[$name] = $helper;
|
||||
}
|
||||
@ -253,7 +254,7 @@ class Handlebars_Helpers
|
||||
public function __get($name)
|
||||
{
|
||||
if (!$this->has($name)) {
|
||||
throw new InvalidArgumentException('Unknow helper :' . $name);
|
||||
throw new \InvalidArgumentException('Unknow helper :' . $name);
|
||||
}
|
||||
return $this->helpers[$name];
|
||||
}
|
||||
@ -309,7 +310,7 @@ class Handlebars_Helpers
|
||||
public function remove($name)
|
||||
{
|
||||
if (!$this->has($name)) {
|
||||
throw new InvalidArgumentException('Unknown helper: ' . $name);
|
||||
throw new \InvalidArgumentException('Unknown helper: ' . $name);
|
||||
}
|
||||
|
||||
unset($this->helpers[$name]);
|
||||
|
@ -26,7 +26,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
interface Handlebars_Loader
|
||||
namespace Handlebars;
|
||||
|
||||
interface Loader
|
||||
{
|
||||
|
||||
/**
|
||||
@ -34,7 +36,7 @@ interface Handlebars_Loader
|
||||
*
|
||||
* @param string $name template name to load
|
||||
*
|
||||
* @return Handlebars_String
|
||||
* @return String
|
||||
*/
|
||||
public function load($name);
|
||||
}
|
||||
|
@ -27,7 +27,10 @@
|
||||
* @link http://xamin.ir *
|
||||
* @implements Loader
|
||||
*/
|
||||
class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
|
||||
namespace Handlebars\Loader;
|
||||
use Handlesbars\String;
|
||||
|
||||
class FilesystemLoader implements Loader
|
||||
{
|
||||
private $_baseDir;
|
||||
private $_extension = '.handlebars';
|
||||
@ -63,7 +66,7 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
|
||||
|
||||
foreach ($this->_baseDir as $dir) {
|
||||
if (!is_dir($dir)) {
|
||||
throw new RuntimeException('FilesystemLoader baseDir must be a directory: ' . $dir);
|
||||
throw new \RuntimeException('FilesystemLoader baseDir must be a directory: ' . $dir);
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,14 +87,14 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
|
||||
*
|
||||
* @param string $name template name
|
||||
*
|
||||
* @return Handlebars_String Handlebars Template source
|
||||
* @return String Handlebars Template source
|
||||
*/
|
||||
public function load($name)
|
||||
{
|
||||
if (!isset($this->_templates[$name])) {
|
||||
$this->_templates[$name] = $this->loadFile($name);
|
||||
}
|
||||
return new Handlebars_String($this->_templates[$name]);
|
||||
return new String($this->_templates[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,7 +110,7 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
|
||||
$fileName = $this->getFileName($name);
|
||||
|
||||
if ($fileName === false) {
|
||||
throw new InvalidArgumentException('Template ' . $name . ' not found.');
|
||||
throw new \InvalidArgumentException('Template ' . $name . ' not found.');
|
||||
}
|
||||
|
||||
return file_get_contents($fileName);
|
||||
|
@ -26,7 +26,11 @@
|
||||
* @link http://xamin.ir *
|
||||
* @implements Loader
|
||||
*/
|
||||
class Handlebars_Loader_StringLoader implements Handlebars_Loader
|
||||
namespace Handlebars\Loader;
|
||||
use Handlebars\Loader;
|
||||
use Handlebars\String;
|
||||
|
||||
class StringLoader implements Loader
|
||||
{
|
||||
|
||||
/**
|
||||
@ -34,10 +38,10 @@ class Handlebars_Loader_StringLoader implements Handlebars_Loader
|
||||
*
|
||||
* @param string $name Handlebars Template source
|
||||
*
|
||||
* @return Handlebars_string Handlebars Template source
|
||||
* @return String Handlebars Template source
|
||||
*/
|
||||
public function load($name)
|
||||
{
|
||||
return new Handlebars_String($name);
|
||||
return new String($name);
|
||||
}
|
||||
}
|
||||
|
@ -27,8 +27,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
namespace Handlebars;
|
||||
|
||||
class Handlebars_Parser
|
||||
class Parser
|
||||
{
|
||||
/**
|
||||
* Process array of tokens and convert them into parse tree
|
||||
@ -39,7 +40,7 @@ class Handlebars_Parser
|
||||
*/
|
||||
public function parse(array $tokens = array())
|
||||
{
|
||||
return $this->_buildTree(new ArrayIterator($tokens));
|
||||
return $this->_buildTree(new \ArrayIterator($tokens));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,7 +52,7 @@ class Handlebars_Parser
|
||||
*
|
||||
* @throws LogicException when nesting errors or mismatched section tags are encountered.
|
||||
*/
|
||||
private function _buildTree(ArrayIterator $tokens)
|
||||
private function _buildTree(\ArrayIterator $tokens)
|
||||
{
|
||||
$stack = array();
|
||||
|
||||
@ -62,22 +63,22 @@ class Handlebars_Parser
|
||||
if ($token === null) {
|
||||
continue;
|
||||
} else {
|
||||
switch ($token[Handlebars_Tokenizer::TYPE]) {
|
||||
case Handlebars_Tokenizer::T_END_SECTION:
|
||||
switch ($token[Tokenizer::TYPE]) {
|
||||
case Tokenizer::T_END_SECTION:
|
||||
$newNodes = array ();
|
||||
$continue = true;
|
||||
do {
|
||||
$result = array_pop($stack);
|
||||
if ($result === null) {
|
||||
throw new LogicException('Unexpected closing tag: /'. $token[Handlebars_Tokenizer::NAME]);
|
||||
throw new \LogicException('Unexpected closing tag: /'. $token[Tokenizer::NAME]);
|
||||
}
|
||||
|
||||
if (!array_key_exists(Handlebars_Tokenizer::NODES, $result)
|
||||
&& isset($result[Handlebars_Tokenizer::NAME])
|
||||
&& $result[Handlebars_Tokenizer::NAME] == $token[Handlebars_Tokenizer::NAME]
|
||||
if (!array_key_exists(Tokenizer::NODES, $result)
|
||||
&& isset($result[Tokenizer::NAME])
|
||||
&& $result[Tokenizer::NAME] == $token[Tokenizer::NAME]
|
||||
) {
|
||||
$result[Handlebars_Tokenizer::NODES] = $newNodes;
|
||||
$result[Handlebars_Tokenizer::END] = $token[Handlebars_Tokenizer::INDEX];
|
||||
$result[Tokenizer::NODES] = $newNodes;
|
||||
$result[Tokenizer::END] = $token[Tokenizer::INDEX];
|
||||
array_push($stack, $result);
|
||||
break 2;
|
||||
} else {
|
||||
|
@ -25,8 +25,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
namespace Handlebars;
|
||||
|
||||
class Handlebars_String
|
||||
class String
|
||||
{
|
||||
private $_string;
|
||||
|
||||
|
@ -27,11 +27,12 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
namespace Handlebars;
|
||||
|
||||
class Handlebars_Template
|
||||
class Template
|
||||
{
|
||||
/**
|
||||
* @var Handlebars_Engine
|
||||
* @var Handlebars
|
||||
*/
|
||||
protected $handlebars;
|
||||
|
||||
@ -48,11 +49,11 @@ class Handlebars_Template
|
||||
/**
|
||||
* Handlebars template constructor
|
||||
*
|
||||
* @param Handlebars_Engine $engine handlebar engine
|
||||
* @param Handlebars $engine handlebar engine
|
||||
* @param array $tree Parsed tree
|
||||
* @param string $source Handlebars source
|
||||
*/
|
||||
public function __construct(Handlebars_Engine $engine, $tree, $source)
|
||||
public function __construct(Handlebars $engine, $tree, $source)
|
||||
{
|
||||
$this->handlebars = $engine;
|
||||
$this->tree = $tree;
|
||||
@ -83,7 +84,7 @@ class Handlebars_Template
|
||||
/**
|
||||
* Get current engine associated with this object
|
||||
*
|
||||
* @return Handlebars_Engine
|
||||
* @return Handlebars
|
||||
*/
|
||||
public function getEngine()
|
||||
{
|
||||
@ -125,8 +126,8 @@ class Handlebars_Template
|
||||
*/
|
||||
public function render($context)
|
||||
{
|
||||
if (!$context instanceof Handlebars_Context) {
|
||||
$context = new Handlebars_Context($context);
|
||||
if (!$context instanceof Context) {
|
||||
$context = new Context($context);
|
||||
}
|
||||
$topTree = end($this->_stack); //This method (render) never pop a value from stack
|
||||
list($index ,$tree, $stop) = $topTree;
|
||||
@ -137,43 +138,43 @@ class Handlebars_Template
|
||||
$index++;
|
||||
//if the section is exactly like waitFor
|
||||
if (is_string($stop)
|
||||
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
|
||||
&& $current[Handlebars_Tokenizer::NAME] === $stop
|
||||
&& $current[Tokenizer::TYPE] == Tokenizer::T_ESCAPED
|
||||
&& $current[Tokenizer::NAME] === $stop
|
||||
) {
|
||||
break;
|
||||
}
|
||||
switch ($current[Handlebars_Tokenizer::TYPE]) {
|
||||
case Handlebars_Tokenizer::T_SECTION :
|
||||
$newStack = isset($current[Handlebars_Tokenizer::NODES]) ? $current[Handlebars_Tokenizer::NODES] : array();
|
||||
switch ($current[Tokenizer::TYPE]) {
|
||||
case Tokenizer::T_SECTION :
|
||||
$newStack = isset($current[Tokenizer::NODES]) ? $current[Tokenizer::NODES] : array();
|
||||
array_push($this->_stack, array(0, $newStack, false));
|
||||
$buffer .= $this->_section($context, $current);
|
||||
array_pop($this->_stack);
|
||||
break;
|
||||
case Handlebars_Tokenizer::T_INVERTED :
|
||||
$newStack = isset($current[Handlebars_Tokenizer::NODES]) ? $current[Handlebars_Tokenizer::NODES] : array();
|
||||
case Tokenizer::T_INVERTED :
|
||||
$newStack = isset($current[Tokenizer::NODES]) ? $current[Tokenizer::NODES] : array();
|
||||
array_push($this->_stack, array(0, $newStack, false));
|
||||
$buffer .= $this->_inverted($context, $current);
|
||||
array_pop($this->_stack);
|
||||
break;
|
||||
case Handlebars_Tokenizer::T_COMMENT :
|
||||
case Tokenizer::T_COMMENT :
|
||||
$buffer .= '';
|
||||
break;
|
||||
case Handlebars_Tokenizer::T_PARTIAL:
|
||||
case Handlebars_Tokenizer::T_PARTIAL_2:
|
||||
case Tokenizer::T_PARTIAL:
|
||||
case Tokenizer::T_PARTIAL_2:
|
||||
$buffer .= $this->_partial($context, $current);
|
||||
break;
|
||||
case Handlebars_Tokenizer::T_UNESCAPED:
|
||||
case Handlebars_Tokenizer::T_UNESCAPED_2:
|
||||
case Tokenizer::T_UNESCAPED:
|
||||
case Tokenizer::T_UNESCAPED_2:
|
||||
$buffer .= $this->_variables($context, $current, false);
|
||||
break;
|
||||
case Handlebars_Tokenizer::T_ESCAPED:
|
||||
case Tokenizer::T_ESCAPED:
|
||||
$buffer .= $this->_variables($context, $current, true);
|
||||
break;
|
||||
case Handlebars_Tokenizer::T_TEXT:
|
||||
$buffer .= $current[Handlebars_Tokenizer::VALUE];
|
||||
case Tokenizer::T_TEXT:
|
||||
$buffer .= $current[Tokenizer::VALUE];
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException('Invalid node type : ' . json_encode($current));
|
||||
throw new \RuntimeException('Invalid node type : ' . json_encode($current));
|
||||
}
|
||||
}
|
||||
if ($stop) {
|
||||
@ -195,8 +196,8 @@ class Handlebars_Template
|
||||
*/
|
||||
public function discard($context)
|
||||
{
|
||||
if (!$context instanceof Handlebars_Context) {
|
||||
$context = new Handlebars_Context($context);
|
||||
if (!$context instanceof Context) {
|
||||
$context = new Context($context);
|
||||
}
|
||||
$topTree = end($this->_stack); //This method never pop a value from stack
|
||||
list($index ,$tree, $stop) = $topTree;
|
||||
@ -205,8 +206,8 @@ class Handlebars_Template
|
||||
$index++;
|
||||
//if the section is exactly like waitFor
|
||||
if (is_string($stop)
|
||||
&& $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED
|
||||
&& $current[Handlebars_Tokenizer::NAME] === $stop
|
||||
&& $current[Tokenizer::TYPE] == Tokenizer::T_ESCAPED
|
||||
&& $current[Tokenizer::NAME] === $stop
|
||||
) {
|
||||
break;
|
||||
}
|
||||
@ -224,21 +225,21 @@ class Handlebars_Template
|
||||
/**
|
||||
* Process section nodes
|
||||
*
|
||||
* @param Handlebars_Context $context current context
|
||||
* @param Context $context current context
|
||||
* @param array $current section node data
|
||||
*
|
||||
* @return string the result
|
||||
*/
|
||||
private function _section(Handlebars_Context $context, $current)
|
||||
private function _section(Context $context, $current)
|
||||
{
|
||||
$helpers = $this->handlebars->getHelpers();
|
||||
$sectionName = $current[Handlebars_Tokenizer::NAME];
|
||||
$sectionName = $current[Tokenizer::NAME];
|
||||
if ($helpers->has($sectionName)) {
|
||||
if (isset($current[Handlebars_Tokenizer::END])) {
|
||||
if (isset($current[Tokenizer::END])) {
|
||||
$source = substr(
|
||||
$this->getSource(),
|
||||
$current[Handlebars_Tokenizer::INDEX],
|
||||
$current[Handlebars_Tokenizer::END] - $current[Handlebars_Tokenizer::INDEX]
|
||||
$current[Tokenizer::INDEX],
|
||||
$current[Tokenizer::END] - $current[Tokenizer::INDEX]
|
||||
);
|
||||
} else {
|
||||
$source = '';
|
||||
@ -246,22 +247,22 @@ class Handlebars_Template
|
||||
$params = array(
|
||||
$this, //First argument is this template
|
||||
$context, //Secound is current context
|
||||
$current[Handlebars_Tokenizer::ARGS], //Arguments
|
||||
$current[Tokenizer::ARGS], //Arguments
|
||||
$source
|
||||
);
|
||||
|
||||
$return = call_user_func_array($helpers->$sectionName, $params);
|
||||
if ($return instanceof Handlebars_String) {
|
||||
if ($return instanceof String) {
|
||||
return $this->handlebars->loadString($return)->render($context);
|
||||
} else {
|
||||
return $return;
|
||||
}
|
||||
} elseif (trim($current[Handlebars_Tokenizer::ARGS]) == '') {
|
||||
} elseif (trim($current[Tokenizer::ARGS]) == '') {
|
||||
//Fallback for mustache style each/with/for just if there is no argument at all.
|
||||
try {
|
||||
$sectionVar = $context->get($sectionName, true);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new RuntimeException($sectionName . ' is not registered as a helper');
|
||||
throw new \RuntimeException($sectionName . ' is not registered as a helper');
|
||||
}
|
||||
$buffer = '';
|
||||
if (is_array($sectionVar) || $sectionVar instanceof Traversable) {
|
||||
@ -282,21 +283,21 @@ class Handlebars_Template
|
||||
}
|
||||
return $buffer;
|
||||
} else {
|
||||
throw new RuntimeException($sectionName . ' is not registered as a helper');
|
||||
throw new \RuntimeException($sectionName . ' is not registered as a helper');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process inverted section
|
||||
*
|
||||
* @param Handlebars_Context $context current context
|
||||
* @param Context $context current context
|
||||
* @param array $current section node data
|
||||
*
|
||||
* @return string the result
|
||||
*/
|
||||
private function _inverted(Handlebars_Context $context, $current)
|
||||
private function _inverted(Context $context, $current)
|
||||
{
|
||||
$sectionName = $current[Handlebars_Tokenizer::NAME];
|
||||
$sectionName = $current[Tokenizer::NAME];
|
||||
$data = $context->get($sectionName);
|
||||
if (!$data) {
|
||||
return $this->render($context);
|
||||
@ -309,17 +310,17 @@ class Handlebars_Template
|
||||
/**
|
||||
* Process partial section
|
||||
*
|
||||
* @param Handlebars_Context $context current context
|
||||
* @param Context $context current context
|
||||
* @param array $current section node data
|
||||
*
|
||||
* @return string the result
|
||||
*/
|
||||
private function _partial($context, $current)
|
||||
{
|
||||
$partial = $this->handlebars->loadPartial($current[Handlebars_Tokenizer::NAME]);
|
||||
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
|
||||
|
||||
if ( $current[Handlebars_Tokenizer::ARGS] ) {
|
||||
$context = $context->get($current[Handlebars_Tokenizer::ARGS]);
|
||||
if ( $current[Tokenizer::ARGS] ) {
|
||||
$context = $context->get($current[Tokenizer::ARGS]);
|
||||
}
|
||||
|
||||
return $partial->render($context);
|
||||
@ -328,7 +329,7 @@ class Handlebars_Template
|
||||
/**
|
||||
* Process partial section
|
||||
*
|
||||
* @param Handlebars_Context $context current context
|
||||
* @param Context $context current context
|
||||
* @param array $current section node data
|
||||
* @param boolean $escaped escape result or not
|
||||
*
|
||||
|
@ -32,7 +32,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
class Handlebars_Tokenizer
|
||||
namespace Handlebars;
|
||||
|
||||
class Tokenizer
|
||||
{
|
||||
|
||||
// Finite state machine states
|
||||
@ -106,7 +108,7 @@ class Handlebars_Tokenizer
|
||||
*/
|
||||
public function scan($text, $delimiters = null)
|
||||
{
|
||||
if ($text instanceof Handlebars_String) {
|
||||
if ($text instanceof String) {
|
||||
$text = $text->getString();
|
||||
}
|
||||
$this->reset();
|
||||
|
Loading…
Reference in New Issue
Block a user