Przeglądaj źródła

Some bugfixes in Template class:

- Multiline bracket notation is no longer allowed for expressions because of
  JavaScript/CSS clashes.
- Variables in expressions can now be preceded by characters unequal to `$',
  which will be prefixed to the variable evaluation.
Taddeus Kroes 13 lat temu
rodzic
commit
e7f038623e

+ 13 - 9
template.php

@@ -188,7 +188,7 @@ class Template extends Node {
 			$line_count += substr_count($before, "\n");
 			
 			// Everything before the new block belongs to its parent
-			$current->add('html')->set('content', $before);
+			$html = $current->add('html')->set('content', $before);
 			
 			if( $brackets_content == 'end' ) {
 				// {end} encountered, go one level up in the tree
@@ -201,6 +201,9 @@ class Template extends Node {
 				$block_name = substr($brackets_content, 6);
 				// Go one level deeper into the tree
 				$current = $current->add('block')->set('name', $block_name);
+			} elseif( strpos($brackets_content, "\n") !== false ) {
+				// Bracket content contains newlines, so it is probably JavaScript or CSS
+				$html->set('content', $before . '{' . $brackets_content . '}');
 			} else {
 				// Variable or something else
 				$current->add('expression')->set('content', $brackets_content);
@@ -213,7 +216,7 @@ class Template extends Node {
 			throw new ParseError($this, 'missing {end}', $line_count + 1);
 		
 		// Add the last remaining content to the root node
-		$root->add('html')->set('content', $after);
+		$after && $root->add('html')->set('content', $after);
 		
 		$this->root_block = $root;
 	}
@@ -272,12 +275,13 @@ class Template extends Node {
 	 * @throws \UnexpectedValueException In some other error situations.
 	 */
 	private static function evaluate_variable(array $matches, Node $data) {
-		$variable = $matches[1];
+		$before = $matches[1];
+		$variable = $matches[2];
 		$value = $data->get($variable);
 		
-		if( count($matches) == 3 ) {
+		if( count($matches) == 4 ) {
 			// $<name>.<name>
-			$attribute = $matches[2];
+			$attribute = $matches[3];
 			
 			if( $value === null ) {
 				throw new \UnexpectedValueException(
@@ -300,9 +304,9 @@ class Template extends Node {
 			} else {
 				$attr_error('variable is no array or object');
 			}
-		} elseif( count($matches) == 4 ) {
+		} elseif( count($matches) == 5 ) {
 			// $<name>.<name>()
-			$method = $matches[2];
+			$method = $matches[3];
 			
 			if( $value === null ) {
 				throw new \UnexpectedValueException(
@@ -324,7 +328,7 @@ class Template extends Node {
 			}
 		}
 		
-		return $value;
+		return $before . $value;
 	}
 	
 	/**
@@ -416,7 +420,7 @@ class Template extends Node {
 			if( preg_match("/^([^?]*?)\s*\?([^:]*)(?::(.*))?$/", $expression, $matches) ) {
 				// <nested_exp>?<nested_exp> | <nested_exp>?<nested_exp>:<nested_exp>
 				return self::evaluate_condition($matches, $data);
-			} elseif( preg_match("/^\\$($name)(?:\.($name)(\(\))?)?$/", $expression, $matches) ) {
+			} elseif( preg_match("/^(.*?)\\$($name)(?:\.($name)(\(\))?)?$/", $expression, $matches) ) {
 				// $<name> | $<name>.<name> | $<name>.<name>()
 				return self::evaluate_variable($matches, $data);
 			} elseif( preg_match("/^($function)\((.+?)\)?$/", $expression, $matches) ) {

+ 4 - 1
tests/_files/rendered/variables.html

@@ -2,4 +2,7 @@ foo
 my_foobar_variable
 bar
 my_foobaz_variable
-baz
+baz
+{
+no_variable
+}

+ 4 - 1
tests/_files/templates/variables.tpl

@@ -2,4 +2,7 @@ foo
 {$foobar}
 bar
 {strtolower($foobaz)}
-baz
+baz
+{
+no_variable
+}

+ 7 - 6
tests/test_template.php

@@ -142,12 +142,11 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
 	function test_parse_blocks_blocks() {
 		$tpl = new Template('blocks');
 		$root_block = $this->get_property($tpl, 'root_block');
-		$this->assert_is_block_node($root_block, null, 3);
+		$this->assert_is_block_node($root_block, null, 2);
 		
-		list($before, $foo, $after) = $root_block->get_children();
+		list($before, $foo) = $root_block->get_children();
 		$this->assert_is_html_node($before, '');
 		$this->assert_is_block_node($foo, 'foo', 3);
-		$this->assert_is_html_node($after, '');
 		
 		list($foofoo, $bar, $foobaz) = $foo->get_children();
 		$this->assert_is_html_node($foofoo, "\nfoofoo\n\t");
@@ -189,7 +188,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
 		$this->assert_is_exp_node($foobar, '$foobar');
 		$this->assert_is_html_node($bar, "\nbar\n");
 		$this->assert_is_exp_node($foobaz, 'strtolower($foobaz)');
-		$this->assert_is_html_node($baz, "\nbaz");
+		$this->assert_is_html_node($baz, "\nbaz\n{\nno_variable\n}");
 	}
 	
 	/**
@@ -304,7 +303,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
 	 * @depends test_evaluate_condition_if
 	 * @depends test_evaluate_condition_if_else
 	 */
-	function test_evaluate_condition_spaces() {
+	function test_evaluate_condition_extended() {
 		$this->assert_evaluates(' bar ', '$true? bar : baz');
 		$this->assert_evaluates(' baz', '$false? bar : baz');
 		
@@ -313,6 +312,8 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
 		
 		$this->assert_evaluates(' Foo bar ', '$true ? Foo bar : Baz foo');
 		$this->assert_evaluates(' Baz foo', '$false ? Foo bar : Baz foo');
+		
+		$this->assert_evaluates('| bar', '$true ?| $foo');
 	}
 	
 	/** 
@@ -343,7 +344,7 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
 	/**
 	 * @depends test_evaluate_variable_success
 	 * @depends test_evaluate_no_expression
-	 * @depends test_evaluate_condition_spaces
+	 * @depends test_evaluate_condition_extended
 	 * @depends test_evaluate_function_success
 	 * @depends test_evaluate_default_value
 	 */