浏览代码

Added possibility to use constants in templates.

Taddeus Kroes 13 年之前
父节点
当前提交
c13df4a71d
共有 2 个文件被更改,包括 16 次插入2 次删除
  1. 7 2
      template.php
  2. 9 0
      tests/test_template.php

+ 7 - 2
template.php

@@ -251,7 +251,8 @@ class Template extends Node {
 	 * name to get of the object, or the associative index to the array.
 	 * *func1*, *func2*, etc. are helper functions that are executed in the
 	 * same order as listed. The retuen value of each helper function replaces
-	 * the previous variable value.
+	 * the previous variable value. *var_name* Can also be the name of a
+	 * defined constant.
 	 * 
 	 * ------------
 	 * If-statement
@@ -300,8 +301,12 @@ class Template extends Node {
 		}
 		
 		// Default: Simple variable name
-		if( !isset($value) )
+		if( !isset($value) ) {
 			$value = $data->get($name);
+			
+			if( $value === null && defined($name) )
+				$value = constant($name);
+		}
 		
 		// Don't continue if the variable name is not found in the data block
 		if( $value !== null ) {

+ 9 - 0
tests/test_template.php

@@ -224,6 +224,15 @@ class TemplateTest extends PHPUnit_Framework_TestCase {
 		$this->assert_replaces('Bar', 'FOO:strtolower:ucfirst');
 	}
 	
+	/**
+	 * @depends test_replace_variable_helper_functions
+	 */
+	function test_replace_variable_constant() {
+		define('FOOCONST', 'foobar');
+		$this->assert_replaces('foobar', 'FOOCONST');
+		$this->assert_replaces('FOOBAR', 'FOOCONST:strtoupper');
+	}
+	
 	/**
 	 * @expectedException UnexpectedValueException
 	 * @expectedExceptionMessage Helper function "idonotexist" is not callable.