From 1119784549db53908d7e4b259492986d3875df79 Mon Sep 17 00:00:00 2001 From: cgray Date: Thu, 16 Jan 2014 12:41:02 -0600 Subject: [PATCH 1/4] Update Context::get to resolve @key and @index --- src/Handlebars/Context.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index f92d601..113a8f3 100755 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -219,6 +219,10 @@ class Context return ''; } elseif ($variableName == '.' || $variableName == 'this') { return $current; + } elseif ($variableName == '@index') { + $current = $this->lastIndex(); + } elseif ($variableName == '@key') { + $current = $this->lastKey(); } else { $chunks = explode('.', $variableName); foreach ($chunks as $chunk) { From da1cf77c58d44c5f1b86bf25c32f958a07a4242b Mon Sep 17 00:00:00 2001 From: Chris Gray Date: Thu, 16 Jan 2014 23:28:48 -0600 Subject: [PATCH 2/4] Added string literal support in Context::get and added Template::parseArguments to ease plugin development --- src/Handlebars/Context.php | 4 +++- src/Handlebars/Template.php | 22 ++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index 113a8f3..870b4ff 100755 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -9,6 +9,7 @@ * @package Handlebars * @author fzerorubigd * @author Behrooz Shabani + * @author Chris Gray * @copyright 2012 (c) ParsPooyesh Co * @copyright 2013 (c) Behrooz Shabani * @copyright 2013 (c) f0ruD A @@ -223,6 +224,8 @@ class Context $current = $this->lastIndex(); } elseif ($variableName == '@key') { $current = $this->lastKey(); + } elseif ($variableName[0] == "'" || $variableName[0] == '"') { + $current = trim($variableName, '"\''); } else { $chunks = explode('.', $variableName); foreach ($chunks as $chunk) { @@ -232,7 +235,6 @@ class Context $current = $this->_findVariableInContext($current, $chunk, $strict); } } - return $current; } diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 0c77ee3..c204383 100755 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -9,6 +9,7 @@ * @package Handlebars * @author fzerorubigd * @author Behrooz Shabani + * @author Chris Gray * @copyright 2012 (c) ParsPooyesh Co * @copyright 2013 (c) Behrooz Shabani * @license MIT @@ -408,12 +409,6 @@ class Template { $name = $current[Tokenizer::NAME]; $value = $context->get($name); - if ($name == '@index') { - return $context->lastIndex(); - } - if ($name == '@key') { - return $context->lastKey(); - } if ($escaped) { $args = $this->handlebars->getEscapeArgs(); array_unshift($args, $value); @@ -425,4 +420,19 @@ class Template return $value; } + + /** + * Break an argument string into an array of strings + * + * @param string $string Argument String as passed to a helper + * + * @return array the argument list as an array + */ + public function parseArguments($string){ + $parts = array(); + preg_match_all('#(? Date: Fri, 17 Jan 2014 00:30:14 -0600 Subject: [PATCH 3/4] Fixed edge case in Context::get where trim was eating trailing escaped quote in a quoted literal string that ends with an escaped quote --- src/Handlebars/Context.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index 870b4ff..80f6a19 100755 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -184,6 +184,7 @@ class Context * @param boolean $strict strict search? if not found then throw exception * * @throws \InvalidArgumentException in strict mode and variable not found + * @throws \RuntimeException if supplied argument is a malformed quoted string * @return mixed */ public function get($variableName, $strict = false) @@ -225,7 +226,11 @@ class Context } elseif ($variableName == '@key') { $current = $this->lastKey(); } elseif ($variableName[0] == "'" || $variableName[0] == '"') { - $current = trim($variableName, '"\''); + if ($variableName[0] == substr($variableName, -1) && strlen($variableName) > 2){ + $current = substr($variableName, 1, strlen($variableName) -2); + } else { + throw new \RuntimeException("Malformed string: ".$variableName); + } } else { $chunks = explode('.', $variableName); foreach ($chunks as $chunk) { From 415e642351c5db3eefbc64c6eb0c58e5d1a1a3e6 Mon Sep 17 00:00:00 2001 From: Chris Gray Date: Fri, 17 Jan 2014 00:46:39 -0600 Subject: [PATCH 4/4] Fixed to conform to coding standards --- src/Handlebars/Context.php | 2 +- src/Handlebars/Template.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index 80f6a19..b364c24 100755 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -226,7 +226,7 @@ class Context } elseif ($variableName == '@key') { $current = $this->lastKey(); } elseif ($variableName[0] == "'" || $variableName[0] == '"') { - if ($variableName[0] == substr($variableName, -1) && strlen($variableName) > 2){ + if ($variableName[0] == substr($variableName, -1) && strlen($variableName) > 2) { $current = substr($variableName, 1, strlen($variableName) -2); } else { throw new \RuntimeException("Malformed string: ".$variableName); diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index c204383..eb19fca 100755 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -424,11 +424,12 @@ class Template /** * Break an argument string into an array of strings * - * @param string $string Argument String as passed to a helper + * @param string $string Argument String as passed to a helper * * @return array the argument list as an array */ - public function parseArguments($string){ + public function parseArguments($string) + { $parts = array(); preg_match_all('#(?