[xtpl-svn] SF.net SVN: xtpl: [18] trunk/xtemplate.class.php

cocomp at users.sourceforge.net cocomp at users.sourceforge.net
Thu Jan 11 03:29:12 GMT 2007


Revision: 18
          http://xtpl.svn.sourceforge.net/xtpl/?rev=18&view=rev
Author:   cocomp
Date:     2007-01-10 19:29:11 -0800 (Wed, 10 Jan 2007)

Log Message:
-----------
Major update for initial PHP 5 version 
Deprecated: PHP 4 constructor, SetNullString (use set_null_string) and SetNullBlock (use set_null_block). Added sf Feature request #1529478. Fixed SF Bug #1261828. Introduced comment_preg property. Refactored assign method - may break some old functionality! Constructor now only calls restart method

Modified Paths:
--------------
    trunk/xtemplate.class.php

Property Changed:
----------------
    trunk/xtemplate.class.php

Modified: trunk/xtemplate.class.php
===================================================================
--- trunk/xtemplate.class.php	2007-01-11 03:09:28 UTC (rev 17)
+++ trunk/xtemplate.class.php	2007-01-11 03:29:11 UTC (rev 18)
@@ -10,118 +10,303 @@
  * @author Barnabas Debreceni [cranx at users.sourceforge.net]
  * @copyright Barnabas Debreceni 2000-2001
  * @author Jeremy Coates [cocomp at users.sourceforge.net]
- * @copyright Jeremy Coates 2002-2005
+ * @copyright Jeremy Coates 2002-2007
  * @see license.txt LGPL / BSD license
