Commit 41717226 authored by Taddeus Kroes's avatar Taddeus Kroes

Added object/associative array variable types to template.

parent 7bf29b24
...@@ -148,6 +148,9 @@ class pQueryTemplate extends pQuery implements pQueryExtension { ...@@ -148,6 +148,9 @@ class pQueryTemplate extends pQuery implements pQueryExtension {
* Apply any of the following helper functions: * Apply any of the following helper functions:
* - Translation: <code>{_:name[:count_var_name]}</code> * - Translation: <code>{_:name[:count_var_name]}</code>
* - Default: <code>{var_name[:func1:func2:...]}</code> * - Default: <code>{var_name[:func1:func2:...]}</code>
* 'var_name' can be of the form 'foo.bar'. In this case, 'foo' is the
* name of an object ot associative array variable. 'bar' is a property
* name to get of the object, or the associative index to the array.
* *
* @param string $variable The variable to replace. * @param string $variable The variable to replace.
* @param Block $data The data block to search in for the value. * @param Block $data The data block to search in for the value.
...@@ -164,7 +167,18 @@ class pQueryTemplate extends pQuery implements pQueryExtension { ...@@ -164,7 +167,18 @@ class pQueryTemplate extends pQuery implements pQueryExtension {
return '--translation--'; return '--translation--';
break; break;
default: default:
if( strpos($name, '.') !== false ) {
list($object_name, $property) = explode('.', $name, 2);
$object = $data->get($object_name);
if( is_object($object) && property_exists($object, $property) ) {
$value = $object->$property;
} elseif( is_array($object) && isset($object[$property]) ) {
$value = $object[$property];
}
} else {
$value = $data->get($name); $value = $data->get($name);
}
// Don't continue if the variable name is not foudn in the data block // Don't continue if the variable name is not foudn in the data block
if( $value === null ) if( $value === null )
......
...@@ -28,7 +28,10 @@ block:test1 ...@@ -28,7 +28,10 @@ block:test1
end:test1 end:test1
-variable value-
amet amet
-object property-
-assoc index-
block:test3 block:test3
......
...@@ -17,7 +17,10 @@ block:test1 ...@@ -17,7 +17,10 @@ block:test1
end:test1 end:test1
{end} {end}
{variable}
amet amet
{object.property}
{assoc.index}
{block:test3} {block:test3}
block:test3 block:test3
......
...@@ -70,15 +70,21 @@ class pQueryTemplateTest extends UnitTestCase { ...@@ -70,15 +70,21 @@ class pQueryTemplateTest extends UnitTestCase {
function test_parse() { function test_parse() {
// Add some blocks with test variables // Add some blocks with test variables
$this->tpl->data->set('variable', '-variable value-');
$object = new StdClass;
$object->property = '-object property-';
$this->tpl->data->set('object', $object);
$this->tpl->data->set('assoc', array('index' => '-assoc index-'));
$test1 = $this->tpl->data->add('test1', array('var' => 'some-variable')); $test1 = $this->tpl->data->add('test1', array('var' => 'some-variable'));
$this->tpl->data->add('test1', array('var' => 'some-other-variable')); $this->tpl->data->add('test1', array('var' => 'some-other-variable'));
$test1->add('test2'); $test1->add('test2');
$this->tpl->data->add('test3'); $this->tpl->data->add('test3');
// Expected content is defined in a text file // Expected content is defined in a text file
$expected_content = file_get_contents($this->templates_folder.'expect_parse.txt'); $expected_content = file_get_contents($this->templates_folder.'expect_parse.html');
$this->assertEqual($this->tpl->parse(), $expected_content, 'parsed templated does not match expected content'); $this->assertEqual($this->tpl->parse(), $expected_content);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment