Przeglądaj źródła

Added object/associative array variable types to template.

Taddeus Kroes 14 lat temu
rodzic
commit
41717226c1

+ 15 - 1
pquery.template.php

@@ -148,6 +148,9 @@ class pQueryTemplate extends pQuery implements pQueryExtension {
 	 * Apply any of the following helper functions:
 	 * - Translation: <code>{_:name[:count_var_name]}</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 Block $data The data block to search in for the value.
@@ -164,7 +167,18 @@ class pQueryTemplate extends pQuery implements pQueryExtension {
 				return '--translation--';
 				break;
 			default:
-				$value = $data->get($name);
+				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);
+				}
 				
 				// Don't continue if the variable name is not foudn in the data block
 				if( $value === null )

+ 3 - 0
test/templates/expect_parse.txt → test/templates/expect_parse.html

@@ -28,7 +28,10 @@ block:test1
 end:test1
 
 
+-variable value-
 amet
+-object property-
+-assoc index-
 
 
 block:test3

+ 3 - 0
test/templates/test.tpl

@@ -17,7 +17,10 @@ block:test1
 end:test1
 {end}
 
+{variable}
 amet
+{object.property}
+{assoc.index}
 
 {block:test3}
 block:test3

+ 8 - 2
test/test_template.php

@@ -70,15 +70,21 @@ class pQueryTemplateTest extends UnitTestCase {
 	
 	function test_parse() {
 		// 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'));
 		$this->tpl->data->add('test1', array('var' => 'some-other-variable'));
 		$test1->add('test2');
 		$this->tpl->data->add('test3');
 		
 		// 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);
 	}
 }