- */
-
-/* $Id$
-// $Log$
-// Revision 1.10  2006/05/02 14:47:11  cocomp
-// Removed new line at end of file
-//
-// Revision 1.9  2006/05/02 14:27:19  cocomp
-// 2006/05/02:
-//
-// - Added $over_write parameter to assign method which defaults to false. The
-//   behaviour is therefore to always overwrite the name/value array
-// - Fixed bug in regexes for older php version (specifically php 4.2.2) that
-//   caused regex to fail
-//
-// Revision 1.8  2005/08/17 09:57:58  cocomp
-// Changelog
-// 2005/08/17:
-//
-// - Improved phpDoc entries
-// - Added support for XTPL_DIR constant from SF Feature request 1202027:  Kenneth Kalmer
-// - Added support for comments in {FILE } includes e.g. {FILE \"somefile.xtpl\"# An included file}
-// - Rewritten assign method, now checks if $val is an array instead of $name which never would be. Also added new parameter for stripslashes support e.g. $xtpl->assign('var', 'var', get_magic_quotes_qpc());
-// - Moved most regular expressions to use | as delimiter
-// - Improved output with regard to block tags and line endings - much better for xml output
-// - Added support for empty files in file includes - previously required at least one byte
-//
-// Revision 1.7  2005/04/11 11:20:28  cocomp
-// Fixed backslashes issue (properly\!)
-//
-// Revision 1.6  2005/04/11 10:00:47  cocomp
-// Added restart() method sf:641407 feature request
-//
-// Revision 1.5  2005/04/08 09:17:37  cocomp
-// Fixed bug with backslashes sf:810773 & updated docs
-//
-// Revision 1.4  2005/04/07 12:02:52  cocomp
-// MAJOR UPDATE: E_ALL safe, better internal documentation, code readability ++, many bugfixes and new features - considered stable
-//
-*/
-
-/**
+ * @since PHP 5
+ * @link $HeadURL$
+ * @version $Id$
+ *
+ *
  * XTemplate class - http://www.phpxtemplate.org/ (x)html / xml generation with templates - fast & easy
- * Latest stable & CVS versions available @ http://sourceforge.net/projects/xtpl/
+ * Latest stable & Subversion versions available @ http://sourceforge.net/projects/xtpl/
  * License: LGPL / BSD - see license.txt
- *
- * Copyright (c) 2000-2001 Barnabas Debreceni [cranx at users.sourceforge.net], 2002-2005 Jeremy Coates [cocomp at users.sourceforge.net]
- *
- * contributors:
- * Ivar Smolin <okul at linux.ee> (14-march-2001)
- * - made some code optimizations
- * Bert Jandehoop <bert.jandehoop at users.info.wau.nl> (26-june-2001)
- * - new feature to substitute template files by other templates
- * - new method array_loop()
- *
- * Various contributions over the years from:
- * Code: Noel Walsh (NW), John Carter (JC)
- * Bug reporting: SadGeezer
+ * Changelog: see changelog.txt
  */
 class XTemplate {
 
-	/***[ variables ]***********************************************************/
+	/**
+	 * Properties
+	 */
 
-	var $filecontents = '';                               /* raw contents of template file */
-	var $blocks = array();                                /* unparsed blocks */
-	var $parsed_blocks = array();                 /* parsed blocks */
-	var $preparsed_blocks = array();          /* preparsed blocks, for file includes */
-	var $block_parse_order = array();         /* block parsing order for recursive parsing (sometimes reverse:) */
-	var $sub_blocks = array();                        /* store sub-block names for fast resetting */
-	var $vars = array();                                  /* variables array */
-	var $filevars = array();                          /* file variables array */
-	var $filevar_parent = array();                /* filevars' parent block */
-	var $filecache = array();                         /* file caching */
+	/**
+	 * Raw contents of the template file
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $filecontents = '';
 
-	var $tpldir = '';                     /* location of template files */
-	var $files = null;                    /* file names lookup table */
-	var $filename = '';
+	/**
+	 * Unparsed blocks
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $blocks = array();
 
+	/**
+	 * Parsed blocks
+	 *
+	 * @var unknown_type
+	 */
+	public $parsed_blocks = array();
+
+	/**
+	 * Preparsed blocks (for file includes)
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $preparsed_blocks = array();
+
+	/**
+	 * Block parsing order for recursive parsing
+	 * (Sometimes reverse :)
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $block_parse_order = array();
+
+	/**
+	 * Store sub-block names
+	 * (For fast resetting)
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $sub_blocks = array();
+
+	/**
+	 * Variables array
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $vars = array();
+
+	/**
+	 * File variables array
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $filevars = array();
+
+	/**
+	 * Filevars' parent block
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $filevar_parent = array();
+
+	/**
+	 * File caching during duration of script
+	 * e.g. files only cached to speed {FILE "filename"} repeats
+	 *
+	 * @access public
+	 * @var array
+	 */
+	public $filecache = array();
+
+	/**
+	 * Location of template files
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $tpldir = '';
+
+	/**
+	 * Filenames lookup table
+	 *
+	 * @access public
+	 * @var null
+	 */
+	public $files = null;
+
+	/**
+	 * Template filename
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $filename = '';
+
 	// moved to setup method so uses the tag_start & end_delims
-	var $file_delim = '';//"/\{FILE\s*\"([^\"]+)\"\s*\}/m";  /* regexp for file includes */
-	var $filevar_delim = '';//"/\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}/m";  /* regexp for file includes */
-	var $filevar_delim_nl = '';//"/^\s*\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}\s*\n/m";  /* regexp for file includes w/ newlines */
-	var $block_start_delim = '<!-- ';         /* block start delimiter */
-	var $block_end_delim = '-->';                 /* block end delimiter */
-	var $block_start_word = 'BEGIN:';         /* block start word */
-	var $block_end_word = 'END:';                 /* block end word */
+	/**
+	 * RegEx for file includes
+	 *
+	 * "/\{FILE\s*\"([^\"]+)\"\s*\}/m";
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $file_delim = '';
 
-	/* this makes the delimiters look like: <!-- BEGIN: block_name --> if you use my syntax. */
+	/**
+	 * RegEx for file include variable
+	 *
+	 * "/\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}/m";
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $filevar_delim = '';
 
-	var $tag_start_delim = '{';
-	var $tag_end_delim = '}';
+	/**
+	 * RegEx for file includes with newlines
+	 *
+	 * "/^\s*\{FILE\s*\{([A-Za-z0-9\._]+?)\}\s*\}\s*\n/m";
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $filevar_delim_nl = '';
+
+	/**
+	 * Template block start delimiter
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $block_start_delim = '<!-- ';
+
+	/**
+	 * Template block end delimiter
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $block_end_delim = '-->';
+
+	/**
+	 * Template block start word
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $block_start_word = 'BEGIN:';
+
+	/**
+	 * Template block end word
+	 *
+	 * The last 3 properties and this make the delimiters look like:
+	 * @example <!-- BEGIN: block_name -->
+	 * if you use the default syntax.
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $block_end_word = 'END:';
+
+	/**
+	 * Template tag start delimiter
+	 *
+	 * This makes the delimiters look like:
+	 * @example {tagname}
+	 * if you use the default syntax.
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $tag_start_delim = '{';
+
+	/**
+	 * Template tag end delimiter
+	 *
+	 * This makes the delimiters look like:
+	 * @example {tagname}
+	 * if you use the default syntax.
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $tag_end_delim = '}';
 	/* this makes the delimiters look like: {tagname} if you use my syntax. */
 
-	var $mainblock = 'main';
+	/**
+	 * Regular expression element for comments within tags and blocks
+	 *
+	 * @example {tagname#My Comment}
+	 * @example {tagname #My Comment}
+	 * @example <!-- BEGIN: blockname#My Comment -->
+	 * @example <!-- BEGIN: blockname #My Comment -->
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $comment_preg = '( ?#.*?)?';
 
-	var $output_type = 'HTML';
+	/**
+	 * Default main template block name
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $mainblock = 'main';
 
-	var $_null_string = array('' => '');             /* null string for unassigned vars */
-	var $_null_block = array('' => '');  /* null string for unassigned blocks */
-	var $_error = '';
-	var $_autoreset = true;                                     /* auto-reset sub blocks */
+	/**
+	 * Script output type
+	 *
+	 * @access public
+	 * @var string
+	 */
+	public $output_type = 'HTML';
 
-	var $_ignore_missing_blocks = true ;          // NW 17 oct 2002 - Set to FALSE to
-	// generate errors if a non-existant blocks is referenced
+	/**
+	 * Debug mode
+	 *
+	 * @access public
+	 * @var boolean
+	 */
+	public $debug = false;
 
-	// JC 20/11/02 for echoing the template filename if in development
-	var $_file_name_full_path = '';
+	/**
+	 * Null string for unassigned vars
+	 *
+	 * @access protected
+	 * @var array
+	 */
+	protected $_null_string = array('' => '');
 
 	/**
-     * Constructor - Instantiate the object
+	 * Null string for unassigned blocks
+	 *
+	 * @access protected
+	 * @var array
+	 */
+	protected $_null_block = array('' => '');
+
+	/**
+	 * Errors
+	 *
+	 * @access protected
+	 * @var string
+	 */
+	protected $_error = '';
+
+	/**
+	 * Auto-reset sub blocks
+	 *
+	 * @access protected
+	 * @var boolean
+	 */
+	protected $_autoreset = true;
+
+	/**
+	 * Set to FALSE to generate errors if a non-existant blocks is referenced
+	 *
+	 * @author NW
+	 * @since 2002/10/17
+	 * @access protected
+	 * @var boolean
+	 */
+	protected $_ignore_missing_blocks = true;
+
+	/**
+     * PHP 5 Constructor - Instantiate the object
      *
      * @param string $file Template file to work on
      * @param string $tpldir Location of template files (useful for keeping files outside web server root)
@@ -130,30 +315,25 @@
      * @param boolean $autosetup If true, run setup() as part of constuctor
      * @return XTemplate
      */
-	function XTemplate ($file,  $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) {
+	public function __construct($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) {
 
-		$this->filename = $file;
+		$this->restart($file, $tpldir, $files, $mainblock, $autosetup, $this->tag_start_delim, $this->tag_end_delim);
+	}
 
-		// JC 20/11/02 for echoing the template filename if in development
-		$this->_file_name_full_path = realpath($file);
+	/**
+     * PHP 4 Constructor - Instantiate the object
+     *
+     * @deprecated Use PHP 5 constructor instead
+     * @param string $file Template file to work on
+     * @param string $tpldir Location of template files (useful for keeping files outside web server root)
+     * @param array $files Filenames lookup
+     * @param string $mainblock Name of main block in the template
+     * @param boolean $autosetup If true, run setup() as part of constuctor
+     * @return XTemplate
+     */
+	public function XTemplate ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true) {
 
-		// From SF Feature request 1202027
-		// Kenneth Kalmer
-		$this->tpldir = $tpldir;
-		if (defined('XTPL_DIR') && empty($this->tpldir)) {
-			$this->tpldir = XTPL_DIR;
-		}
-
-		if (is_array($files)) {
-			$this->files = $files;
-		}
-
-		$this->mainblock = $mainblock;
-
-		if ($autosetup) {
-			// setup the rest of the preprocess elements
-			$this->setup();
-		}
+		assert('Deprecated - use PHP 5 constructor');
 	}
 
 
@@ -179,12 +359,10 @@
 	 * @param string $tag_start {
 	 * @param string $tag_end }
 	 */
-	function restart ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $tag_start = '{', $tag_end = '}') {
+	public function restart ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $tag_start = '{', $tag_end = '}') {
 
 		$this->filename = $file;
 
-		$this->_file_name_full_path = realpath($file);
-
 		// From SF Feature request 1202027
 		// Kenneth Kalmer
 		$this->tpldir = $tpldir;
@@ -226,7 +404,7 @@
      * @access public
      * @param boolean $add_outer If true is passed when called, it adds an outer main block to the file
      */
-	function setup ($add_outer = false) {
+	public function setup ($add_outer = false) {
 
 		$this->tag_start_delim = preg_quote($this->tag_start_delim);
 		$this->tag_end_delim = preg_quote($this->tag_end_delim);
@@ -234,13 +412,13 @@
 		// Setup the file delimiters
 
 		// regexp for file includes
-		$this->file_delim = "/" . $this->tag_start_delim . "FILE\s*\"([^\"]+)\"[ ?#.*?]?" . $this->tag_end_delim . "/m";
+		$this->file_delim = "/" . $this->tag_start_delim . "FILE\s*\"([^\"]+)\"" . $this->comment_preg . $this->tag_end_delim . "/m";
 
 		// regexp for file includes
-		$this->filevar_delim = "/" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)[ ?#.*?]?" . $this->tag_end_delim . "[ ?#.*?]?" . $this->tag_end_delim . "/m";
+		$this->filevar_delim = "/" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "/m";
 
 		// regexp for file includes w/ newlines
-		$this->filevar_delim_nl = "/^\s*" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)[ ?#.*?]?" . $this->tag_end_delim . "[ ?#.*?]?" . $this->tag_end_delim . "\s*\n/m";
+		$this->filevar_delim_nl = "/^\s*" . $this->tag_start_delim . "FILE\s*" . $this->tag_start_delim . "([A-Za-z0-9\._]+?)" . $this->comment_preg . $this->tag_end_delim . $this->comment_preg . $this->tag_end_delim . "\s*\n/m";
 
 		if (empty($this->filecontents)) {
 			// read in template file
@@ -260,12 +438,30 @@
 	/**
      * assign a variable
      *
+     * @example Simplest case:
+     * @example $xtpl->assign('name', 'value');
+     * @example {name} in template
+     *
+     * @example Array assign:
+     * @example $xtpl->assign(array('name' => 'value', 'name2' => 'value2'));
+     * @example {name} {name2} in template
+     *
+     * @example Value as array assign:
+     * @example $xtpl->assign('name', array('key' => 'value', 'key2' => 'value2'));
+     * @example {name.key} {name.key2} in template
+     *
+     * @example Reset array:
+     * @example $xtpl->assign('name', array('key' => 'value', 'key2' => 'value2'));
+     * @example // Other code then:
+     * @example $xtpl->assign('name', array('key3' => 'value3'), false);
+     * @example {name.key} {name.key2} {name.key3} in template
+     *
      * @access public
      * @param string $name Variable to assign $val to
      * @param string / array $val Value to assign to $name
-     * @param boolean $over_write Overwrite the name/vars array defaults to false which means the array is always overwritten
-     *//*
-	function assign ($name, $val = '') {
+	 * @param boolean $reset_array Reset the variable array if $val is an array
+     */
+	public function assign ($name, $val = '', $reset_array = true) {
 
 		if (is_array($name)) {
 
@@ -273,25 +469,24 @@
 
 				$this->vars[$k] = $v;
 			}
+		} elseif (is_array($val)) {
+
+			// Clear the existing values
+    		if ($reset_array) {
+    			$this->vars[$name] = array();
+    		}
+
+        	foreach ($val as $k => $v) {
+
+        		$this->vars[$name][$k] = $v;
+        	}
+
 		} else {
 
 			$this->vars[$name] = $val;
 		}
-	}*/
-	function assign ($name, $val='', $magic_quotes = false, $over_write = false) {
+	}
 
-    	if (is_array($val)) {
-    		if (!$over_write) {
-    			$this->vars[$name] = array();
-    		}
-        	foreach ($val as $k=>$v) {
-        		$this->vars[$name][$k] = $magic_quotes ? stripslashes($v) : $v;
-        	}
-        } else {
-        	$this->vars[$name] = $magic_quotes ? stripslashes($val) : $val;
-        }
-    }
-
 	/**
      * assign a file variable
      *
@@ -299,7 +494,7 @@
      * @param string $name Variable to assign $val to
      * @param string / array $val Values to assign to $name
      */
-	function assign_file ($name, $val = '') {
+	public function assign_file ($name, $val = '') {
 
 		if (is_array($name)) {
 
@@ -319,7 +514,7 @@
      * @access public
      * @param string $bname Block name to parse
      */
-	function parse ($bname) {
+	public function parse ($bname) {
 
 		if (isset($this->preparsed_blocks[$bname])) {
 
@@ -355,7 +550,8 @@
 		$var_array = array();
 
 		/* find & replace variables+blocks */
-		preg_match_all("|" . $this->tag_start_delim . "([A-Za-z0-9\._]+? ?#?.*?)" . $this->tag_end_delim. "|", $copy, $var_array);
+		preg_match_all("|" . $this->tag_start_delim . "([A-Za-z0-9\._]+?" . $this->comment_preg . ")" . $this->tag_end_delim. "|", $copy, $var_array);
+
 		$var_array = $var_array[1];
 
 		foreach ($var_array as $k => $v) {
@@ -388,12 +584,12 @@
 
 					if ($nul == '') {
 						// -----------------------------------------------------------
-						// Removed requriement for blocks to be at the start of string
+						// Removed requirement for blocks to be at the start of string
 						// -----------------------------------------------------------
 						//                      $copy=preg_replace("/^\s*\{".$v."\}\s*\n*/m","",$copy);
 						// Now blocks don't need to be at the beginning of a line,
 						//$copy=preg_replace("/\s*" . $this->tag_start_delim . $v . $this->tag_end_delim . "\s*\n*/m","",$copy);
-						$copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "(\n?)+|m", '', $copy);
+						$copy = preg_replace("|" . $this->tag_start_delim . $v . $this->tag_end_delim . "|m", '', $copy);
 
 					} else {
 
@@ -404,16 +600,16 @@
 					//$var = trim($var);
 					switch (true) {
 						case preg_match('/^\n/', $var) && preg_match('/\n$/', $var):
-						$var = substr($var, 1, -1);
-						break;
+							$var = substr($var, 1, -1);
+							break;
 
 						case preg_match('/^\n/', $var):
-						$var = substr($var, 1);
-						break;
+							$var = substr($var, 1);
+							break;
 
 						case preg_match('/\n$/', $var):
-						$var = substr($var, 0, -1);
-						break;
+							$var = substr($var, 0, -1);
+							break;
 					}
 
 					// SF Bug no. 810773 - thanks anonymous
@@ -462,7 +658,7 @@
 					// Removed requriement for blocks to be at the start of string
 					// -----------------------------------------------------------
 					//                    $copy=preg_replace("|^\s*\{".$v." ?#?".$comments."\}\s*\n|m","",$copy);
-					$copy = preg_replace("|" . $this->tag_start_delim . $v . " ?#?" . $comments . $this->tag_end_delim . "(\n?)+|m", '', $copy);
+					$copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", '', $copy);
 				}
 
 				$var = trim($var);
@@ -473,7 +669,7 @@
 				// Replace str_replaces with preg_quote
 				//$var = preg_quote($var);
 				$var = str_replace('\\|', '|', $var);
-				$copy = preg_replace("|" . $this->tag_start_delim . $v . " ?#?" . $comments . $this->tag_end_delim . "(\n?)+|m", "$var", $copy);
+				$copy = preg_replace("|" . $this->tag_start_delim . $v . "( ?#" . $comments . ")?" . $this->tag_end_delim . "|m", "$var", $copy);
 
 				if (preg_match('/^\n/', $copy) && preg_match('/\n$/', $copy)) {
 					$copy = substr($copy, 1);
@@ -504,7 +700,7 @@
      * @access public
      * @param string $bname Block name to parse
      */
-	function rparse ($bname) {
+	public function rparse ($bname) {
 
 		if (!empty($this->sub_blocks[$bname])) {
 
@@ -529,7 +725,7 @@
      * @param string $var Variable to assign values to
      * @param string / array $value Value to assign to $var
     */
-	function insert_loop ($bname, $var, $value = '') {
+	public function insert_loop ($bname, $var, $value = '') {
 
 		$this->assign($var, $value);
 		$this->parse($bname);
@@ -543,14 +739,13 @@
      * @param string $var Variable to assign values to
      * @param array $values Values to assign to $var
     */
-	function array_loop ($bname, $var, &$values) {
+	public function array_loop ($bname, $var, &$values) {
 
 		if (is_array($values)) {
 
 			foreach($values as $v) {
 
-				$this->assign($var, $v);
-				$this->parse($bname);
+				$this->insert_loop($bname, $var, $v);
 			}
 		}
 	}
@@ -562,16 +757,16 @@
      * @param string $bname Block name to return
      * @return string
      */
-	function text ($bname = '') {
+	public function text ($bname = '') {
 
-		// JC 20/11/02 moved from ::out()
 		$text = '';
-		/*if (SYSTEM_TYPE == 'development' && $this->output_type == "HTML") {
-		$Text = "<!-- Template: " . $this->_file_name_full_path . " -->\n";
-		} else {
-		$Text = "";
-		}*/
 
+		if ($this->debug && $this->output_type == 'HTML') {
+			// JC 20/11/02 echo the template filename if in development as
+			// html comment
+			$text .= '<!-- XTemplate: ' . realpath($this->filename) . " -->\n";
+		}
+
 		$bname = !empty($bname) ? $bname : $this->mainblock;
 
 		$text .= isset($this->parsed_blocks[$bname]) ? $this->parsed_blocks[$bname] : $this->get_error();
@@ -585,21 +780,12 @@
      * @access public
      * @param string $bname Block name to echo out
      */
-	function out ($bname) {
+	public function out ($bname) {
 
 		$out = $this->text($bname);
 		//        $length=strlen($out);
 		//header("Content-Length: ".$length); // TODO: Comment this back in later
 
-		// JC 20/11/02 echo the template filename if in development as
-		// html comment
-		// note 4.3.0 and ZE2 have new function debug_backtrace() that show a
-		// function call list - it may be nice to dump that here too
-		//if (SYSTEM_TYPE == 'development') {
-		//    echo "<!-- Template: " . $this->_file_name_full_path . " -->\n";
-		//}
-		// moved to ::text() so parsing sub templates work
-
 		echo $out;
 	}
 
@@ -610,7 +796,7 @@
      * @param string $bname Block name to write out
      * @param string $fname File name to write to
      */
-	function out_file ($bname, $fname) {
+	public function out_file ($bname, $fname) {
 
 		if (!empty($bname) && !empty($fname) && is_writeable($fname)) {
 
@@ -626,7 +812,7 @@
      * @access public
      * @param string $bname Block to reset
      */
-	function reset ($bname) {
+	public function reset ($bname) {
 
 		$this->parsed_blocks[$bname] = '';
 	}
@@ -638,7 +824,7 @@
      * @param string $bname Block name to test
      * @return boolean
      */
-	function parsed ($bname) {
+	public function parsed ($bname) {
 
 		return (!empty($this->parsed_blocks[$bname]));
 	}
@@ -650,31 +836,53 @@
      * @param string $str Display string for null block
      * @param string $varname Variable name to apply $str to
      */
-	function SetNullString ($str, $varname = '') {
+	public function set_null_string($str, $varname = '') {
 
 		$this->_null_string[$varname] = $str;
 	}
 
 	/**
+	 * Backwards compatibility only
+	 *
+	 * @param string $str
+	 * @param string $varname
+	 * @deprecated Change to set_null_string to keep in with rest of naming convention
+	 */
+	public function SetNullString ($str, $varname = '') {
+		$this->set_null_string($str, $varname);
+	}
+
+	/**
      * sets the string to replace in case the block was not parsed
      *
      * @access public
      * @param string $str Display string for null block
      * @param string $bname Block name to apply $str to
      */
-	function SetNullBlock ($str, $bname = '') {
+	public function set_null_block ($str, $bname = '') {
 
 		$this->_null_block[$bname] = $str;
 	}
 
 	/**
+	 * Backwards compatibility only
+	 *
+	 * @param string $str
+	 * @param string $bname
+	 * @deprecated Change to set_null_block to keep in with rest of naming convention
+	 */
+	public function SetNullBlock ($str, $bname = '') {
+		$this->set_null_block($str, $bname);
+	}
+
+	/**
      * sets AUTORESET to 1. (default is 1)
      * if set to 1, parse() automatically resets the parsed blocks' sub blocks
      * (for multiple level blocks)
      *
      * @access public
      */
-	function set_autoreset () {
+	public function set_autoreset () {
 
 		$this->_autoreset = true;
 	}
@@ -686,7 +894,7 @@
      *
      * @access public
      */
-	function clear_autoreset () {
+	public function clear_autoreset () {
 
 		$this->_autoreset = false;
 	}
@@ -696,7 +904,7 @@
      *
      * @access public
      */
-	function scan_globals () {
+	public function scan_globals () {
 
 		reset($GLOBALS);
 
@@ -704,7 +912,12 @@
 			$GLOB[$k] = $v;
 		}
 
-		$this->assign('PHP', $GLOB); /* access global variables as {PHP.HTTP_SERVER_VARS.HTTP_HOST} in your template! */
+		/**
+		 * Access global variables as:
+		 * @example {PHP._SERVER.HTTP_HOST}
+		 * in your template!
+		 */
+		$this->assign('PHP', $GLOB);
 	}
 
 	/**
@@ -713,7 +926,7 @@
      * @access public
      * @return boolean / string
      */
-	function get_error () {
+	public function get_error () {
 
 		// JRC: 3/1/2003 Added ouptut wrapper and detection of output type for error message output
 		$retval = false;
@@ -723,12 +936,12 @@
 			switch ($this->output_type) {
 				case 'HTML':
 				case 'html':
-				$retval = '<b>[XTemplate]</b><ul>' . nl2br(str_replace('* ', '<li>', str_replace(" *\n", "</li>\n", $this->_error))) . '</ul>';
-				break;
+					$retval = '<b>[XTemplate]</b><ul>' . nl2br(str_replace('* ', '<li>', str_replace(" *\n", "</li>\n", $this->_error))) . '</ul>';
+					break;
 
 				default:
-				$retval = '[XTemplate] ' . str_replace(' *\n', "\n", $this->_error);
-				break;
+					$retval = '[XTemplate] ' . str_replace(' *\n', "\n", $this->_error);
+					break;
 			}
 		}
 
@@ -740,13 +953,15 @@
 	/***************************************************************************/
 
 	/**
-     * generates the array containing to-be-parsed stuff: $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc. also builds the reverse parse order.
+     * generates the array containing to-be-parsed stuff:
+     * $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc.
+     * also builds the reverse parse order.
      *
-     * @access private
+     * @access public - aiming for private
      * @param string $con content to be processed
      * @param string $parentblock name of the parent block in the block hierarchy
      */
-	function _maketree ($con, $parentblock='') {
+	public function _maketree ($con, $parentblock='') {
 
 		$blocks = array();
 
@@ -763,21 +978,23 @@
 			$level = 0;
 		}
 
+		// JRC 06/04/2005 Added block comments (on BEGIN or END) <!-- BEGIN: block_name#Comments placed here -->
+		//$patt = "($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)";
+		$patt = "(" . $this->block_start_word . "|" . $this->block_end_word . ")\s*(\w+)" . $this->comment_preg . "\s*" . $this->block_end_delim . "(.*)";
+
 		foreach($con2 as $k => $v) {
 
-			// JRC 06/04/2005 Added block comments (on BEGIN or END) <!-- BEGIN: block_name#Comments placed here -->
-			//$patt = "($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)";
-			$patt = "($this->block_start_word|$this->block_end_word)\s*(\w+) ?#?.*?\s*$this->block_end_delim(.*)";
-
 			$res = array();
 
 			if (preg_match_all("/$patt/ims", $v, $res, PREG_SET_ORDER)) {
 				// $res[0][1] = BEGIN or END
 				// $res[0][2] = block name
-				// $res[0][3] = kinda content
+				// $res[0][3] = comment
+				// $res[0][4] = kinda content
 				$block_word	= $res[0][1];
 				$block_name	= $res[0][2];
-				$content	= $res[0][3];
+				$comment	= $res[0][3];
+				$content	= $res[0][4];
 
 				if (strtoupper($block_word) == $this->block_start_word) {
 
@@ -811,7 +1028,7 @@
 					$parent_name = implode('.', $block_names);
 
 					// add rest of block to parent block
-					$blocks[$parent_name] .= $res[0][3];
+					$blocks[$parent_name] .= $content;
 				}
 			} else {
 
@@ -834,10 +1051,11 @@
 	/**
      * Sub processing for assign_file method
      *
+     * @access private
      * @param string $name
      * @param string $val
      */
-	function _assign_file_sub ($name, $val) {
+	private function _assign_file_sub ($name, $val) {
 
 		if (isset($this->filevar_parent[$name])) {
 
@@ -862,11 +1080,17 @@
 
 					if (is_array($res) && isset($res[0])) {
 
-						foreach ($res[0] as $v) {
+						// Changed as per solution in SF bug ID #1261828
+						foreach ($res as $v) {
 
-							$copy = preg_replace("/" . preg_quote($v) . "/", "$val", $copy);
-							$this->preparsed_blocks = array_merge($this->preparsed_blocks, $this->_maketree($copy, $parent));
-							$this->filevar_parent = array_merge($this->filevar_parent, $this->_store_filevar_parents($this->preparsed_blocks));
+							// Changed as per solution in SF bug ID #1261828
+							if ($v[1] == $name) {
+
+								// Changed as per solution in SF bug ID #1261828
+								$copy = preg_replace("/" . preg_quote($v[0]) . "/", "$val", $copy);
+								$this->preparsed_blocks = array_merge($this->preparsed_blocks, $this->_maketree($copy, $parent));
+								$this->filevar_parent = array_merge($this->filevar_parent, $this->_store_filevar_parents($this->preparsed_blocks));
+							}
 						}
 					}
 				}
@@ -879,11 +1103,11 @@
 	/**
      * store container block's name for file variables
      *
-     * @access private
+     * @access public - aiming for private
      * @param array $blocks
      * @return array
      */
-	function _store_filevar_parents ($blocks){
+	public function _store_filevar_parents ($blocks){
 
 		$parents = array();
 
@@ -904,11 +1128,11 @@
 	/**
      * Set the error string
      *
+     * @access private
      * @param string $str
      */
-	function _set_error ($str)    {
+	private function _set_error ($str)    {
 
-		//$this->_error="<b>[XTemplate]</b>&nbsp;<i>".$str."</i>";
 		// JRC: 3/1/2003 Made to append the error messages
 		$this->_error .= '* ' . $str . " *\n";
 		// JRC: 3/1/2003 Removed trigger error, use this externally if you want it eg. trigger_error($xtpl->get_error())
@@ -918,11 +1142,11 @@
 	/**
      * returns the contents of a file
      *
-     * @access private
+     * @access protected
      * @param string $file
      * @return string
      */
-	function _getfile ($file) {
+	protected function _getfile ($file) {
 
 		if (!isset($file)) {
 			// JC 19/12/02 added $file to error message
@@ -943,15 +1167,19 @@
 		// prepend template dir
 		if (!empty($this->tpldir)) {
 
-			$file = $this->tpldir. '/' . $file;
+			$file = $this->tpldir. DIRECTORY_SEPARATOR . $file;
 		}
 
 		$file_text = '';
 
 		if (isset($this->filecache[$file])) {
 
-			$file_text=$this->filecache[$file];
+			$file_text .= $this->filecache[$file];
 
+			if ($this->debug) {
+				$file_text = '<!-- XTemplate debug cached: ' . realpath($file) . ' -->' . "\n" . $file_text;
+			}
+
 		} else {
 
 			if (is_file($file) && is_readable($file)) {
@@ -960,19 +1188,38 @@
 
 					if (!($fh = fopen($file, 'r'))) {
 
-						$this->_set_error('Cannot open file: ' . $file);
+						$this->_set_error('Cannot open file: ' . realpath($file));
 						return '';
 					}
 
-					$file_text = fread($fh,filesize($file));
+					$file_text .= fread($fh,filesize($file));
 					fclose($fh);
 
 				}
-			} else {
 
-				// NW 17Oct 2002 : Added realpath around the file name to identify where the code is searching.
+				if ($this->debug) {
+					$file_text = '<!-- XTemplate debug: ' . realpath($file) . ' -->' . "\n" . $file_text;
+				}
+
+			} elseif (str_replace('.', '', phpversion()) >= '430' && $file_text = @file_get_contents($file, true)) {
+				// Enable use of include path by using file_get_contents
+				// Implemented at suggestion of SF Feature Request ID #1529478 michaelgroh
+				if ($file_text === false) {
+					$this->_set_error("[" . realpath($file) . "] ($file) does not exist");
+					$file_text = "<b>__XTemplate fatal error: file [$file] does not exist in the include path__</b>";
+				} elseif ($this->debug) {
+					$file_text = '<!-- XTemplate debug: ' . realpath($file) . ' (via include path) -->' . "\n" . $file_text;
+				}
+			} elseif (!is_file($file)) {
+
+				// NW 17 Oct 2002 : Added realpath around the file name to identify where the code is searching.
 				$this->_set_error("[" . realpath($file) . "] ($file) does not exist");
-				$file_text = "<b>__XTemplate fatal error: file [$file] does not exist__</b>";
+				$file_text .= "<b>__XTemplate fatal error: file [$file] does not exist__</b>";
+
+			} elseif (!is_readable($file)) {
+
+				$this->_set_error("[" . realpath($file) . "] ($file) is not readable");
+				$file_text .= "<b>__XTemplate fatal error: file [$file] is not readable__</b>";
 			}
 
 			$this->filecache[$file] = $file_text;
@@ -984,11 +1231,11 @@
 	/**
      * recursively gets the content of a file with {FILE "filename.tpl"} directives
      *
-     * @access private
+     * @access public - aiming for private
      * @param string $file
      * @return string
      */
-	function _r_getfile ($file) {
+	public function _r_getfile ($file) {
 
 		$text = $this->_getfile($file);
 
@@ -1009,7 +1256,7 @@
      *
      * @access private
      */
-	function _add_outer_block () {
+	private function _add_outer_block () {
 
 		$before = $this->block_start_delim . $this->block_start_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
 		$after = $this->block_start_delim . $this->block_end_word . ' ' . $this->mainblock . ' ' . $this->block_end_delim;
@@ -1021,58 +1268,16 @@
      * Debug function - var_dump wrapped in '<pre></pre>' tags
      *
      * @access private
-     * @param multiple Var_dumps all the supplied arguments
+     * @param multiple var_dumps all the supplied arguments
      */
-	function _pre_var_dump ($args) {
+	private function _pre_var_dump ($args) {
 
-		echo '<pre>';
-		var_dump(func_get_args());
-		echo '</pre>';
+		if ($this->debug) {
+			echo '<pre>';
+			var_dump(func_get_args());
+			echo '</pre>';
+		}
 	}
 } /* end of XTemplate class. */
 
-/* Stuff from development outside sourceforge
-
-// Revision 1.2  2003/12/05 22:22:17  jeremy
-// Removed duplicate function call in out method
-//
-// Revision 1.1.1.1  2003/10/29 20:22:43  jeremy
-// Initial Import
-//
-// Revision 1.1  2003/06/25 17:17:52  jeremy
-// Initial Import
-//
-// Revision 1.4  2001/08/17 18:25:45  jeremy
-// Sorted greedy matching regular expression in parse function preg_match_all line 166: added ? after .* when looking for comments
-//
-*/
-/* Old log stuff
-
-Revision 1.2  2001/09/19 14:11:25  cranx
-fixed a bug in the whitespace-stripping block variable interpolating regexp.
-
-Revision 1.1  2001/07/11 10:42:39  cranx
-added:
-- filename substitution, no nested arrays for the moment, sorry
-(including happens when assigning, so assign filevar in the outside blocks first!)
-
-Revision 1.5  2001/07/11 10:39:08  cranx
-added:
-- we can now specify base dir
-- array_loop()
-- trigger_error in _set_error
-
-modified:
-- newline bugs fixed (for XML)
-- in out(): content-length header added
-- whiles changed to foreach
-- from now on, the class is php4 only :P
-
-*/
-/* Old stuff from original releases
-
-xtemplate class 0.3pre
-!!! {FILE {VAR}} file variable interpolation may still be buggy !!!
-*/
-
 ?>
\ No newline at end of file


Property changes on: trunk/xtemplate.class.php
___________________________________________________________________
Name: svn:keywords
   - Author Date Id Revision
   + Author Date HeadURL Id Revision
Name: svn:mime-type
   + text/plain


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


More information about the Xtpl-svn mailing list