mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
phpcs approval
“oh the spacing!”
This commit is contained in:
parent
33c76738b4
commit
0070ad79c0
@ -41,20 +41,22 @@ namespace Handlebars;
|
|||||||
|
|
||||||
class ChildContext extends Context
|
class ChildContext extends Context
|
||||||
{
|
{
|
||||||
protected $parentContext = null;
|
protected $parentContext = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets a parent context in which
|
* Sets a parent context in which
|
||||||
* we will case for the ../ in get()
|
* we will case for the ../ in get()
|
||||||
*
|
*
|
||||||
* @param Context
|
* @param Context $parent parent context
|
||||||
* @return void
|
*
|
||||||
*/
|
* @return void
|
||||||
public function setParent(Context $parent) {
|
*/
|
||||||
$this->parentContext = $parent;
|
public function setParent(Context $parent)
|
||||||
}
|
{
|
||||||
|
$this->parentContext = $parent;
|
||||||
/**
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* Get a available from current context
|
* Get a available from current context
|
||||||
* Supported types :
|
* Supported types :
|
||||||
* variable , ../variable , variable.variable , variable.[variable] , .
|
* variable , ../variable , variable.variable , variable.[variable] , .
|
||||||
@ -69,17 +71,19 @@ class ChildContext extends Context
|
|||||||
*/
|
*/
|
||||||
public function get($variableName, $strict = false)
|
public function get($variableName, $strict = false)
|
||||||
{
|
{
|
||||||
//if the variable name starts with a ../
|
//if the variable name starts with a ../
|
||||||
//and we have a parent
|
//and we have a parent
|
||||||
if(strpos($variableName, '../') === 0 && $this->parentContext instanceof Context) {
|
if (strpos($variableName, '../') === 0
|
||||||
//just remove the first ../
|
&& $this->parentContext instanceof Context
|
||||||
$variableName = substr($variableName, 3);
|
) {
|
||||||
|
//just remove the first ../
|
||||||
//and let the parent context handle the rest
|
$variableName = substr($variableName, 3);
|
||||||
return $this->parentContext->get($variableName, $strict);
|
|
||||||
}
|
//and let the parent context handle the rest
|
||||||
|
return $this->parentContext->get($variableName, $strict);
|
||||||
//otherwise, it's business as usual
|
}
|
||||||
return parent::get($variableName, $strict);
|
|
||||||
|
//otherwise, it's business as usual
|
||||||
|
return parent::get($variableName, $strict);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ class Handlebars
|
|||||||
const VERSION = '1.0.0';
|
const VERSION = '1.0.0';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* factory method
|
* Factory method
|
||||||
*
|
*
|
||||||
* @param array $options see __construct's options parameter
|
* @param array $options see __construct's options parameter
|
||||||
*
|
*
|
||||||
@ -55,41 +55,57 @@ class Handlebars
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Current tokenizer instance
|
||||||
|
*
|
||||||
* @var Tokenizer
|
* @var Tokenizer
|
||||||
*/
|
*/
|
||||||
private $_tokenizer;
|
private $_tokenizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Current parser instance
|
||||||
|
*
|
||||||
* @var Parser
|
* @var Parser
|
||||||
*/
|
*/
|
||||||
private $_parser;
|
private $_parser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Current helper list
|
||||||
|
*
|
||||||
* @var Helpers
|
* @var Helpers
|
||||||
*/
|
*/
|
||||||
private $_helpers;
|
private $_helpers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Current loader instance
|
||||||
|
*
|
||||||
* @var Loader
|
* @var Loader
|
||||||
*/
|
*/
|
||||||
private $_loader;
|
private $_loader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Current partial loader instance
|
||||||
|
*
|
||||||
* @var Loader
|
* @var Loader
|
||||||
*/
|
*/
|
||||||
private $_partialsLoader;
|
private $_partialsLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Current cache instance
|
||||||
|
*
|
||||||
* @var Cache
|
* @var Cache
|
||||||
*/
|
*/
|
||||||
private $_cache;
|
private $_cache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The escape method
|
||||||
|
*
|
||||||
* @var callable escape function to use
|
* @var callable escape function to use
|
||||||
*/
|
*/
|
||||||
private $_escape = 'htmlspecialchars';
|
private $_escape = 'htmlspecialchars';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Parameters for the escpae method above
|
||||||
|
*
|
||||||
* @var array parametes to pass to escape function
|
* @var array parametes to pass to escape function
|
||||||
*/
|
*/
|
||||||
private $_escapeArgs = array(
|
private $_escapeArgs = array(
|
||||||
@ -165,8 +181,8 @@ class Handlebars
|
|||||||
* @param mixed $data data to use as context
|
* @param mixed $data data to use as context
|
||||||
*
|
*
|
||||||
* @return string Rendered template
|
* @return string Rendered template
|
||||||
* @see Handlebars::loadTemplate
|
* @see Handlebars::loadTemplate
|
||||||
* @see Template::render
|
* @see Template::render
|
||||||
*/
|
*/
|
||||||
public function render($template, $data)
|
public function render($template, $data)
|
||||||
{
|
{
|
||||||
@ -235,8 +251,8 @@ class Handlebars
|
|||||||
{
|
{
|
||||||
return $this->getHelpers()->has($name);
|
return $this->getHelpers()->has($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new helper.
|
* Add a new helper.
|
||||||
*
|
*
|
||||||
* @param string $name helper name
|
* @param string $name helper name
|
||||||
@ -245,100 +261,99 @@ class Handlebars
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function registerHelper($name, $helper)
|
public function registerHelper($name, $helper)
|
||||||
{
|
{
|
||||||
$this->addHelper($name, function($template, $context, $arg) use ($helper)
|
$callback = function ($template, $context, $arg) use ($helper) {
|
||||||
{
|
$args = $template->parseArguments($arg);
|
||||||
$args = $template->parseArguments($arg);
|
$named = $template->parseNamedArguments($arg);
|
||||||
$named = $template->parseNamedArguments($arg);
|
|
||||||
|
foreach ($args as $i => $arg) {
|
||||||
foreach($args as $i => $arg) {
|
//if it's literally string
|
||||||
//if it's literally string
|
if ($arg instanceof StringWrapper) {
|
||||||
if($arg instanceof StringWrapper) {
|
//we have no problems here
|
||||||
//we have no problems here
|
$args[$i] = (string) $arg;
|
||||||
$args[$i] = (string) $arg;
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
//not sure what to do if it's not a string or StringWrapper
|
||||||
//not sure what to do if it's not a string or StringWrapper
|
if (!is_string($arg)) {
|
||||||
if(!is_string($arg)) {
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
//it's a variable and we need to figure out the value of it
|
||||||
//it's a variable and we need to figure out the value of it
|
$args[$i] = $context->get($arg);
|
||||||
$args[$i] = $context->get($arg);
|
}
|
||||||
}
|
|
||||||
|
//push the options
|
||||||
//push the options
|
$args[] = array(
|
||||||
$args[] = array(
|
//special fields
|
||||||
//special fields
|
'data' => array(
|
||||||
'data' => array(
|
'index' => $context->get('@index'),
|
||||||
'index' => $context->get('@index'),
|
'key' => $context->get('@key'),
|
||||||
'key' => $context->get('@key'),
|
'first' => $context->get('@first'),
|
||||||
'first' => $context->get('@first'),
|
'last' => $context->get('@last')),
|
||||||
'last' => $context->get('@last')),
|
// Named arguments
|
||||||
// Named arguments
|
'hash' => $named,
|
||||||
'hash' => $named,
|
// A renderer for block helper
|
||||||
// A renderer for block helper
|
'fn' => function ($inContext = null) use ($context, $template) {
|
||||||
'fn' => function($inContext = null) use($context, $template)
|
$defined = !!$inContext;
|
||||||
{
|
|
||||||
$defined = !!$inContext;
|
if (!$defined) {
|
||||||
|
$inContext = $context;
|
||||||
if(!$defined) {
|
$inContext->push($inContext->last());
|
||||||
$inContext = $context;
|
} else if (!$inContext instanceof Context) {
|
||||||
$inContext->push($inContext->last());
|
$inContext = new ChildContext($inContext);
|
||||||
} else if (!$inContext instanceof Context) {
|
$inContext->setParent($context);
|
||||||
$inContext = new ChildContext($inContext);
|
}
|
||||||
$inContext->setParent($context);
|
|
||||||
}
|
$template->setStopToken('else');
|
||||||
|
$buffer = $template->render($inContext);
|
||||||
$template->setStopToken('else');
|
$template->setStopToken(false);
|
||||||
$buffer = $template->render($inContext);
|
//what if it's a loop ?
|
||||||
$template->setStopToken(false);
|
$template->rewind();
|
||||||
//what if it's a loop ?
|
//What's the point of this again?
|
||||||
$template->rewind();
|
//I mean in this context (literally)
|
||||||
//What's the point of this again?
|
//$template->discard($inContext);
|
||||||
//I mean in this context (literally)
|
|
||||||
//$template->discard($inContext);
|
if ($defined) {
|
||||||
|
$inContext->pop();
|
||||||
if($defined) {
|
}
|
||||||
$inContext->pop();
|
|
||||||
}
|
return $buffer;
|
||||||
|
},
|
||||||
return $buffer;
|
|
||||||
},
|
// A render for the else block
|
||||||
|
'inverse' => function ($inContext = null) use ($context, $template) {
|
||||||
// A render for the else block
|
$defined = !!$inContext;
|
||||||
'inverse' => function($inContext = null) use($context, $template)
|
|
||||||
{
|
if (!$defined) {
|
||||||
$defined = !!$inContext;
|
$inContext = $context;
|
||||||
|
$inContext->push($inContext->last());
|
||||||
if(!$defined) {
|
} else if (!$inContext instanceof Context) {
|
||||||
$inContext = $context;
|
$inContext = new ChildContext($inContext);
|
||||||
$inContext->push($inContext->last());
|
$inContext->setParent($context);
|
||||||
} else if (!$inContext instanceof Context) {
|
}
|
||||||
$inContext = new ChildContext($inContext);
|
|
||||||
$inContext->setParent($context);
|
$template->setStopToken('else');
|
||||||
}
|
$template->discard($inContext);
|
||||||
|
$template->setStopToken(false);
|
||||||
$template->setStopToken('else');
|
$buffer = $template->render($inContext);
|
||||||
$template->discard($inContext);
|
|
||||||
$template->setStopToken(false);
|
if ($defined) {
|
||||||
$buffer = $template->render($inContext);
|
$inContext->pop();
|
||||||
|
}
|
||||||
if($defined) {
|
|
||||||
$inContext->pop();
|
return $buffer;
|
||||||
}
|
},
|
||||||
|
|
||||||
return $buffer;
|
// The current context.
|
||||||
},
|
'context' => $context,
|
||||||
|
// The current template
|
||||||
// The current context.
|
'template' => $template);
|
||||||
'context' => $context,
|
|
||||||
// The current template
|
return call_user_func_array($helper, $args);
|
||||||
'template' => $template);
|
};
|
||||||
|
|
||||||
return call_user_func_array($helper, $args);
|
$this->addHelper($name, $callback);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -366,7 +381,7 @@ class Handlebars
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get current loader
|
* Get current loader
|
||||||
*
|
*
|
||||||
* @return Loader
|
* @return Loader
|
||||||
*/
|
*/
|
||||||
@ -392,7 +407,7 @@ class Handlebars
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get current partials loader
|
* Get current partials loader
|
||||||
*
|
*
|
||||||
* @return Loader
|
* @return Loader
|
||||||
*/
|
*/
|
||||||
@ -618,7 +633,7 @@ class Handlebars
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* try to tokenize source, or get them from cache if available
|
* Try to tokenize source, or get them from cache if available
|
||||||
*
|
*
|
||||||
* @param string $source handlebars source code
|
* @param string $source handlebars source code
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user