Commits

Toro Hill committed 13c050a

Make the code which handles the path and suffix configuration options smarter.

Comments (0)

Files changed (1)

PHPTemplate/Template.php

 	 *
 	 * The options are:
 	 * 	* path - Default path to templates.  
-	 * 		Will be prepended to all template names regardless of whether they are relative or absolute.
-	 * 		Set path to empty string to unset the current path.
+	 * 		Will only prepended to relative template names, absolute names (will be left alone) 
+	 * 		Trailing slash is optional. Set path to NULL to unset the current path.
 	 * 	* suffix - Default template suffix.
-	 * 		Will be appended to template file names for all template objects.
-	 *		Should contain joining . (eg. .html.php)
-	 * 		Set suffix to empty string to unset the current suffix.
+	 * 		Will be appended to template file names for all template objects unless the 
+	 * 		suffix is already present. Should contain joining . (eg. .html.php)
+	 * 		Set suffix to NULL to unset the current suffix.
 	 *
 	 * Uses an associative array of configuration options instead of individual
 	 * static properties as it's easier to add new options.
 	 * 
 	 */
 	protected static $config = array(
-		'path' => ''
-		, 'suffix' => ''
+		'path' => NULL
+		, 'suffix' => NULL
 	);
 
 	/**
 		extract($this->vars, EXTR_SKIP);
 
 		ob_start();
-		include(self::$config['path'] . $this->file . self::$config['suffix']);
+		include($this->getFileName());
 		$content = ob_get_contents();
 		ob_end_clean();
 
 	}
 
 	/**
+	 * Return the full file name of the current template based on the current configuration.
+	 *
+	 * $config['path'] will not be prepended if the template file name is already absolute.
+	 * $config['suffix'] will not be appended if the template file name already ends in the suffix.
+	 *
+	 * @return	string	Full file name 
+	 */
+	protected function getFileName(){
+		$path = '';
+		if(!is_null(self::$config['path']) && '/' !== substr($this->file, 0, 1)){
+			$path .= self::$config['path'];
+			if('/' !== substr($path, -1)){
+				// Add a joining / is needed.
+				$path .= '/';
+			}
+		}
+		$path .= $this->file;
+		if(!is_null(self::$config['suffix']) 
+			&& self::$config['suffix'] !== substr($path, -strlen(self::$config['suffix']))){
+
+			$path .= self::$config['suffix'];
+		}
+		return $path;
+	}
+
+	/**
 	 * Set the default configuration options for all templates.
 	 *
 	 * @param	array	$config 	Associative array of configuration options.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.