return {{^var}} handler

This commit is contained in:
fzerorubigd 2012-11-29 12:20:49 +03:30
parent 9fb753f75c
commit 8d266bfcd8
3 changed files with 77 additions and 51 deletions

View File

@ -151,6 +151,12 @@ class Handlebars_Template
$buffer .= $this->_section($context, $current); $buffer .= $this->_section($context, $current);
array_pop($this->_stack); array_pop($this->_stack);
break; break;
case Handlebars_Tokenizer::T_INVERTED :
$newStack = isset($current[Handlebars_Tokenizer::NODES]) ? $current[Handlebars_Tokenizer::NODES] : array();
array_push($this->_stack, array(0, $newStack));
$buffer .= $this->_inverted($context, $current);
array_pop($this->_stack);
break;
case Handlebars_Tokenizer::T_COMMENT : case Handlebars_Tokenizer::T_COMMENT :
$buffer .= ''; $buffer .= '';
break; break;
@ -265,6 +271,26 @@ class Handlebars_Template
} }
} }
/**
* Process inverted section
*
* @param Handlebars_Context $context current context
* @param array $current section node data
*
* @return string the result
*/
private function _inverted(Handlebars_Context $context, $current)
{
$sectionName = $current[Handlebars_Tokenizer::NAME];
$data = $context->get($sectionName);
if (!$data) {
return $this->render($context);
} else {
//No need to disacard here, since itshas no else
return '';
}
}
/** /**
* Process partial section * Process partial section
* *

View File

@ -26,8 +26,8 @@
* @category Xamin * @category Xamin
* @package Handlebars * @package Handlebars
* @author Justin Hileman <dontknow@example.org> * @author Justin Hileman <dontknow@example.org>
* @copyright 2012 Justin Hileman
* @author fzerorubigd <fzerorubigd@gmail.com> * @author fzerorubigd <fzerorubigd@gmail.com>
* @copyright 2012 Justin Hileman
* @license MIT <http://opensource.org/licenses/mit-license.php> * @license MIT <http://opensource.org/licenses/mit-license.php>
* @version Release: @package_version@ * @version Release: @package_version@
* @link http://xamin.ir * @link http://xamin.ir
@ -42,7 +42,7 @@ class Handlebars_Tokenizer
// Token types // Token types
const T_SECTION = '#'; const T_SECTION = '#';
//const T_INVERTED = '^'; //Must remove this const T_INVERTED = '^';
const T_END_SECTION = '/'; const T_END_SECTION = '/';
const T_COMMENT = '!'; const T_COMMENT = '!';
const T_PARTIAL = '>'; //Maybe remove this partials and replace them with helpers const T_PARTIAL = '>'; //Maybe remove this partials and replace them with helpers
@ -56,7 +56,7 @@ class Handlebars_Tokenizer
// Valid token types // Valid token types
private static $_tagTypes = array( private static $_tagTypes = array(
self::T_SECTION => true, self::T_SECTION => true,
//self::T_INVERTED => true, self::T_INVERTED => true,
self::T_END_SECTION => true, self::T_END_SECTION => true,
self::T_COMMENT => true, self::T_COMMENT => true,
self::T_PARTIAL => true, self::T_PARTIAL => true,
@ -157,7 +157,7 @@ class Handlebars_Tokenizer
default: default:
if ($this->tagChange($this->ctag, $text, $i)) { if ($this->tagChange($this->ctag, $text, $i)) {
// Sections (Helpers) can accept parameters // Sections (Helpers) can accept parameters
if ($this->tagType == self::T_SECTION /*|| $this->tagType == self::T_INVERTED*/) { if ($this->tagType == self::T_SECTION) {
$newBuffer = explode(' ', trim($this->buffer), 2); $newBuffer = explode(' ', trim($this->buffer), 2);
$args = ''; $args = '';
if (count($newBuffer) == 2) { if (count($newBuffer) == 2) {