mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-03 18:43:07 +03:00
Added Tests and changed internal representation of quoted string literals from strangely quoted string to an instance of \Handlebars\String
This commit is contained in:
parent
49dcbb2573
commit
a44777f52f
@ -189,7 +189,9 @@ class Context
|
|||||||
*/
|
*/
|
||||||
public function get($variableName, $strict = false)
|
public function get($variableName, $strict = false)
|
||||||
{
|
{
|
||||||
//Need to clean up
|
if ($variableName instanceof \Handlebars\String){
|
||||||
|
return (string)$variableName;
|
||||||
|
}
|
||||||
$variableName = trim($variableName);
|
$variableName = trim($variableName);
|
||||||
$level = 0;
|
$level = 0;
|
||||||
while (substr($variableName, 0, 3) == '../') {
|
while (substr($variableName, 0, 3) == '../') {
|
||||||
@ -217,7 +219,6 @@ class Context
|
|||||||
'can not find variable in context'
|
'can not find variable in context'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
} elseif ($variableName == '.' || $variableName == 'this') {
|
} elseif ($variableName == '.' || $variableName == 'this') {
|
||||||
return $current;
|
return $current;
|
||||||
@ -225,8 +226,6 @@ class Context
|
|||||||
$current = $this->lastIndex();
|
$current = $this->lastIndex();
|
||||||
} elseif ($variableName == '@key') {
|
} elseif ($variableName == '@key') {
|
||||||
$current = $this->lastKey();
|
$current = $this->lastKey();
|
||||||
} elseif ($variableName instanceof \Handlebars\String){
|
|
||||||
$current = (string)$variableName;
|
|
||||||
} else {
|
} else {
|
||||||
$chunks = explode('.', $variableName);
|
$chunks = explode('.', $variableName);
|
||||||
foreach ($chunks as $chunk) {
|
foreach ($chunks as $chunk) {
|
||||||
|
@ -446,7 +446,6 @@ class Template
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $args;
|
return $args;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,6 +94,16 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
'{{data.length}}',
|
'{{data.length}}',
|
||||||
array("data"=>array("length"=>"15 inches", "test","test","test")),
|
array("data"=>array("length"=>"15 inches", "test","test","test")),
|
||||||
"15 inches"
|
"15 inches"
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{{data.0}}',
|
||||||
|
array("data" => array(1,2,3,4)),
|
||||||
|
'1'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{{data.property.3}}',
|
||||||
|
array("data"=>array("property"=>array(1,2,3,4))),
|
||||||
|
'4'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -527,6 +537,41 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function stringLiteralInCustomHelperProvider(){
|
||||||
|
return array(
|
||||||
|
array('{{#test2 arg1 "Argument 2"}}', array("arg1"=>"Argument 1"), "Argument 1:Argument 2"),
|
||||||
|
array('{{#test2 "Argument 1" "Argument 2"}}', array("arg1"=>"Argument 1"), "Argument 1:Argument 2"),
|
||||||
|
array('{{#test2 "Argument 1" arg2}}', array("arg2"=>"Argument 2"), "Argument 1:Argument 2")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Test String literals in the context of a helper
|
||||||
|
*
|
||||||
|
* @param string $template template text
|
||||||
|
* @param array $data context data
|
||||||
|
* @param string $results The Expected Results
|
||||||
|
*
|
||||||
|
* @dataProvider stringLiteralInCustomHelperProvider
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testStringLiteralInCustomHelper($template, $data, $results){
|
||||||
|
$engine = new \Handlebars\Handlebars();
|
||||||
|
$engine->addHelper('test2', function ($template, $context, $args) {
|
||||||
|
$args = $template->parseArguments($args);
|
||||||
|
|
||||||
|
$args = array_map(function ($a) use ($context) {return $context->get($a);}, $args);
|
||||||
|
return implode(':', $args);
|
||||||
|
});
|
||||||
|
$res = $engine->render($template, $data);
|
||||||
|
$this->assertEquals($res, $results);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testString(){
|
||||||
|
$string = new \Handlebars\String("Hello World");
|
||||||
|
$this->assertEquals((string)$string, "Hello World");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user