Ver Fonte

Added default value to template expression grammar.

Taddeus Kroes há 13 anos atrás
pai
commit
97c7d6c3fe
2 ficheiros alterados com 20 adições e 4 exclusões
  1. 13 4
      template.php
  2. 7 0
      tests/test_template.php

+ 13 - 4
template.php

@@ -71,10 +71,12 @@ require_once 'node.php';
  * curly brackets: *{expression}*. The grammar of all expressions that are
  * currently supported can be described as follows:
  * <code>
- * &lt;expression&gt; : {&lt;nested_exp&gt;}
- *              | {&lt;nested_exp&gt;?&lt;nested_exp&gt;:&lt;nested_exp&gt;}  # Conditional statement
+ * &lt;expression&gt; : {&lt;exp&gt;}
+ * &lt;exp&gt; : &lt;nested_exp&gt;
+ *       | &lt;nested_exp&gt;?&lt;nested_exp&gt;:&lt;nested_exp&gt;  # Conditional statement
  * &lt;nested_exp&gt; : &lt;variable&gt;
- *              | &lt;function&gt;(&lt;nested_exp&gt;)  # Static function call
+ *              | &lt;nested_exp&gt;||&lt;nested_exp&gt;  # Default value
+ *              | &lt;function&gt;(&lt;nested_exp&gt;)    # Static function call
  *              | &lt;constant&gt;
  *              | &lt;html&gt;
  * &lt;variable&gt; : $&lt;name&gt;             # Regular variable
@@ -83,7 +85,7 @@ require_once 'node.php';
  * &lt;function&gt; : &lt;name&gt;          # Global function
  *            | &lt;name&gt;::&lt;name&gt;  # Static class method
  * &lt;constant&gt; : An all-caps PHP constant: [A-Z0-9_]+
- * &lt;html&gt; : A string without parentheses, curly brackets or semicolons: [^(){}:]*
+ * &lt;html&gt; : A string without parentheses, pipes, curly brackets or semicolons: [^()|{}:]*
  * &lt;name&gt; : A non-empty variable/method name consisting of [a-zA-Z0-9-_]+
  * </code>
  * 
@@ -423,6 +425,13 @@ class Template extends Node {
 			} elseif( preg_match("/^([A-Z0-9_]+)$/", $expression, $matches) ) {
 				// <constant>
 				return self::evaluate_constant($expression, $root_level);
+			} elseif( ($split_at = strpos($expression, '||', 1)) !== false ) {
+				// <nested_exp>||<nested_exp>
+				try {
+					return self::evaluate_expression(substr($expression, 0, $split_at), $data, false);
+				} catch(\RuntimeException $e) {
+					return self::evaluate_expression(substr($expression, $split_at + 2), $data, false);
+				}
 			}
 		}
 		

+ 7 - 0
tests/test_template.php

@@ -334,11 +334,18 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
 		$this->assert_evaluates('Bar', 'ucfirst(strtolower($FOO))');
 	}
 	
+	function test_evaluate_default_value() {
+		$this->assert_evaluates('bar', '$foo||fallback');
+		$this->assert_evaluates('fallback', '$foo.bar||fallback');
+		$this->assert_evaluates('', '$foo.bar||');
+	}
+	
 	/**
 	 * @depends test_evaluate_variable_success
 	 * @depends test_evaluate_no_expression
 	 * @depends test_evaluate_condition_spaces
 	 * @depends test_evaluate_function_success
+	 * @depends test_evaluate_default_value
 	 */
 	function test_evaluate_expression_combined() {
 		$this->assert_evaluates('Bar', '$true?ucfirst($foo)');