3 * Project: template_lite, a smarter template engine
4 * File: class.template.php
5 * Author: Paul Lockaby <paul@paullockaby.com>, Mark Dickenson <akapanamajack@sourceforge.net>
6 * Copyright: 2003,2004,2005 by Paul Lockaby, 2005,2006 Mark Dickenson
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * The latest version of template_lite can be obtained from:
23 * http://templatelite.sourceforge.net
27 if (!defined('TEMPLATE_LITE_DIR')) {
28 define('TEMPLATE_LITE_DIR', dirname(__FILE__
) . DIRECTORY_SEPARATOR
);
31 class Template_Exception
extends RuntimeException
33 public function __construct($message, &$tpl = null)
35 if (!is_null($tpl) && is_object($tpl))
37 $message = '[in ' . $tpl->_file
. ' line ' . $tpl->_linenum
. '] ' . $message;
40 parent
::__construct($message);
45 // public configuration variables
46 var $left_delimiter = "{"; // the left delimiter for template tags
47 var $right_delimiter = "}"; // the right delimiter for template tags
48 var $cache = false; // whether or not to allow caching of files
49 var $force_compile = false; // force a compile regardless of saved state
50 var $template_dir = "templates"; // where the templates are to be found
51 var $plugins_dir = array("plugins"); // where the plugins are to be found
52 var $compile_dir = "compiled"; // the directory to store the compiled files in
53 var $config_dir = "templates"; // where the config files are
54 var $cache_dir = "cached"; // where cache files are stored
55 var $config_overwrite = false;
56 var $config_booleanize = true;
57 var $config_fix_new_lines = true;
58 var $config_read_hidden = true;
59 var $cache_lifetime = 0; // how long the file in cache should be considered "fresh"
60 var $encode_file_name = true; // Set this to false if you do not want the name of the compiled/cached file to be md5 encoded.
61 var $php_extract_vars = false; // Set this to true if you want the $this->_tpl variables to be extracted for use by PHP code inside the template.
62 var $reserved_template_varname = "templatelite";
63 var $default_modifiers = array();
64 var $debugging = false;
66 var $compiler_file = 'class.compiler.php';
67 var $compiler_class = 'Template_Lite_Compiler';
68 var $config_class = 'config';
70 // gzip output configuration
72 var $force_compression = 0;
73 var $compression_level = 9;
76 // private internal variables
77 var $_vars = array(); // stores all internal assigned variables
78 var $_confs = array(); // stores all internal config variables
79 var $_plugins = array( 'modifier' => array(),
80 'function' => array(),
82 'compiler' => array(),
83 'resource' => array(),
84 'prefilter' => array(),
85 'postfilter' => array(),
86 'outputfilter' => array());
87 var $_linenum = 0; // the current line number in the file we are processing
88 var $_file = ""; // the current file we are processing
89 var $_config_obj = null;
90 var $_compile_obj = null;
91 var $_cache_id = null;
92 var $_cache_dir = ""; // stores where this specific file is going to be cached
93 var $_cache_info = array('config' => array(), 'template' => array());
94 var $_sl_md5 = '39fc70570b8b60cbc1b85839bf242aff';
95 var $_version = 'V2.10 Template Lite 4 January 2007 (c) 2005-2007 Mark Dickenson. All rights reserved. Released LGPL.';
96 var $_version_date = "2007-01-04 10:34:21";
97 var $_config_module_loaded = false;
98 var $_templatelite_debug_info = array();
99 var $_templatelite_debug_loop = false;
100 var $_templatelite_debug_dir = "";
101 var $_inclusion_depth = 0;
103 var $_resource_type = 1;
105 var $_sections = array();
106 var $_foreach = array();
108 function Template_Lite()
110 $this->_version_date
= strtotime($this->_version_date
);
111 $this->_base_plugins
= $this->_plugins
;
114 function load_filter($type, $name)
119 include_once( $this->_get_plugin_dir($type . "filter." . $name . ".php") . $type . "filter." . $name . ".php");
120 $this->_plugins
['outputfilter'][$name] = "template_" . $type . "filter_" . $name;
124 if (!isset($this->_plugins
[$type . 'filter'][$name]))
126 $this->_plugins
[$type . 'filter'][$name] = "template_" . $type . "filter_" . $name;
132 function assign($key, $value = null)
136 foreach($key as $var => $val)
139 $this->_vars
[$var] = $val;
146 $this->_vars
[$key] = $value;
151 function assign_by_ref($key, $value = null)
155 $this->_vars
[$key] = &$value;
159 function assign_config($key, $value = null)
163 foreach($key as $var => $val)
167 $this->_confs
[$var] = $val;
175 $this->_confs
[$key] = $value;
180 function append($key, $value=null, $merge=false)
184 foreach ($key as $_key => $_value)
188 if(!@is_array
($this->_vars
[$_key]))
190 settype($this->_vars
[$_key],'array');
192 if($merge && is_array($_value))
194 foreach($_value as $_mergekey => $_mergevalue)
196 $this->_vars
[$_key][$_mergekey] = $_mergevalue;
201 $this->_vars
[$_key][] = $_value;
208 if ($key != '' && isset($value))
210 if(!@is_array
($this->_vars
[$key]))
212 settype($this->_vars
[$key],'array');
214 if($merge && is_array($value))
216 foreach($value as $_mergekey => $_mergevalue)
218 $this->_vars
[$key][$_mergekey] = $_mergevalue;
223 $this->_vars
[$key][] = $value;
229 function append_by_ref($key, &$value, $merge=false)
231 if ($key != '' && isset($value))
233 if(!@is_array
($this->_vars
[$key]))
235 settype($this->_vars
[$key],'array');
237 if ($merge && is_array($value))
239 foreach($value as $_key => $_val)
241 $this->_vars
[$key][$_key] = &$value[$_key];
246 $this->_vars
[$key][] = &$value;
251 function clear_assign($key = null)
255 $this->_vars
= array();
261 foreach($key as $index => $value)
263 if (in_array($value, $this->_vars
))
265 unset($this->_vars
[$index]);
271 if (in_array($key, $this->_vars
))
273 unset($this->_vars
[$index]);
279 function clear_all_assign()
281 $this->_vars
= array();
284 function clear_config($key = null)
288 $this->_conf
= array();
294 foreach($key as $index => $value)
296 if (in_array($value, $this->_conf
))
298 unset($this->_conf
[$index]);
304 if (in_array($key, $this->_conf
))
306 unset($this->_conf
[$key]);
312 function &get_template_vars($key = null)
320 if (isset($this->_vars
[$key]))
322 return $this->_vars
[$key];
331 function &get_config_vars($key = null)
335 return $this->_confs
;
339 if (isset($this->_confs
[$key]))
341 return $this->_confs
[$key];
350 function clear_compiled_tpl($file = null)
352 $this->_destroy_dir($file, null, $this->_get_dir($this->compile_dir
));
355 function clear_cache($file = null, $cache_id = null, $compile_id = null, $exp_time = null)
357 $this->_destroy_dir($file, $cache_id, $this->_get_dir($this->cache_dir
));
360 function clear_all_cache($exp_time = null)
362 $this->clear_cache();
365 function is_cached($file, $cache_id = null)
367 if (!$this->force_compile
&& $this->_is_cached($file, $cache_id))
377 // Warning : don't use $base, it's a temporary fix because plugin management is very crappy actually
378 function register_modifier($modifier, $implementation, $base=true)
380 if (!is_callable($implementation))
381 throw new Template_Exception("'$implementation' modifier doesn't seem to be a valid callback");
383 $this->_plugins
['modifier'][$modifier] = $implementation;
385 $this->_base_plugins
['modifier'][$modifier] = $implementation;
388 function unregister_modifier($modifier)
390 unset($this->_plugins
['modifier'][$modifier]);
393 function register_function($function, $implementation, $base=true)
395 if (!is_callable($implementation))
396 throw new Template_Exception("Function '$function' doesn't seem to be a valid callback");
398 // Non-static object callbacks are not supported now
399 if (is_array($implementation) && !is_string($function[0]))
401 throw new Template_Exception("Unsupported object callback for function '$function'");
404 if (is_object($implementation))
406 throw new Template_Exception("Closures are not supported for functions.");
409 $this->_plugins
['function'][$function] = $implementation;
411 $this->_base_plugins
['function'][$function] = $implementation;
414 function unregister_function($function)
416 unset($this->_plugins
['function'][$function]);
419 function register_block($function, $implementation, $base=true)
421 if (!is_callable($implementation))
422 throw new Template_Exception("'$implementation' block function doesn't seem to be a valid callback");
424 $this->_plugins
['block'][$function] = $implementation;
426 $this->_base_plugins
['block'][$function] = $implementation;
429 function unregister_block($function)
431 unset($this->_plugins
['block'][$function]);
434 function register_compiler($function, $implementation, $base=true)
436 if (!is_callable($implementation))
437 throw new Template_Exception("'$implementation' compiler function doesn't seem to be a valid callback");
439 $this->_plugins
['compiler'][$function] = $implementation;
441 $this->_base_plugins
['compiler'][$function] = $implementation;
444 function unregister_compiler($function)
446 unset($this->_plugins
['compiler'][$function]);
449 function register_prefilter($function)
451 $_name = (is_array($function)) ?
$function[1] : $function;
452 $this->_plugins
['prefilter'][$_name] = $_name;
455 function unregister_prefilter($function)
457 unset($this->_plugins
['prefilter'][$function]);
460 function register_postfilter($function)
462 $_name = (is_array($function)) ?
$function[1] : $function;
463 $this->_plugins
['postfilter'][$_name] = $_name;
466 function unregister_postfilter($function)
468 unset($this->_plugins
['postfilter'][$function]);
471 function register_outputfilter($function)
473 $_name = (is_array($function)) ?
$function[1] : $function;
474 $this->_plugins
['outputfilter'][$_name] = $_name;
477 function unregister_outputfilter($function)
479 unset($this->_plugins
['outputfilter'][$function]);
482 function register_resource($type, $functions)
484 if (count($functions) == 4)
486 $this->_plugins
['resource'][$type] = $functions;
490 throw new Template_Exception("malformed function-list for '$type' in register_resource", $this);
494 function unregister_resource($type)
496 unset($this->_plugins
['resource'][$type]);
499 function template_exists($file)
501 if (file_exists($this->_get_dir($this->template_dir
).$file))
503 $this->_resource_time
= filemtime($this->_get_dir($this->template_dir
).$file);
504 $this->_resource_type
= 1;
509 if (file_exists($file))
511 $this->_resource_time
= filemtime($file);
512 $this->_resource_type
= "file";
519 function _get_resource($file)
521 $_resource_name = explode(':', trim($file));
523 if (count($_resource_name) == 1 ||
$_resource_name[0] == "file" ||
$_resource_name[0] == 'phar')
525 if($_resource_name[0] == "file")
527 $file = substr($file, 5);
530 $exists = $this->template_exists($file);
534 throw new Template_Exception("file '$file' does not exist", $this);
539 $this->_resource_type
= $_resource_name[0];
540 $file = substr($file, strlen($this->_resource_type
) +
1);
541 $exists = isset($this->_plugins
['resource'][$this->_resource_type
]) && call_user_func_array($this->_plugins
['resource'][$this->_resource_type
][1], array($file, &$resource_timestamp, &$this));
545 throw new Template_Exception("file '$file' does not exist", $this);
547 $this->_resource_time
= $resource_timestamp;
552 function display($file, $cache_id = null)
554 $this->fetch($file, $cache_id, true);
557 function fetch($file, $cache_id = null, $display = false)
559 $file = $this->_get_resource($file);
561 if ($this->debugging
)
563 $this->_templatelite_debug_info
[] = array('type' => 'template',
566 'exec_time' => array_sum(explode(' ', microtime())) );
567 $included_tpls_idx = count($this->_templatelite_debug_info
) - 1;
570 $this->_cache_id
= $cache_id;
571 $this->template_dir
= $this->_get_dir($this->template_dir
);
572 $this->compile_dir
= $this->_get_dir($this->compile_dir
);
575 $this->_cache_dir
= $this->_build_dir($this->cache_dir
, $this->_cache_id
);
578 $name = ($this->encode_file_name
) ?
md5((($this->_resource_type
== 1) ?
$this->template_dir
.$file : $this->_resource_type
. "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type
. "_" . $file)).'.php';
580 $this->_error_level
= $this->debugging ?
error_reporting() : error_reporting(error_reporting() & ~E_NOTICE
);
581 // $this->_error_level = error_reporting(E_ALL);
583 if (!$this->force_compile
&& $this->cache
&& $this->_is_cached($file, $cache_id))
586 include($this->_cache_dir
.$name);
587 $output = ob_get_contents();
589 $output = substr($output, strpos($output, "\n") +
1);
594 $output = $this->_fetch_compile($file);
598 $f = fopen($this->_cache_dir
.$name, "w");
599 fwrite($f, serialize($this->_cache_info
) . "\n" . str_replace('<?xml', "<?php echo '<?xml'; ?>", $output));
604 if (strpos($output, $this->_sl_md5
) !== false)
606 preg_match_all('!' . $this->_sl_md5
. '{_run_insert (.*)}' . $this->_sl_md5
. '!U',$output,$_match);
607 foreach($_match[1] as $value)
609 $arguments = unserialize($value);
610 $output = str_replace($this->_sl_md5
. '{_run_insert ' . $value . '}' . $this->_sl_md5
, call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)), $output);
614 foreach ($this->_plugins
['outputfilter'] as $function)
616 $output = $function($output, $this);
619 error_reporting($this->_error_level
);
621 if ($this->debugging
)
623 $this->_templatelite_debug_info
[$included_tpls_idx]['exec_time'] = array_sum(explode(' ', microtime())) - $this->_templatelite_debug_info
[$included_tpls_idx]['exec_time'];
629 if($this->debugging
&& !$this->_templatelite_debug_loop
)
631 $this->debugging
= false;
632 if(!function_exists("template_generate_debug_output"))
634 require_once(TEMPLATE_LITE_DIR
. "internal/template.generate_debug_output.php");
636 $debug_output = template_generate_debug_output($this);
637 $this->debugging
= true;
647 function config_load($file, $section_name = null, $var_name = null)
649 require_once(TEMPLATE_LITE_DIR
. "internal/template.config_loader.php");
652 function _is_cached($file, $cache_id)
654 $this->_cache_dir
= $this->_get_dir($this->cache_dir
, $cache_id);
655 $this->config_dir
= $this->_get_dir($this->config_dir
);
656 $this->template_dir
= $this->_get_dir($this->template_dir
);
658 $file = $this->_get_resource($file);
660 $name = ($this->encode_file_name
) ?
md5((($this->_resource_type
== 1) ?
$this->template_dir
.$file : $this->_resource_type
. "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type
. "_" . $file)).'.php';
662 if (file_exists($this->_cache_dir
.$name) && (((time() - filemtime($this->_cache_dir
.$name)) < $this->cache_lifetime
) ||
$this->cache_lifetime
== -1) && (filemtime($this->_cache_dir
.$name) > $this->_resource_time
))
664 $fh = fopen($this->_cache_dir
.$name, "r");
665 if (!feof($fh) && ($line = fgets($fh, filesize($this->_cache_dir
.$name))))
667 $includes = unserialize($line);
668 if (isset($includes['template']))
670 foreach($includes['template'] as $value)
672 if (!(file_exists($this->template_dir
.$value) && (filemtime($this->_cache_dir
.$name) > filemtime($this->template_dir
.$value))))
678 if (isset($includes['config']))
680 foreach($includes['config'] as $value)
682 if (!(file_exists($this->config_dir
.$value) && (filemtime($this->_cache_dir
.$name) > filemtime($this->config_dir
.$value))))
698 function _fetch_compile_include($_templatelite_include_file, $_templatelite_include_vars)
700 if(!function_exists("template_fetch_compile_include"))
702 require_once(TEMPLATE_LITE_DIR
. "internal/template.fetch_compile_include.php");
704 return template_fetch_compile_include($_templatelite_include_file, $_templatelite_include_vars, $this);
707 function _fetch_compile($file, $include=false)
709 $this->template_dir
= $this->_get_dir($this->template_dir
);
711 $name = ($this->encode_file_name
) ?
md5((($this->_resource_type
== 1) ?
$this->template_dir
.$file : $this->_resource_type
. "_" . $file)).'.php' : str_replace(".", "_", str_replace("/", "_", $this->_resource_type
. "_" . $file)).'.php';
715 array_push($this->_cache_info
['template'], $file);
718 if (!$this->force_compile
&& file_exists($this->compile_dir
.'c_'.$name)
719 && (filemtime($this->compile_dir
.'c_'.$name) > $this->_resource_time
)
720 && (filemtime($this->compile_dir
.'c_'.$name) > $this->_version_date
))
723 include($this->compile_dir
.'c_'.$name);
724 $output = ob_get_contents();
726 error_reporting($this->_error_level
);
731 if($this->_resource_type
== 1)
733 $f = fopen($this->template_dir
. $file, "r");
734 $size = filesize($this->template_dir
. $file);
737 $file_contents = fread($f, $size);
741 if($this->_resource_type
== "file")
743 $f = fopen($file, "r");
744 $size = filesize($file);
747 $file_contents = fread($f, $size);
752 call_user_func_array($this->_plugins
['resource'][$this->_resource_type
][0], array($file, &$file_contents, &$this));
755 $this->_file
= $file;
758 if (!is_object($this->_compile_obj
))
760 if (file_exists(TEMPLATE_LITE_DIR
. $this->compiler_file
)) {
761 require_once(TEMPLATE_LITE_DIR
. $this->compiler_file
);
763 require_once($this->compiler_file
);
765 $this->_compile_obj
= new $this->compiler_class
;
767 $this->_compile_obj
->left_delimiter
= $this->left_delimiter
;
768 $this->_compile_obj
->right_delimiter
= $this->right_delimiter
;
769 $this->_compile_obj
->plugins_dir
= &$this->plugins_dir
;
770 $this->_compile_obj
->template_dir
= &$this->template_dir
;
771 $this->_compile_obj
->_vars
= &$this->_vars
;
772 $this->_compile_obj
->_confs
= &$this->_confs
;
773 $this->_compile_obj
->_linenum
= &$this->_linenum
;
774 $this->_compile_obj
->_file
= &$this->_file
;
775 $this->_compile_obj
->php_extract_vars
= &$this->php_extract_vars
;
776 $this->_compile_obj
->reserved_template_varname
= &$this->reserved_template_varname
;
777 $this->_compile_obj
->default_modifiers
= $this->default_modifiers
;
779 // FIXME: the is a lot of bugs with _plugins because it's crappy
780 // _plugins is used to register plugins, and that's cool, but register_plugins is used also
781 // in compiled templates, so it creates a lot of bugs, we'll have to rewrite this later,
782 // but for now the most simple patch is to use a new thing : _base_plugins
783 $this->_compile_obj
->_plugins
= $this->_base_plugins
;
785 $output = $this->_compile_obj
->_compile_file($file_contents);
787 $f = fopen($this->compile_dir
.'c_'.$name, "w");
792 eval(' ?>' . $output . '<?php ');
793 $output = ob_get_contents();
796 // We're putting back used plugins before the inclusion
797 if ($include && isset($old_plugins))
799 $this->_plugins
= $old_plugins;
800 $this->_compile_obj
->_plugins
= &$this->_plugins
;
806 function _run_modifier()
808 $arguments = func_get_args();
809 list($variable, $modifier, $php_function, $_map_array) = array_splice($arguments, 0, 4);
810 array_unshift($arguments, $variable);
811 if ($_map_array && is_array($variable))
813 foreach($variable as $key => $value)
815 if($php_function == "PHP")
817 $variable[$key] = call_user_func_array($modifier, $arguments);
821 $variable[$key] = call_user_func_array($this->_plugins
["modifier"][$modifier], $arguments);
827 if($php_function == "PHP")
829 $variable = call_user_func_array($modifier, $arguments);
833 $variable = call_user_func_array($this->_plugins
["modifier"][$modifier], $arguments);
840 function _run_insert($arguments)
844 return $this->_sl_md5
. '{_run_insert ' . serialize((array)$arguments) . '}' . $this->_sl_md5
;
848 if (!function_exists('insert_' . $arguments['name']))
850 throw new Template_Exception("function 'insert_" . $arguments['name'] . "' does not exist in 'insert'", $this);
852 if (isset($arguments['assign']))
854 $this->assign($arguments['assign'], call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this)));
858 return call_user_func_array('insert_' . $arguments['name'], array((array)$arguments, $this));
863 function _get_dir($dir, $id = null)
869 if (substr($dir, -1) != DIRECTORY_SEPARATOR
)
871 $dir .= DIRECTORY_SEPARATOR
;
875 $_args = explode('|', $id);
876 if (count($_args) == 1 && empty($_args[0]))
880 foreach($_args as $value)
882 $dir .= $value.DIRECTORY_SEPARATOR
;
888 function _get_plugin_dir($plugin_name)
890 static $_path_array = null;
892 $plugin_dir_path = "";
893 $_plugin_dir_list = is_array($this->plugins_dir
) ?
$this->plugins_dir
: (array)$this->plugins_dir
;
894 foreach ($_plugin_dir_list as $_plugin_dir)
896 if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $_plugin_dir))
899 if (file_exists(dirname(__FILE__
) . DIRECTORY_SEPARATOR
. $_plugin_dir . DIRECTORY_SEPARATOR
. $plugin_name))
901 $plugin_dir_path = dirname(__FILE__
) . DIRECTORY_SEPARATOR
. $_plugin_dir . DIRECTORY_SEPARATOR
;
908 if(!isset($_path_array))
910 $_ini_include_path = ini_get('include_path');
912 if(strstr($_ini_include_path,';'))
915 $_path_array = explode(';',$_ini_include_path);
919 $_path_array = explode(':',$_ini_include_path);
923 if(!in_array($_plugin_dir,$_path_array))
925 array_unshift($_path_array,$_plugin_dir);
928 foreach ($_path_array as $_include_path)
930 if (file_exists($_include_path . DIRECTORY_SEPARATOR
. $plugin_name))
932 $plugin_dir_path = $_include_path . DIRECTORY_SEPARATOR
;
938 return $plugin_dir_path;
941 // function _parse_resource_link($resource_link)
943 // $stuffing = "file:/this/is/the/time_5-23.tpl";
944 // $stuffing_data = explode(":", $stuffing);
945 // preg_match_all('/(?:([0-9a-z._-]+))/i', $stuffing, $stuff);
947 // echo "<br>Path: " . str_replace($stuff[0][count($stuff[0]) - 1], "", $stuffing);
948 // echo "<br>Filename: " . $stuff[0][count($stuff[0]) - 1];
951 function _build_dir($dir, $id)
953 if(!function_exists("template_build_dir"))
955 require_once(TEMPLATE_LITE_DIR
. "internal/template.build_dir.php");
957 return template_build_dir($dir, $id, $this);
960 function _destroy_dir($file, $id, $dir)
962 if(!function_exists("template_destroy_dir"))
964 require_once(TEMPLATE_LITE_DIR
. "internal/template.destroy_dir.php");
966 return template_destroy_dir($file, $id, $dir, $this);