From 2d11e1c095b7cf6197b7ce21bde806ad2cc178d8 Mon Sep 17 00:00:00 2001 From: "Marek A. Ruszczynski" Date: Mon, 7 Mar 2011 16:59:41 +0100 Subject: [feature/template-engine] Improved template engine. PHPBB3-9726 --- phpBB/includes/template.php | 287 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 222 insertions(+), 65 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 643a86cde9..47262eaccf 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -16,40 +16,75 @@ if (!defined('IN_PHPBB')) exit; } +/** +* @todo +* IMG_ for image substitution? +* {IMG_[key]:[alt]:[type]} +* {IMG_ICON_CONTACT:CONTACT:full} -> $user->img('icon_contact', 'CONTACT', 'full'); +* +* More in-depth... +* yadayada +*/ + /** * Base Template class. * @package phpBB3 */ -class template +class phpbb_template { - /** variable that holds all the data we'll be substituting into + public $phpbb_required = array('user', 'config'); + public $phpbb_optional = array(); + + /** + * variable that holds all the data we'll be substituting into * the compiled templates. Takes form: * --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value * if it's a root-level variable, it'll be like this: * --> $this->_tpldata[.][0][varname] == value + * @var array + */ + private $_tpldata = array('.' => array(0 => array())); + + /** + * @var array Reference to template->_tpldata['.'][0] + */ + private $_rootref; + + /** + * @var string Root dir for template. */ - var $_tpldata = array('.' => array(0 => array())); - var $_rootref; - - // Root dir and hash of filenames for each template handle. - var $root = ''; - var $cachepath = ''; - var $files = array(); - var $filename = array(); - var $files_inherit = array(); - var $files_template = array(); - var $inherit_root = ''; - var $orig_tpl_storedb; - var $orig_tpl_inherits_id; + private $root = ''; + + /** + * @var string Path of the cache directory for the template + */ + public $cachepath = ''; + + /** + * @var array Hash of handle => file path pairs + */ + public $files = array(); + + /** + * @var array Hash of handle => filename pairs + */ + public $filename = array(); + + public $files_inherit = array(); + public $files_template = array(); + public $inherit_root = ''; + + public $orig_tpl_storedb; + public $orig_tpl_inherits_id; // this will hash handle names to the compiled/uncompiled code for that handle. - var $compiled_code = array(); + public $compiled_code = array(); /** * Set template location * @access public */ - function set_template() + public function set_template() { global $phpbb_root_path, $user; @@ -89,8 +124,11 @@ class template /** * Set custom template location (able to use directory outside of phpBB) * @access public + * @param string $template_path Path to template directory + * @param string $template_name Name of template + * @param string $fallback_template_path Path to fallback template */ - function set_custom_template($template_path, $template_name, $fallback_template_path = false) + public function set_custom_template($template_path, $template_name, $fallback_template_path = false) { global $phpbb_root_path, $user; @@ -131,13 +169,10 @@ class template * Sets the template filenames for handles. $filename_array * should be a hash of handle => filename pairs. * @access public + * @param array $filname_array Should be a hash of handle => filename pairs. */ - function set_filenames($filename_array) + public function set_filenames(array $filename_array) { - if (!is_array($filename_array)) - { - return false; - } foreach ($filename_array as $handle => $filename) { if (empty($filename)) @@ -161,17 +196,26 @@ class template * Destroy template data set * @access public */ - function destroy() + public function destroy() { $this->_tpldata = array('.' => array(0 => array())); $this->_rootref = &$this->_tpldata['.'][0]; } + /** + * destroy method kept for compatibility. + */ + public function __destruct() + { + $this->destroy(); + } + /** * Reset/empty complete block * @access public + * @param string $blockname Name of block to destroy */ - function destroy_block_vars($blockname) + public function destroy_block_vars($blockname) { if (strpos($blockname, '.') !== false) { @@ -200,12 +244,15 @@ class template /** * Display handle * @access public + * @param string $handle Handle to display + * @param bool $include_once Allow multiple inclusions + * @return bool True on success, false on failure */ - function display($handle, $include_once = true) + public function display($handle, $include_once = true) { global $user, $phpbb_hook; - if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) + if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once)) { if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__))) { @@ -221,13 +268,23 @@ class template } } - if ($filename = $this->_tpl_load($handle)) + $_tpldata = &$this->_tpldata; + $_rootref = &$this->_rootref; + $_lang = &$user->lang; + + if (($filename = $this->_tpl_load($handle)) !== false) { ($include_once) ? include_once($filename) : include($filename); } + else if (($code = $this->_tpl_eval($handle)) !== false) + { + $code = ' ?> ' . $code . ' ' . $this->compiled_code[$handle] . 'display($handle, $include_once); @@ -256,8 +318,11 @@ class template /** * Load a compiled template if possible, if not, recompile it * @access private + * @param string $handle Handle of the template to load + * @return string|bool Return filename on success otherwise false + * @uses template_compile is used to compile uncached templates */ - function _tpl_load(&$handle) + private function _tpl_load($handle) { global $user, $phpEx, $config; @@ -276,7 +341,9 @@ class template $this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0; $recompile = false; - if (!file_exists($filename) || @filesize($filename) === 0) + $recompile = (!file_exists($filename) || @filesize($filename) === 0 || ($config['load_tplcompile'] && @filemtime($filename) < @filemtime($this->files[$handle]))) ? true : false; + + if (defined('DEBUG_EXTRA')) { $recompile = true; } @@ -288,7 +355,7 @@ class template $this->files[$handle] = $this->files_inherit[$handle]; $this->files_template[$handle] = $user->theme['template_inherits_id']; } - $recompile = (@filemtime($filename) < filemtime($this->files[$handle])) ? true : false; + $recompile = (@filemtime($filename) < @filemtime($this->files[$handle])) ? true : false; } // Recompile page if the original template is newer, otherwise load the compiled version @@ -296,33 +363,59 @@ class template { return $filename; } + + // Inheritance - we point to another template file for this one. Equality is also used for store_db + if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + { + $this->files[$handle] = $this->files_inherit[$handle]; + $this->files_template[$handle] = $user->theme['template_inherits_id']; + } - global $db, $phpbb_root_path; + // If we don't have a file assigned to this handle, die. + if (!isset($this->files[$handle])) + { + trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); + } - if (!class_exists('template_compile')) + if (!class_exists('phpbb_template_compile')) { - include($phpbb_root_path . 'includes/functions_template.' . $phpEx); + include 'template_compile.php'; } + + $compile = new phpbb_template_compile($this); - // Inheritance - we point to another template file for this one. Equality is also used for store_db - if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + if ($compile->_tpl_load_file($handle) === false) { - $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $user->theme['template_inherits_id']; + return false; } + + return $filename; + } - $compile = new template_compile($this); + /** + * This code should only run when some high level error prevents us from writing to the cache. + * @access private + * @param string $handle Template handle to compile + * @return string|bool Return compiled code on success otherwise false + * @uses template_compile is used to compile template + */ + private function _tpl_eval($handle) + { + if (!class_exists('phpbb_template_compile')) + { + include 'template_compile.php'; + } + + $compile = new phpbb_template_compile($this); // If we don't have a file assigned to this handle, die. if (!isset($this->files[$handle])) { - trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); + trigger_error("template->_tpl_eval(): No file specified for handle $handle", E_USER_ERROR); } - // Just compile if no user object is present (happens within the installer) - if (!$user) + if (($code = $compile->_tpl_gen_src($handle)) === false) { - $compile->_tpl_load_file($handle); return false; } @@ -337,6 +430,8 @@ class template } $ids[] = $user->theme['template_id']; + global $db; + foreach ($ids as $id) { $sql = 'SELECT * @@ -381,7 +476,7 @@ class template $this->files_template[$row['template_filename']] = $user->theme['template_id']; } - if ($force_reload || $row['template_mtime'] < filemtime($file)) + if ($force_reload || $row['template_mtime'] < @filemtime($file)) { if ($row['template_filename'] == $this->filename[$handle]) { @@ -441,8 +536,9 @@ class template /** * Assign key variable pairs from an array * @access public + * @param array $vararray A hash of variable name => value pairs */ - function assign_vars($vararray) + public function assign_vars(array $vararray) { foreach ($vararray as $key => $val) { @@ -455,8 +551,10 @@ class template /** * Assign a single variable to a single key * @access public + * @param string $varname Variable name + * @param string $varval Value to assign to variable */ - function assign_var($varname, $varval) + public function assign_var($varname, $varval) { $this->_rootref[$varname] = $varval; @@ -466,8 +564,10 @@ class template /** * Assign key variable pairs from an array to a specified block * @access public + * @param string $blockname Name of block to assign $vararray to + * @param array $vararray A hash of variable name => value pairs */ - function assign_block_vars($blockname, $vararray) + public function assign_block_vars($blockname, array $vararray) { if (strpos($blockname, '.') !== false) { @@ -558,18 +658,51 @@ class template * @return bool false on error, true on success * @access public */ - function alter_block_array($blockname, $vararray, $key = false, $mode = 'insert') + public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert') { if (strpos($blockname, '.') !== false) { - // Nested blocks are not supported - return false; + // Nested block. + $blocks = explode('.', $blockname); + $blockcount = sizeof($blocks) - 1; + + $block = &$this->_tpldata; + for ($i = 0; $i < $blockcount; $i++) + { + if (($pos = strpos($blocks[$i], '[')) !== false) + { + $name = substr($blocks[$i], 0, $pos); + + if (strpos($blocks[$i], '[]') === $pos) + { + $index = sizeof($block[$name]) - 1; + } + else + { + $index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1); + } + } + else + { + $name = $blocks[$i]; + $index = sizeof($block[$name]) - 1; + } + $block = &$block[$name]; + $block = &$block[$index]; + } + + $block = &$block[$blocks[$i]]; // Traverse the last block + } + else + { + // Top-level block. + $block = &$this->_tpldata[$blockname]; } // Change key to zero (change first position) if false and to last position if true if ($key === false || $key === true) { - $key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]); + $key = ($key === false) ? 0 : sizeof($block); } // Get correct position if array given @@ -579,7 +712,7 @@ class template list($search_key, $search_value) = @each($key); $key = NULL; - foreach ($this->_tpldata[$blockname] as $i => $val_ary) + foreach ($block as $i => $val_ary) { if ($val_ary[$search_key] === $search_value) { @@ -612,15 +745,13 @@ class template } // Re-position template blocks - for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--) + for ($i = sizeof($block); $i > $key; $i--) { - $this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1]; - $this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i; + $block[$i] = $block[$i-1]; } // Insert vararray at given position - $vararray['S_ROW_COUNT'] = $key; - $this->_tpldata[$blockname][$key] = $vararray; + $block[$key] = $vararray; return true; } @@ -628,12 +759,13 @@ class template // Which block to change? if ($mode == 'change') { - if ($key == sizeof($this->_tpldata[$blockname])) + if ($key == sizeof($block)) { $key--; } - $this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray); + $block[$key] = array_merge($block[$key], $vararray); + return true; } @@ -643,8 +775,11 @@ class template /** * Include a separate template * @access private + * @param string $filename Template filename to include + * @param bool $include True to include the file, false to just load it + * @uses template_compile is used to compile uncached templates */ - function _tpl_include($filename, $include = true) + private function _tpl_include($filename, $include = true) { $handle = $filename; $this->filename[$handle] = $filename; @@ -654,18 +789,31 @@ class template $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; } - $filename = $this->_tpl_load($handle); + $filename = $this->_tpl_load($handle); if ($include) { global $user; + $_tpldata = &$this->_tpldata; + $_rootref = &$this->_rootref; + $_lang = &$user->lang; + if ($filename) { include($filename); return; } - eval(' ?>' . $this->compiled_code[$handle] . '_tpl_gen_src($handle)) !== false) + { + $code = ' ?> ' . $code . ' Date: Sat, 23 Apr 2011 22:47:22 -0400 Subject: [feature/template-engine] Delete ?>, add newline at EOF. PHPBB3-9726 --- phpBB/includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 47262eaccf..5eebeeae34 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -844,4 +844,4 @@ class phpbb_template class template extends phpbb_template { // dirty hack -} \ No newline at end of file +} -- cgit v1.2.1 From 203187a8410c411b0bdd90729e19c257ec3a7820 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 24 Apr 2011 00:22:41 -0400 Subject: [feature/template-engine] Fix recompilation logic. Do not change $recompile from true to false - any recompilation condition alone is sufficient to force recompilation. Also uncomment the nonexistent file test which passes with this fix. PHPBB3-9726 --- phpBB/includes/template.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 5eebeeae34..5d0e8416a4 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -343,19 +343,22 @@ class phpbb_template $recompile = false; $recompile = (!file_exists($filename) || @filesize($filename) === 0 || ($config['load_tplcompile'] && @filemtime($filename) < @filemtime($this->files[$handle]))) ? true : false; - if (defined('DEBUG_EXTRA')) - { - $recompile = true; - } - else if ($config['load_tplcompile']) + if (!$recompile) { - // No way around it: we need to check inheritance here - if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + if (defined('DEBUG_EXTRA')) { - $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $user->theme['template_inherits_id']; + $recompile = true; + } + else if ($config['load_tplcompile']) + { + // No way around it: we need to check inheritance here + if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + { + $this->files[$handle] = $this->files_inherit[$handle]; + $this->files_template[$handle] = $user->theme['template_inherits_id']; + } + $recompile = (@filemtime($filename) < @filemtime($this->files[$handle])) ? true : false; } - $recompile = (@filemtime($filename) < @filemtime($this->files[$handle])) ? true : false; } // Recompile page if the original template is newer, otherwise load the compiled version -- cgit v1.2.1 From 321ecf427356f2d503818089c7b0e008e5a73c2c Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 24 Apr 2011 01:18:09 -0400 Subject: [feature/template-engine] Delete class_exists checks, rely on autoloading. PHPBB3-9726 --- phpBB/includes/template.php | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 5d0e8416a4..30ede2f242 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -380,11 +380,6 @@ class phpbb_template trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); } - if (!class_exists('phpbb_template_compile')) - { - include 'template_compile.php'; - } - $compile = new phpbb_template_compile($this); if ($compile->_tpl_load_file($handle) === false) @@ -404,11 +399,6 @@ class phpbb_template */ private function _tpl_eval($handle) { - if (!class_exists('phpbb_template_compile')) - { - include 'template_compile.php'; - } - $compile = new phpbb_template_compile($this); // If we don't have a file assigned to this handle, die. -- cgit v1.2.1 From c8db531fcb11cdeeefcae88b05c2674b74e2d824 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 24 Apr 2011 01:59:40 -0400 Subject: [feature/template-engine] Removed a useless space. PHPBB3-9726 --- phpBB/includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 30ede2f242..24d7aeadd0 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -782,7 +782,7 @@ class phpbb_template $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; } - $filename = $this->_tpl_load($handle); + $filename = $this->_tpl_load($handle); if ($include) { -- cgit v1.2.1 From 81962d1d8ff083dd366c77e7d3858fd2c9ed7d43 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Tue, 23 Nov 2010 16:09:09 +0100 Subject: [ticket/9924] Pass template instance into $template->display hook This is a cherry-pick of 053cf790a93e9cfb521f484901d79c72783f868f which appears to have been partially reverted here. PHPBB3-9924 --- phpBB/includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 24d7aeadd0..2b4c7a09f7 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -252,7 +252,7 @@ class phpbb_template { global $user, $phpbb_hook; - if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once)) + if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) { if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__))) { -- cgit v1.2.1 From a2c75f60537ccf6a424c7f066eafbffc13c0c38d Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 24 Apr 2011 21:18:18 -0400 Subject: [feature/template-engine] Deleted $template from phpbb_template_compile class. phpbb_template_compile is now much simpler. It takes complete file paths as inputs, either source template path or source template path and output compiled template path. The number of methods also went down to two - compile template and returned compiled text or compile and write to file. phpbb_compile class is responsible for determining source and compiled paths. It already had all the data necessary for this, now the code is in the same place as the data it uses. PHPBB3-9726 --- phpBB/includes/template.php | 67 +++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 18 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 2b4c7a09f7..e1e96791c9 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -366,7 +366,7 @@ class phpbb_template { return $filename; } - + // Inheritance - we point to another template file for this one. Equality is also used for store_db if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) { @@ -374,20 +374,54 @@ class phpbb_template $this->files_template[$handle] = $user->theme['template_inherits_id']; } + $source_file = $this->_source_file_for_handle($handle); + + $compile = new phpbb_template_compile($this); + + if ($compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)) === false) + { + return false; + } + + return $filename; + } + + /** + * Resolves template handle $handle to source file path. + * @access private + * @param string $handle Template handle (i.e. "friendly" template name) + * @return string Source file path + */ + private function _source_file_for_handle($handle) + { // If we don't have a file assigned to this handle, die. if (!isset($this->files[$handle])) { - trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); + trigger_error("_source_file_for_handle(): No file specified for handle $handle", E_USER_ERROR); } - $compile = new phpbb_template_compile($this); + $source_file = $this->files[$handle]; - if ($compile->_tpl_load_file($handle) === false) + // Try and open template for reading + if (!file_exists($source_file)) { - return false; + trigger_error("_source_file_for_handle(): File $source_file does not exist", E_USER_ERROR); } - - return $filename; + return $source_file; + } + + /** + * Determines compiled file path for handle $handle. + * @access private + * @param string $handle Template handle (i.e. "friendly" template name) + * @return string Compiled file path + */ + private function _compiled_file_for_handle($handle) + { + global $phpEx; + + $compiled_file = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; + return $compiled_file; } /** @@ -401,13 +435,9 @@ class phpbb_template { $compile = new phpbb_template_compile($this); - // If we don't have a file assigned to this handle, die. - if (!isset($this->files[$handle])) - { - trigger_error("template->_tpl_eval(): No file specified for handle $handle", E_USER_ERROR); - } + $source_file = $this->_source_file_for_handle($handle); - if (($code = $compile->_tpl_gen_src($handle)) === false) + if (($code = $compile->compile_gen($source_file)) === false) { return false; } @@ -473,13 +503,13 @@ class phpbb_template { if ($row['template_filename'] == $this->filename[$handle]) { - $compile->_tpl_load_file($handle, true); + $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)); } else { $this->files[$row['template_filename']] = $file; $this->filename[$row['template_filename']] = $row['template_filename']; - $compile->_tpl_load_file($row['template_filename'], true); + $compile->compile_write($this->_source_file_for_handle($row['template_filename']), $this->_compiled_file_for_handle($row['template_filename'])); unset($this->compiled_code[$row['template_filename']]); unset($this->files[$row['template_filename']]); unset($this->filename[$row['template_filename']]); @@ -515,14 +545,14 @@ class phpbb_template $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id']; } // Try to load from filesystem and instruct to insert into the styles table... - $compile->_tpl_load_file($handle, true); + $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)); return false; } return false; } - $compile->_tpl_load_file($handle); + $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)); return false; } @@ -801,7 +831,8 @@ class phpbb_template { $compile = new phpbb_template_compile($this); - if (($code = $compile->_tpl_gen_src($handle)) !== false) + $source_file = $this->_source_file_for_handle($handle); + if (($code = $compile->compile_gen($source_file)) !== false) { $code = ' ?> ' . $code . ' Date: Sun, 24 Apr 2011 21:22:43 -0400 Subject: [feature/template-engine] Deleted silencing of notices. The code is now supposed to be notice-free, therefore there is no need to have the notices silenced. PHPBB3-9726 --- phpBB/includes/template.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index e1e96791c9..109f7a29ee 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -260,6 +260,7 @@ class phpbb_template } } + /* if (defined('IN_ERROR_HANDLER')) { if ((E_NOTICE & error_reporting()) == E_NOTICE) @@ -267,6 +268,7 @@ class phpbb_template error_reporting(error_reporting() ^ E_NOTICE); } } + */ $_tpldata = &$this->_tpldata; $_rootref = &$this->_rootref; -- cgit v1.2.1 From 909ee59871402aa8dc7fa4e540d2f7e5da1628fc Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 26 Apr 2011 12:10:52 -0400 Subject: [feature/template-engine] Removed $this from new phpbb_template_compile calls. The compile class no longer takes template as a parameter. PHPBB3-9726 --- phpBB/includes/template.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 109f7a29ee..ae68f5fad3 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -378,7 +378,7 @@ class phpbb_template $source_file = $this->_source_file_for_handle($handle); - $compile = new phpbb_template_compile($this); + $compile = new phpbb_template_compile(); if ($compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)) === false) { @@ -435,7 +435,7 @@ class phpbb_template */ private function _tpl_eval($handle) { - $compile = new phpbb_template_compile($this); + $compile = new phpbb_template_compile(); $source_file = $this->_source_file_for_handle($handle); @@ -831,7 +831,7 @@ class phpbb_template } else { - $compile = new phpbb_template_compile($this); + $compile = new phpbb_template_compile(); $source_file = $this->_source_file_for_handle($handle); if (($code = $compile->compile_gen($source_file)) !== false) -- cgit v1.2.1 From c844a277b2a6e4ad6232925fa78868cd945fc8a1 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 1 May 2011 03:09:49 -0400 Subject: [feature/template-engine] Check return value of display() in assign_display(). If display() failed, propagate the failure out of assign_display(). PHPBB3-9726 --- phpBB/includes/template.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index ae68f5fad3..b07e77989c 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -304,7 +304,11 @@ class phpbb_template public function assign_display($handle, $template_var = '', $return_content = true, $include_once = false) { ob_start(); - $this->display($handle, $include_once); + $result = $this->display($handle, $include_once); + if ($result === false) + { + return false; + } $contents = ob_get_clean(); if ($return_content) -- cgit v1.2.1 From 234b891a4be11dc0f20d93dd272242ad20344aa0 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 1 May 2011 03:14:11 -0400 Subject: [feature/template-engine] Fixed description of assign_display(). This function returns false on failure, which can happen if display() failed. Document the failure return value. PHPBB3-9726 --- phpBB/includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index b07e77989c..ac37dc3dd2 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -299,7 +299,7 @@ class phpbb_template * @param string $template_var Template variable to assign compiled handle to * @param bool $return_content If true return compiled handle, otherwise assign to $template_var * @param bool $include_once Allow multiple inclusions of the file - * @return bool|string If $return_content is true return string of the compiled handle, otherwise return true + * @return bool|string false on failure, otherwise if $return_content is true return string of the compiled handle, otherwise return true */ public function assign_display($handle, $template_var = '', $return_content = true, $include_once = false) { -- cgit v1.2.1 From 48691b53a6b3397ede4f009e8a108b14b7f7f305 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 1 May 2011 04:00:24 -0400 Subject: [feature/template-engine] Rename template_compile methods for clarity. PHPBB3-9726 --- phpBB/includes/template.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index ac37dc3dd2..dc461fb60e 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -384,7 +384,7 @@ class phpbb_template $compile = new phpbb_template_compile(); - if ($compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)) === false) + if ($compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)) === false) { return false; } @@ -443,7 +443,7 @@ class phpbb_template $source_file = $this->_source_file_for_handle($handle); - if (($code = $compile->compile_gen($source_file)) === false) + if (($code = $compile->compile_file($source_file)) === false) { return false; } @@ -509,13 +509,13 @@ class phpbb_template { if ($row['template_filename'] == $this->filename[$handle]) { - $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)); + $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); } else { $this->files[$row['template_filename']] = $file; $this->filename[$row['template_filename']] = $row['template_filename']; - $compile->compile_write($this->_source_file_for_handle($row['template_filename']), $this->_compiled_file_for_handle($row['template_filename'])); + $compile->compile_file_to_file($this->_source_file_for_handle($row['template_filename']), $this->_compiled_file_for_handle($row['template_filename'])); unset($this->compiled_code[$row['template_filename']]); unset($this->files[$row['template_filename']]); unset($this->filename[$row['template_filename']]); @@ -525,7 +525,7 @@ class phpbb_template if ($row['template_filename'] == $this->filename[$handle]) { $this->compiled_code[$handle] = $compile->compile(trim($row['template_data'])); - $compile->compile_write($handle, $this->compiled_code[$handle]); + $compile->compile_file_to_file($handle, $this->compiled_code[$handle]); } else { @@ -533,7 +533,7 @@ class phpbb_template if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx)) { $this->filename[$row['template_filename']] = $row['template_filename']; - $compile->compile_write($row['template_filename'], $compile->compile(trim($row['template_data']))); + $compile->compile_file_to_file($row['template_filename'], $compile->compile(trim($row['template_data']))); unset($this->filename[$row['template_filename']]); } } @@ -551,14 +551,14 @@ class phpbb_template $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id']; } // Try to load from filesystem and instruct to insert into the styles table... - $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)); + $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); return false; } return false; } - $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)); + $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); return false; } @@ -838,7 +838,7 @@ class phpbb_template $compile = new phpbb_template_compile(); $source_file = $this->_source_file_for_handle($handle); - if (($code = $compile->compile_gen($source_file)) !== false) + if (($code = $compile->compile_file($source_file)) !== false) { $code = ' ?> ' . $code . ' Date: Sun, 1 May 2011 04:03:06 -0400 Subject: [feature/template-engine] Delete template class, use phpbb_template instead. PHPBB3-9726 --- phpBB/includes/template.php | 9 --------- 1 file changed, 9 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index dc461fb60e..2a32af406c 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -866,12 +866,3 @@ class phpbb_template include($file); } } - -/** - * @todo remove this - * - */ -class template extends phpbb_template -{ - // dirty hack -} -- cgit v1.2.1 From 237deb12cea909822e0f2fa3c072d31adfec3fb1 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 3 May 2011 23:50:17 -0400 Subject: [feature/template-engine] Removed storedb-related logic. phpBB 3.1 will not provide the option to store templates in the database. This commit removes code that handles templates stored in database from the template engine. PHPBB3-9726 --- phpBB/includes/template.php | 126 +------------------------------------------- 1 file changed, 2 insertions(+), 124 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 2a32af406c..add52b5c30 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -74,7 +74,6 @@ class phpbb_template public $files_template = array(); public $inherit_root = ''; - public $orig_tpl_storedb; public $orig_tpl_inherits_id; // this will hash handle names to the compiled/uncompiled code for that handle. @@ -93,17 +92,11 @@ class phpbb_template $this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'; $this->cachepath = $phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $user->theme['template_path']) . '_'; - if ($this->orig_tpl_storedb === null) - { - $this->orig_tpl_storedb = $user->theme['template_storedb']; - } - if ($this->orig_tpl_inherits_id === null) { $this->orig_tpl_inherits_id = $user->theme['template_inherits_id']; } - $user->theme['template_storedb'] = $this->orig_tpl_storedb; $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; if ($user->theme['template_inherits_id']) @@ -156,10 +149,6 @@ class phpbb_template $this->orig_tpl_inherits_id = false; } - // the database does not store the path or name of a custom template - // so there is no way we can properly store custom templates there - $this->orig_tpl_storedb = false; - $this->_rootref = &$this->_tpldata['.'][0]; return true; @@ -337,10 +326,9 @@ class phpbb_template trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); } - // reload these settings to have the values they had when this object was initialised + // reload this setting to have the values they had when this object was initialised // using set_template or set_custom_template, they might otherwise have been overwritten // by other template class instances in between. - $user->theme['template_storedb'] = $this->orig_tpl_storedb; $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; @@ -373,7 +361,7 @@ class phpbb_template return $filename; } - // Inheritance - we point to another template file for this one. Equality is also used for store_db + // Inheritance - we point to another template file for this one. if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) { $this->files[$handle] = $this->files_inherit[$handle]; @@ -448,116 +436,6 @@ class phpbb_template return false; } - if (isset($user->theme['template_storedb']) && $user->theme['template_storedb']) - { - $rows = array(); - $ids = array(); - // Inheritance - if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id']) - { - $ids[] = $user->theme['template_inherits_id']; - } - $ids[] = $user->theme['template_id']; - - global $db; - - foreach ($ids as $id) - { - $sql = 'SELECT * - FROM ' . STYLES_TEMPLATE_DATA_TABLE . ' - WHERE template_id = ' . $id . " - AND (template_filename = '" . $db->sql_escape($this->filename[$handle]) . "' - OR template_included " . $db->sql_like_expression($db->any_char . $this->filename[$handle] . ':' . $db->any_char) . ')'; - - $result = $db->sql_query($sql); - while ($row = $db->sql_fetchrow($result)) - { - $rows[$row['template_filename']] = $row; - } - $db->sql_freeresult($result); - } - - if (sizeof($rows)) - { - foreach ($rows as $row) - { - $file = $this->root . '/' . $row['template_filename']; - $force_reload = false; - if ($row['template_id'] != $user->theme['template_id']) - { - // make sure that we are not overlooking a file not in the db yet - if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file)) - { - $file = $this->inherit_root . '/' . $row['template_filename']; - $this->files[$row['template_filename']] = $file; - $this->files_inherit[$row['template_filename']] = $file; - $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id']; - } - else if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id']) - { - // Ok, we have a situation. There is a file in the subtemplate, but nothing in the DB. We have to fix that. - $force_reload = true; - $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id']; - } - } - else - { - $this->files_template[$row['template_filename']] = $user->theme['template_id']; - } - - if ($force_reload || $row['template_mtime'] < @filemtime($file)) - { - if ($row['template_filename'] == $this->filename[$handle]) - { - $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); - } - else - { - $this->files[$row['template_filename']] = $file; - $this->filename[$row['template_filename']] = $row['template_filename']; - $compile->compile_file_to_file($this->_source_file_for_handle($row['template_filename']), $this->_compiled_file_for_handle($row['template_filename'])); - unset($this->compiled_code[$row['template_filename']]); - unset($this->files[$row['template_filename']]); - unset($this->filename[$row['template_filename']]); - } - } - - if ($row['template_filename'] == $this->filename[$handle]) - { - $this->compiled_code[$handle] = $compile->compile(trim($row['template_data'])); - $compile->compile_file_to_file($handle, $this->compiled_code[$handle]); - } - else - { - // Only bother compiling if it doesn't already exist - if (!file_exists($this->cachepath . str_replace('/', '.', $row['template_filename']) . '.' . $phpEx)) - { - $this->filename[$row['template_filename']] = $row['template_filename']; - $compile->compile_file_to_file($row['template_filename'], $compile->compile(trim($row['template_data']))); - unset($this->filename[$row['template_filename']]); - } - } - } - } - else - { - $file = $this->root . '/' . $row['template_filename']; - - if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($file)) - { - $file = $this->inherit_root . '/' . $row['template_filename']; - $this->files[$row['template_filename']] = $file; - $this->files_inherit[$row['template_filename']] = $file; - $this->files_template[$row['template_filename']] = $user->theme['template_inherits_id']; - } - // Try to load from filesystem and instruct to insert into the styles table... - $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); - return false; - } - - return false; - } - $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); return false; } -- cgit v1.2.1 From d06e59f63bc8213a4d679ff0c20a23dcf8cd524e Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 3 May 2011 23:53:22 -0400 Subject: [feature/template-engine] Split template execution logic into classes. Template executor interface defines a template executor object. It is an object which can execute (i.e. display/render) a template. Currently there are two implementations: * phpbb_template_executor_include includes php code from a file. * phpbb_template_executor_eval eval's php code. PHPBB3-9726 --- phpBB/includes/template.php | 107 ++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 63 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index add52b5c30..8529b90719 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -263,22 +263,17 @@ class phpbb_template $_rootref = &$this->_rootref; $_lang = &$user->lang; - if (($filename = $this->_tpl_load($handle)) !== false) - { - ($include_once) ? include_once($filename) : include($filename); - } - else if (($code = $this->_tpl_eval($handle)) !== false) + $executor = $this->_tpl_load($handle); + + if ($executor) { - $code = ' ?> ' . $code . ' execute(); + return true; } else { - // if we could not eval AND the file exists, something horrific has occured return false; } - - return true; } /** @@ -311,11 +306,27 @@ class phpbb_template } /** - * Load a compiled template if possible, if not, recompile it + * Obtains a template executor for a template identified by specified + * handle. THe template executor can execute the template later. + * + * Template source will first be compiled into php code. + * If template cache is writable the compiled php code will be stored + * on filesystem and template will not be subsequently recompiled. + * If template cache is not writable template source will be recompiled + * every time it is needed. DEBUG_EXTRA define and load_tplcompile + * configuration setting may be used to force templates to be always + * recompiled. + * + * Returns an object implementing phpbb_template_executor, or null + * if template loading or compilation failed. Call execute() on the + * executor to execute the template. This will result in template + * contents sent to the output stream (unless, of course, output + * buffering is in effect). + * * @access private * @param string $handle Handle of the template to load - * @return string|bool Return filename on success otherwise false - * @uses template_compile is used to compile uncached templates + * @return phpbb_template_executor Template executor object, or null on failure + * @uses template_compile is used to compile template source */ private function _tpl_load($handle) { @@ -358,7 +369,7 @@ class phpbb_template // Recompile page if the original template is newer, otherwise load the compiled version if (!$recompile) { - return $filename; + return new phpbb_template_executor_include($filename); } // Inheritance - we point to another template file for this one. @@ -372,12 +383,21 @@ class phpbb_template $compile = new phpbb_template_compile(); - if ($compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)) === false) + $output_file = $this->_compiled_file_for_handle($handle); + if ($compile->compile_file_to_file($source_file, $output_file) !== false) { - return false; + $executor = new phpbb_template_executor_include($output_file); + } + else if (($code = $compile->compile_file($source_file)) !== false) + { + $executor = new phpbb_template_executor_eval($code); + } + else + { + $executor = null; } - return $filename; + return $executor; } /** @@ -418,28 +438,6 @@ class phpbb_template return $compiled_file; } - /** - * This code should only run when some high level error prevents us from writing to the cache. - * @access private - * @param string $handle Template handle to compile - * @return string|bool Return compiled code on success otherwise false - * @uses template_compile is used to compile template - */ - private function _tpl_eval($handle) - { - $compile = new phpbb_template_compile(); - - $source_file = $this->_source_file_for_handle($handle); - - if (($code = $compile->compile_file($source_file)) === false) - { - return false; - } - - $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); - return false; - } - /** * Assign key variable pairs from an array * @access public @@ -696,32 +694,15 @@ class phpbb_template $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; } - $filename = $this->_tpl_load($handle); + $executor = $this->_tpl_load($handle); - if ($include) + if ($executor) { - global $user; - - $_tpldata = &$this->_tpldata; - $_rootref = &$this->_rootref; - $_lang = &$user->lang; - - if ($filename) - { - include($filename); - return; - } - else - { - $compile = new phpbb_template_compile(); - - $source_file = $this->_source_file_for_handle($handle); - if (($code = $compile->compile_file($source_file)) !== false) - { - $code = ' ?> ' . $code . ' execute(); + } + else + { + // What should we do here? } } -- cgit v1.2.1 From d8122df3329214f8710c8af3c0ce1ed86505e810 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 5 May 2011 19:03:46 -0400 Subject: [feature/template-engine] Always call ob_end_clean. We have to stop output buffering even when rendering fails. PHPBB3-9726 --- phpBB/includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 8529b90719..8d7eb57dc4 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -289,11 +289,11 @@ class phpbb_template { ob_start(); $result = $this->display($handle, $include_once); + $contents = ob_get_clean(); if ($result === false) { return false; } - $contents = ob_get_clean(); if ($return_content) { -- cgit v1.2.1 From 0501640d5db158a010741e27803191ab469834c4 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 7 May 2011 15:10:50 -0400 Subject: [feature/template-engine] Added phpbb_template_context class. Objects of this class hold variables assigned to templates. PHPBB3-9726 --- phpBB/includes/template.php | 281 +++++++------------------------------------- 1 file changed, 41 insertions(+), 240 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 8d7eb57dc4..755e6adba9 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -36,19 +36,11 @@ class phpbb_template public $phpbb_optional = array(); /** - * variable that holds all the data we'll be substituting into - * the compiled templates. Takes form: - * --> $this->_tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value - * if it's a root-level variable, it'll be like this: - * --> $this->_tpldata[.][0][varname] == value - * @var array - */ - private $_tpldata = array('.' => array(0 => array())); - - /** - * @var array Reference to template->_tpldata['.'][0] + * @var phpbb_template_context Template context. + * Stores template data used during template rendering. + * @access private */ - private $_rootref; + private $_context; /** * @var string Root dir for template. @@ -109,7 +101,7 @@ class phpbb_template trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR); } - $this->_rootref = &$this->_tpldata['.'][0]; + $this->_context = new phpbb_template_context(); return true; } @@ -149,7 +141,7 @@ class phpbb_template $this->orig_tpl_inherits_id = false; } - $this->_rootref = &$this->_tpldata['.'][0]; + $this->_context = new phpbb_template_context(); return true; } @@ -181,24 +173,6 @@ class phpbb_template return true; } - /** - * Destroy template data set - * @access public - */ - public function destroy() - { - $this->_tpldata = array('.' => array(0 => array())); - $this->_rootref = &$this->_tpldata['.'][0]; - } - - /** - * destroy method kept for compatibility. - */ - public function __destruct() - { - $this->destroy(); - } - /** * Reset/empty complete block * @access public @@ -206,28 +180,7 @@ class phpbb_template */ public function destroy_block_vars($blockname) { - if (strpos($blockname, '.') !== false) - { - // Nested block. - $blocks = explode('.', $blockname); - $blockcount = sizeof($blocks) - 1; - - $str = &$this->_tpldata; - for ($i = 0; $i < $blockcount; $i++) - { - $str = &$str[$blocks[$i]]; - $str = &$str[sizeof($str) - 1]; - } - - unset($str[$blocks[$blockcount]]); - } - else - { - // Top-level block. - unset($this->_tpldata[$blockname]); - } - - return true; + $this->_context->destroy_block_vars($blockname); } /** @@ -239,7 +192,7 @@ class phpbb_template */ public function display($handle, $include_once = true) { - global $user, $phpbb_hook; + global $phpbb_hook; if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) { @@ -259,15 +212,11 @@ class phpbb_template } */ - $_tpldata = &$this->_tpldata; - $_rootref = &$this->_rootref; - $_lang = &$user->lang; - $executor = $this->_tpl_load($handle); if ($executor) { - $executor->execute(); + $executor->execute($this->_context, $this->get_lang()); return true; } else @@ -276,6 +225,27 @@ class phpbb_template } } + /** + * Obtains language array. + * This is either lang property of global $user object, or if + * it is not set an empty array. + * @return array language entries + */ + public function get_lang() + { + global $user; + + if (isset($user->lang)) + { + $lang = $user->lang; + } + else + { + $lang = array(); + } + return $lang; + } + /** * Display the handle and assign the output to a template variable or return the compiled result. * @access public @@ -369,7 +339,7 @@ class phpbb_template // Recompile page if the original template is newer, otherwise load the compiled version if (!$recompile) { - return new phpbb_template_executor_include($filename); + return new phpbb_template_executor_include($filename, $this); } // Inheritance - we point to another template file for this one. @@ -386,11 +356,11 @@ class phpbb_template $output_file = $this->_compiled_file_for_handle($handle); if ($compile->compile_file_to_file($source_file, $output_file) !== false) { - $executor = new phpbb_template_executor_include($output_file); + $executor = new phpbb_template_executor_include($output_file, $this); } else if (($code = $compile->compile_file($source_file)) !== false) { - $executor = new phpbb_template_executor_eval($code); + $executor = new phpbb_template_executor_eval($code, $this); } else { @@ -447,10 +417,8 @@ class phpbb_template { foreach ($vararray as $key => $val) { - $this->_rootref[$key] = $val; + $this->assign_var($key, $val); } - - return true; } /** @@ -461,11 +429,10 @@ class phpbb_template */ public function assign_var($varname, $varval) { - $this->_rootref[$varname] = $varval; - - return true; + $this->_context->assign_var($varname, $varval); } + // Docstring is copied from phpbb_template_context method with the same name. /** * Assign key variable pairs from an array to a specified block * @access public @@ -474,67 +441,10 @@ class phpbb_template */ public function assign_block_vars($blockname, array $vararray) { - if (strpos($blockname, '.') !== false) - { - // Nested block. - $blocks = explode('.', $blockname); - $blockcount = sizeof($blocks) - 1; - - $str = &$this->_tpldata; - for ($i = 0; $i < $blockcount; $i++) - { - $str = &$str[$blocks[$i]]; - $str = &$str[sizeof($str) - 1]; - } - - $s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0; - $vararray['S_ROW_COUNT'] = $s_row_count; - - // Assign S_FIRST_ROW - if (!$s_row_count) - { - $vararray['S_FIRST_ROW'] = true; - } - - // Now the tricky part, we always assign S_LAST_ROW and remove the entry before - // This is much more clever than going through the complete template data on display (phew) - $vararray['S_LAST_ROW'] = true; - if ($s_row_count > 0) - { - unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']); - } - - // Now we add the block that we're actually assigning to. - // We're adding a new iteration to this block with the given - // variable assignments. - $str[$blocks[$blockcount]][] = $vararray; - } - else - { - // Top-level block. - $s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0; - $vararray['S_ROW_COUNT'] = $s_row_count; - - // Assign S_FIRST_ROW - if (!$s_row_count) - { - $vararray['S_FIRST_ROW'] = true; - } - - // We always assign S_LAST_ROW and remove the entry before - $vararray['S_LAST_ROW'] = true; - if ($s_row_count > 0) - { - unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']); - } - - // Add a new iteration to this block with the variable assignments we were given. - $this->_tpldata[$blockname][] = $vararray; - } - - return true; + return $this->_context->assign_block_vars($blockname, $vararray); } + // Docstring is copied from phpbb_template_context method with the same name. /** * Change already assigned key variable pair (one-dimensional - single loop entry) * @@ -565,116 +475,7 @@ class phpbb_template */ public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert') { - if (strpos($blockname, '.') !== false) - { - // Nested block. - $blocks = explode('.', $blockname); - $blockcount = sizeof($blocks) - 1; - - $block = &$this->_tpldata; - for ($i = 0; $i < $blockcount; $i++) - { - if (($pos = strpos($blocks[$i], '[')) !== false) - { - $name = substr($blocks[$i], 0, $pos); - - if (strpos($blocks[$i], '[]') === $pos) - { - $index = sizeof($block[$name]) - 1; - } - else - { - $index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1); - } - } - else - { - $name = $blocks[$i]; - $index = sizeof($block[$name]) - 1; - } - $block = &$block[$name]; - $block = &$block[$index]; - } - - $block = &$block[$blocks[$i]]; // Traverse the last block - } - else - { - // Top-level block. - $block = &$this->_tpldata[$blockname]; - } - - // Change key to zero (change first position) if false and to last position if true - if ($key === false || $key === true) - { - $key = ($key === false) ? 0 : sizeof($block); - } - - // Get correct position if array given - if (is_array($key)) - { - // Search array to get correct position - list($search_key, $search_value) = @each($key); - - $key = NULL; - foreach ($block as $i => $val_ary) - { - if ($val_ary[$search_key] === $search_value) - { - $key = $i; - break; - } - } - - // key/value pair not found - if ($key === NULL) - { - return false; - } - } - - // Insert Block - if ($mode == 'insert') - { - // Make sure we are not exceeding the last iteration - if ($key >= sizeof($this->_tpldata[$blockname])) - { - $key = sizeof($this->_tpldata[$blockname]); - unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']); - $vararray['S_LAST_ROW'] = true; - } - else if ($key === 0) - { - unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']); - $vararray['S_FIRST_ROW'] = true; - } - - // Re-position template blocks - for ($i = sizeof($block); $i > $key; $i--) - { - $block[$i] = $block[$i-1]; - } - - // Insert vararray at given position - $block[$key] = $vararray; - - return true; - } - - // Which block to change? - if ($mode == 'change') - { - if ($key == sizeof($block)) - { - $key--; - } - - $block[$key] = array_merge($block[$key], $vararray); - - return true; - } - - return false; + return $this->_context->alter_block_array($blockname, $vararray, $key, $mode); } /** @@ -684,7 +485,7 @@ class phpbb_template * @param bool $include True to include the file, false to just load it * @uses template_compile is used to compile uncached templates */ - private function _tpl_include($filename, $include = true) + public function _tpl_include($filename, $include = true) { $handle = $filename; $this->filename[$handle] = $filename; @@ -698,7 +499,7 @@ class phpbb_template if ($executor) { - $executor->execute(); + $executor->execute($this->_context, $this->get_lang()); } else { -- cgit v1.2.1 From b5444a7d7d1f6ac694468d8cc37fc0489e714547 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 7 May 2011 17:50:48 -0400 Subject: [feature/template-engine] Removed more dead code. PHPBB3-9726 --- phpBB/includes/template.php | 6 ------ 1 file changed, 6 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 755e6adba9..02815c4e15 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -32,9 +32,6 @@ if (!defined('IN_PHPBB')) */ class phpbb_template { - public $phpbb_required = array('user', 'config'); - public $phpbb_optional = array(); - /** * @var phpbb_template_context Template context. * Stores template data used during template rendering. @@ -68,9 +65,6 @@ class phpbb_template public $orig_tpl_inherits_id; - // this will hash handle names to the compiled/uncompiled code for that handle. - public $compiled_code = array(); - /** * Set template location * @access public -- cgit v1.2.1 From 84bc485ccc426b87e21c8db627a3019eee7497dd Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 8 May 2011 04:03:41 -0400 Subject: [feature/template-engine] Renamed template executor and friends to renderer. PHPBB3-9726 --- phpBB/includes/template.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 02815c4e15..f27d0c7560 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -206,11 +206,11 @@ class phpbb_template } */ - $executor = $this->_tpl_load($handle); + $renderer = $this->_tpl_load($handle); - if ($executor) + if ($renderer) { - $executor->execute($this->_context, $this->get_lang()); + $renderer->render($this->_context, $this->get_lang()); return true; } else @@ -270,8 +270,8 @@ class phpbb_template } /** - * Obtains a template executor for a template identified by specified - * handle. THe template executor can execute the template later. + * Obtains a template renderer for a template identified by specified + * handle. The template renderer can display the template later. * * Template source will first be compiled into php code. * If template cache is writable the compiled php code will be stored @@ -281,15 +281,15 @@ class phpbb_template * configuration setting may be used to force templates to be always * recompiled. * - * Returns an object implementing phpbb_template_executor, or null - * if template loading or compilation failed. Call execute() on the - * executor to execute the template. This will result in template + * Returns an object implementing phpbb_template_renderer, or null + * if template loading or compilation failed. Call render() on the + * renderer to display the template. This will result in template * contents sent to the output stream (unless, of course, output * buffering is in effect). * * @access private * @param string $handle Handle of the template to load - * @return phpbb_template_executor Template executor object, or null on failure + * @return phpbb_template_renderer Template renderer object, or null on failure * @uses template_compile is used to compile template source */ private function _tpl_load($handle) @@ -333,7 +333,7 @@ class phpbb_template // Recompile page if the original template is newer, otherwise load the compiled version if (!$recompile) { - return new phpbb_template_executor_include($filename, $this); + return new phpbb_template_renderer_include($filename, $this); } // Inheritance - we point to another template file for this one. @@ -350,18 +350,18 @@ class phpbb_template $output_file = $this->_compiled_file_for_handle($handle); if ($compile->compile_file_to_file($source_file, $output_file) !== false) { - $executor = new phpbb_template_executor_include($output_file, $this); + $renderer = new phpbb_template_renderer_include($output_file, $this); } else if (($code = $compile->compile_file($source_file)) !== false) { - $executor = new phpbb_template_executor_eval($code, $this); + $renderer = new phpbb_template_renderer_eval($code, $this); } else { - $executor = null; + $renderer = null; } - return $executor; + return $renderer; } /** @@ -489,11 +489,11 @@ class phpbb_template $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; } - $executor = $this->_tpl_load($handle); + $renderer = $this->_tpl_load($handle); - if ($executor) + if ($renderer) { - $executor->execute($this->_context, $this->get_lang()); + $renderer->render($this->_context, $this->get_lang()); } else { -- cgit v1.2.1 From df76885b11e56348f7fbe6274a6dbc727bb677ad Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 9 May 2011 22:01:07 -0400 Subject: [feature/template-engine] Reinstate phpbb_template#destroy function. PHPBB3-9726 --- phpBB/includes/template.php | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index f27d0c7560..c03b87b13c 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -167,6 +167,15 @@ class phpbb_template return true; } + /** + * Clears all variables and blocks assigned to this template. + * @access public + */ + public function destroy() + { + $this->_context->clear(); + } + /** * Reset/empty complete block * @access public -- cgit v1.2.1 From 0ca7ad66dc8d9173394e0c5fb7e8393977de22ed Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 9 May 2011 22:01:54 -0400 Subject: [feature/template-engine] Refactor hook logic into a separate function. PHPBB3-9726 --- phpBB/includes/template.php | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index c03b87b13c..9a030ffd35 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -195,14 +195,10 @@ class phpbb_template */ public function display($handle, $include_once = true) { - global $phpbb_hook; - - if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) + $result = $this->call_hook($handle, $include_once); + if ($result !== false) { - if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__))) - { - return $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__)); - } + return $result[0]; } /* @@ -228,6 +224,27 @@ class phpbb_template } } + /** + * Calls hook if any is defined. + * @param string $handle Template handle being displayed. + * @param bool $include_once Allow multiple inclusions + */ + private function call_hook($handle, $include_once) + { + global $phpbb_hook; + + if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) + { + if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__))) + { + $result = $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__)); + return array($result); + } + } + + return false; + } + /** * Obtains language array. * This is either lang property of global $user object, or if -- cgit v1.2.1 From 97d2a6527e75a5db9249a7c57360087cbaf424ac Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Mon, 9 May 2011 22:02:14 -0400 Subject: [feature/template-engine] Remove commented out error reporting logic. PHPBB3-9726 --- phpBB/includes/template.php | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 9a030ffd35..a30690615d 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -201,16 +201,6 @@ class phpbb_template return $result[0]; } - /* - if (defined('IN_ERROR_HANDLER')) - { - if ((E_NOTICE & error_reporting()) == E_NOTICE) - { - error_reporting(error_reporting() ^ E_NOTICE); - } - } - */ - $renderer = $this->_tpl_load($handle); if ($renderer) -- cgit v1.2.1 From 169c4377e98826432411db773ebcbfa19bcdb439 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 11 May 2011 09:04:07 -0400 Subject: [feature/template-engine] Disposed of underscores in property names. PHPBB3-9726 --- phpBB/includes/template.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index a30690615d..4dadfac571 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -37,7 +37,7 @@ class phpbb_template * Stores template data used during template rendering. * @access private */ - private $_context; + private $context; /** * @var string Root dir for template. @@ -95,7 +95,7 @@ class phpbb_template trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR); } - $this->_context = new phpbb_template_context(); + $this->context = new phpbb_template_context(); return true; } @@ -135,7 +135,7 @@ class phpbb_template $this->orig_tpl_inherits_id = false; } - $this->_context = new phpbb_template_context(); + $this->context = new phpbb_template_context(); return true; } @@ -173,7 +173,7 @@ class phpbb_template */ public function destroy() { - $this->_context->clear(); + $this->context->clear(); } /** @@ -183,7 +183,7 @@ class phpbb_template */ public function destroy_block_vars($blockname) { - $this->_context->destroy_block_vars($blockname); + $this->context->destroy_block_vars($blockname); } /** @@ -205,7 +205,7 @@ class phpbb_template if ($renderer) { - $renderer->render($this->_context, $this->get_lang()); + $renderer->render($this->context, $this->get_lang()); return true; } else @@ -439,7 +439,7 @@ class phpbb_template */ public function assign_var($varname, $varval) { - $this->_context->assign_var($varname, $varval); + $this->context->assign_var($varname, $varval); } // Docstring is copied from phpbb_template_context method with the same name. @@ -451,7 +451,7 @@ class phpbb_template */ public function assign_block_vars($blockname, array $vararray) { - return $this->_context->assign_block_vars($blockname, $vararray); + return $this->context->assign_block_vars($blockname, $vararray); } // Docstring is copied from phpbb_template_context method with the same name. @@ -485,7 +485,7 @@ class phpbb_template */ public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert') { - return $this->_context->alter_block_array($blockname, $vararray, $key, $mode); + return $this->context->alter_block_array($blockname, $vararray, $key, $mode); } /** @@ -509,7 +509,7 @@ class phpbb_template if ($renderer) { - $renderer->render($this->_context, $this->get_lang()); + $renderer->render($this->context, $this->get_lang()); } else { -- cgit v1.2.1 From 504acaba6b898de13d1c27dde567f1a01c0b0bd6 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 11 May 2011 09:23:16 -0400 Subject: [feature/template-engine] Deleted useless assignment. PHPBB3-9726 --- phpBB/includes/template.php | 1 - 1 file changed, 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 4dadfac571..85d378fa67 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -325,7 +325,6 @@ class phpbb_template $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; $this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0; - $recompile = false; $recompile = (!file_exists($filename) || @filesize($filename) === 0 || ($config['load_tplcompile'] && @filemtime($filename) < @filemtime($this->files[$handle]))) ? true : false; if (!$recompile) -- cgit v1.2.1 From 77787718196c05b98efccec668c4a9762591398f Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 11 May 2011 19:25:07 -0400 Subject: [feature/template-engine] Move DEBUG_EXTRA check for $recompile up. PHPBB3-9726 --- phpBB/includes/template.php | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 85d378fa67..865535cfd5 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -325,24 +325,20 @@ class phpbb_template $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; $this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0; - $recompile = (!file_exists($filename) || @filesize($filename) === 0 || ($config['load_tplcompile'] && @filemtime($filename) < @filemtime($this->files[$handle]))) ? true : false; + $recompile = defined('DEBUG_EXTRA') || + !file_exists($filename) || + @filesize($filename) === 0 || + ($config['load_tplcompile'] && @filemtime($filename) < @filemtime($this->files[$handle])); - if (!$recompile) + if (!$recompile && $config['load_tplcompile']) { - if (defined('DEBUG_EXTRA')) - { - $recompile = true; - } - else if ($config['load_tplcompile']) + // No way around it: we need to check inheritance here + if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) { - // No way around it: we need to check inheritance here - if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) - { - $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $user->theme['template_inherits_id']; - } - $recompile = (@filemtime($filename) < @filemtime($this->files[$handle])) ? true : false; + $this->files[$handle] = $this->files_inherit[$handle]; + $this->files_template[$handle] = $user->theme['template_inherits_id']; } + $recompile = (@filemtime($filename) < @filemtime($this->files[$handle])) ? true : false; } // Recompile page if the original template is newer, otherwise load the compiled version -- cgit v1.2.1 From 7638bcb56011ebde65005a0ce1abfe7f4a6be4b7 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 11 May 2011 19:32:32 -0400 Subject: [feature/template-engine] Rename $filename to $compiled_path for clarity. PHPBB3-9726 --- phpBB/includes/template.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 865535cfd5..2ebbca3568 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -322,13 +322,13 @@ class phpbb_template // by other template class instances in between. $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; + $compiled_path = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; $this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0; $recompile = defined('DEBUG_EXTRA') || - !file_exists($filename) || - @filesize($filename) === 0 || - ($config['load_tplcompile'] && @filemtime($filename) < @filemtime($this->files[$handle])); + !file_exists($compiled_path) || + @filesize($compiled_path) === 0 || + ($config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($this->files[$handle])); if (!$recompile && $config['load_tplcompile']) { @@ -338,13 +338,13 @@ class phpbb_template $this->files[$handle] = $this->files_inherit[$handle]; $this->files_template[$handle] = $user->theme['template_inherits_id']; } - $recompile = (@filemtime($filename) < @filemtime($this->files[$handle])) ? true : false; + $recompile = (@filemtime($compiled_path) < @filemtime($this->files[$handle])) ? true : false; } // Recompile page if the original template is newer, otherwise load the compiled version if (!$recompile) { - return new phpbb_template_renderer_include($filename, $this); + return new phpbb_template_renderer_include($compiled_path, $this); } // Inheritance - we point to another template file for this one. -- cgit v1.2.1 From d2daaf0317ac6bb224b8f93c1ac9578c9d570dc9 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 11 May 2011 19:38:04 -0400 Subject: [feature/template-engine] Try to handle failed template includes. PHPBB3-9726 --- phpBB/includes/template.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 2ebbca3568..90b8bb2a22 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -508,7 +508,8 @@ class phpbb_template } else { - // What should we do here? + // trigger_error cannot be used here, as the output already started + echo 'template->_tpl_include(): Failed including ' . htmlspecialchars($handle) . "\n"; } } @@ -525,7 +526,7 @@ class phpbb_template if (!file_exists($file)) { // trigger_error cannot be used here, as the output already started - echo 'template->_php_include(): File ' . htmlspecialchars($file) . ' does not exist or is empty'; + echo 'template->_php_include(): File ' . htmlspecialchars($file) . " does not exist\n"; return; } include($file); -- cgit v1.2.1 From 345852d240fe224fa825f90b0ef8a34398c91291 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 11 May 2011 19:43:08 -0400 Subject: [feature/template-engine] Clarify cache directory path for set_custom_template Even if the template may be outside of phpBB, phpBB's cache directory is still going to be used for storing compiled template code. PHPBB3-9726 --- phpBB/includes/template.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 90b8bb2a22..daff785dc0 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -101,7 +101,10 @@ class phpbb_template } /** - * Set custom template location (able to use directory outside of phpBB) + * Set custom template location (able to use directory outside of phpBB). + * + * Note: Templates are still compiled to phpBB's cache directory. + * * @access public * @param string $template_path Path to template directory * @param string $template_name Name of template -- cgit v1.2.1 From d2ac05aa74d3a1c9723be41a4c0ffe86542f223f Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 12 May 2011 18:08:36 -0400 Subject: [feature/template-engine] Replaced globals with dependency injection. PHPBB3-9726 --- phpBB/includes/template.php | 93 ++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 35 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index daff785dc0..64a3b80f07 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -65,34 +65,67 @@ class phpbb_template public $orig_tpl_inherits_id; + /** + * @var string phpBB root path + */ + private $phpbb_root_path; + + /** + * @var phpEx PHP file extension + */ + private $phpEx; + + /** + * @var phpbb_config phpBB config instance + */ + private $config; + + /** + * @var user current user + */ + private $user; + + /** + * Constructor. + * + * @param string $phpbb_root_path phpBB root path + * @param user $user current user + */ + public function __construct($phpbb_root_path, $phpEx, $config, $user) + { + $this->phpbb_root_path = $phpbb_root_path; + $this->phpEx = $phpEx; + $this->config = $config; + $this->user = $user; + } + /** * Set template location * @access public */ public function set_template() { - global $phpbb_root_path, $user; - - if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template')) + $template_path = $this->user->theme['template_path']; + if (file_exists($this->phpbb_root_path . 'styles/' . $template_path . '/template')) { - $this->root = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template'; - $this->cachepath = $phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $user->theme['template_path']) . '_'; + $this->root = $this->phpbb_root_path . 'styles/' . $template_path . '/template'; + $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $template_path) . '_'; if ($this->orig_tpl_inherits_id === null) { - $this->orig_tpl_inherits_id = $user->theme['template_inherits_id']; + $this->orig_tpl_inherits_id = $this->user->theme['template_inherits_id']; } - $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; + $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - if ($user->theme['template_inherits_id']) + if ($this->user->theme['template_inherits_id']) { - $this->inherit_root = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template'; + $this->inherit_root = $this->phpbb_root_path . 'styles/' . $this->user->theme['template_inherit_path'] . '/template'; } } else { - trigger_error('Template path could not be found: styles/' . $user->theme['template_path'] . '/template', E_USER_ERROR); + trigger_error('Template path could not be found: styles/' . $template_path . '/template', E_USER_ERROR); } $this->context = new phpbb_template_context(); @@ -112,8 +145,6 @@ class phpbb_template */ public function set_custom_template($template_path, $template_name, $fallback_template_path = false) { - global $phpbb_root_path, $user; - // Make sure $template_path has no ending slash if (substr($template_path, -1) == '/') { @@ -121,7 +152,7 @@ class phpbb_template } $this->root = $template_path; - $this->cachepath = $phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_'; + $this->cachepath = $this->phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_'; if ($fallback_template_path !== false) { @@ -240,17 +271,15 @@ class phpbb_template /** * Obtains language array. - * This is either lang property of global $user object, or if + * This is either lang property of $user property, or if * it is not set an empty array. * @return array language entries */ public function get_lang() { - global $user; - - if (isset($user->lang)) + if (isset($this->user->lang)) { - $lang = $user->lang; + $lang = $this->user->lang; } else { @@ -313,8 +342,6 @@ class phpbb_template */ private function _tpl_load($handle) { - global $user, $phpEx, $config; - if (!isset($this->filename[$handle])) { trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); @@ -323,23 +350,23 @@ class phpbb_template // reload this setting to have the values they had when this object was initialised // using set_template or set_custom_template, they might otherwise have been overwritten // by other template class instances in between. - $user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; + $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - $compiled_path = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; - $this->files_template[$handle] = (isset($user->theme['template_id'])) ? $user->theme['template_id'] : 0; + $compiled_path = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; + $this->files_template[$handle] = (isset($this->user->theme['template_id'])) ? $this->user->theme['template_id'] : 0; $recompile = defined('DEBUG_EXTRA') || !file_exists($compiled_path) || @filesize($compiled_path) === 0 || - ($config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($this->files[$handle])); + ($this->config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($this->files[$handle])); - if (!$recompile && $config['load_tplcompile']) + if (!$recompile && $this->config['load_tplcompile']) { // No way around it: we need to check inheritance here - if ($user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + if ($this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) { $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $user->theme['template_inherits_id']; + $this->files_template[$handle] = $this->user->theme['template_inherits_id']; } $recompile = (@filemtime($compiled_path) < @filemtime($this->files[$handle])) ? true : false; } @@ -351,10 +378,10 @@ class phpbb_template } // Inheritance - we point to another template file for this one. - if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + if (isset($this->user->theme['template_inherits_id']) && $this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) { $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $user->theme['template_inherits_id']; + $this->files_template[$handle] = $this->user->theme['template_inherits_id']; } $source_file = $this->_source_file_for_handle($handle); @@ -410,9 +437,7 @@ class phpbb_template */ private function _compiled_file_for_handle($handle) { - global $phpEx; - - $compiled_file = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx; + $compiled_file = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; return $compiled_file; } @@ -522,9 +547,7 @@ class phpbb_template */ private function _php_include($filename) { - global $phpbb_root_path; - - $file = $phpbb_root_path . $filename; + $file = $this->phpbb_root_path . $filename; if (!file_exists($file)) { -- cgit v1.2.1 From 94560d708649df876f8486d4ba5361475037f1ff Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 12 May 2011 20:09:41 -0400 Subject: [feature/template-engine] Make INCLUDEPHP relative to board root. PHPBB3-9726 --- phpBB/includes/template.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 64a3b80f07..f2b1cccd2c 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -513,7 +513,7 @@ class phpbb_template /** * Include a separate template - * @access private + * @access public * @param string $filename Template filename to include * @param bool $include True to include the file, false to just load it * @uses template_compile is used to compile uncached templates @@ -543,9 +543,9 @@ class phpbb_template /** * Include a php-file - * @access private + * @access public */ - private function _php_include($filename) + public function _php_include($filename) { $file = $this->phpbb_root_path . $filename; -- cgit v1.2.1 From b04f0a5f70ed79d45e254d7b6db61f6fc1f484f5 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 13 May 2011 10:49:53 -0400 Subject: [feature/template-engine] Delete @access everywhere. Access specification in php 5 is done directly on functions/properties. PHPBB3-9726 --- phpBB/includes/template.php | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index f2b1cccd2c..5fb0f41311 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -35,7 +35,6 @@ class phpbb_template /** * @var phpbb_template_context Template context. * Stores template data used during template rendering. - * @access private */ private $context; @@ -101,7 +100,6 @@ class phpbb_template /** * Set template location - * @access public */ public function set_template() { @@ -138,7 +136,6 @@ class phpbb_template * * Note: Templates are still compiled to phpBB's cache directory. * - * @access public * @param string $template_path Path to template directory * @param string $template_name Name of template * @param string $fallback_template_path Path to fallback template @@ -177,7 +174,6 @@ class phpbb_template /** * Sets the template filenames for handles. $filename_array * should be a hash of handle => filename pairs. - * @access public * @param array $filname_array Should be a hash of handle => filename pairs. */ public function set_filenames(array $filename_array) @@ -203,7 +199,6 @@ class phpbb_template /** * Clears all variables and blocks assigned to this template. - * @access public */ public function destroy() { @@ -212,7 +207,6 @@ class phpbb_template /** * Reset/empty complete block - * @access public * @param string $blockname Name of block to destroy */ public function destroy_block_vars($blockname) @@ -222,7 +216,6 @@ class phpbb_template /** * Display handle - * @access public * @param string $handle Handle to display * @param bool $include_once Allow multiple inclusions * @return bool True on success, false on failure @@ -290,7 +283,6 @@ class phpbb_template /** * Display the handle and assign the output to a template variable or return the compiled result. - * @access public * @param string $handle Handle to operate on * @param string $template_var Template variable to assign compiled handle to * @param bool $return_content If true return compiled handle, otherwise assign to $template_var @@ -335,7 +327,6 @@ class phpbb_template * contents sent to the output stream (unless, of course, output * buffering is in effect). * - * @access private * @param string $handle Handle of the template to load * @return phpbb_template_renderer Template renderer object, or null on failure * @uses template_compile is used to compile template source @@ -407,7 +398,6 @@ class phpbb_template /** * Resolves template handle $handle to source file path. - * @access private * @param string $handle Template handle (i.e. "friendly" template name) * @return string Source file path */ @@ -431,7 +421,6 @@ class phpbb_template /** * Determines compiled file path for handle $handle. - * @access private * @param string $handle Template handle (i.e. "friendly" template name) * @return string Compiled file path */ @@ -443,7 +432,6 @@ class phpbb_template /** * Assign key variable pairs from an array - * @access public * @param array $vararray A hash of variable name => value pairs */ public function assign_vars(array $vararray) @@ -456,7 +444,6 @@ class phpbb_template /** * Assign a single variable to a single key - * @access public * @param string $varname Variable name * @param string $varval Value to assign to variable */ @@ -468,7 +455,6 @@ class phpbb_template // Docstring is copied from phpbb_template_context method with the same name. /** * Assign key variable pairs from an array to a specified block - * @access public * @param string $blockname Name of block to assign $vararray to * @param array $vararray A hash of variable name => value pairs */ @@ -504,7 +490,6 @@ class phpbb_template * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars) * * @return bool false on error, true on success - * @access public */ public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert') { @@ -513,7 +498,6 @@ class phpbb_template /** * Include a separate template - * @access public * @param string $filename Template filename to include * @param bool $include True to include the file, false to just load it * @uses template_compile is used to compile uncached templates @@ -543,7 +527,6 @@ class phpbb_template /** * Include a php-file - * @access public */ public function _php_include($filename) { -- cgit v1.2.1 From 7d911e0bc1475ee769bfd3849d1eca6db7cc95d2 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 13 May 2011 10:54:23 -0400 Subject: [feature/template-engine] Delete @version everywhere. This is not used since the switch to git. PHPBB3-9726 --- phpBB/includes/template.php | 1 - 1 file changed, 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 5fb0f41311..e99b9a8c6c 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -2,7 +2,6 @@ /** * * @package phpBB3 -* @version $Id$ * @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc * @license http://opensource.org/licenses/gpl-license.php GNU Public License * -- cgit v1.2.1 From 6ae5a64f6c12937367ad56cac5e38104b445c780 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 19 May 2011 12:18:16 -0400 Subject: [feature/template-engine] Fixed absolute path PHP includes, added test. PHPBB3-9726 --- phpBB/includes/template.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index e99b9a8c6c..935605b12a 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -529,7 +529,14 @@ class phpbb_template */ public function _php_include($filename) { - $file = $this->phpbb_root_path . $filename; + if (is_absolute($filename)) + { + $file = $filename; + } + else + { + $file = $this->phpbb_root_path . $filename; + } if (!file_exists($file)) { -- cgit v1.2.1 From f7b06ca12db9da519d49ce334eaf96573d003c0f Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 9 Jul 2011 23:33:44 +0200 Subject: [feature/template-engine] Move template.php to includes/template This allows making use of autoloading. PHPBB3-9726 --- phpBB/includes/template.php | 549 -------------------------------------------- 1 file changed, 549 deletions(-) delete mode 100644 phpBB/includes/template.php (limited to 'phpBB/includes/template.php') diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php deleted file mode 100644 index 935605b12a..0000000000 --- a/phpBB/includes/template.php +++ /dev/null @@ -1,549 +0,0 @@ - $user->img('icon_contact', 'CONTACT', 'full'); -* -* More in-depth... -* yadayada -*/ - -/** -* Base Template class. -* @package phpBB3 -*/ -class phpbb_template -{ - /** - * @var phpbb_template_context Template context. - * Stores template data used during template rendering. - */ - private $context; - - /** - * @var string Root dir for template. - */ - private $root = ''; - - /** - * @var string Path of the cache directory for the template - */ - public $cachepath = ''; - - /** - * @var array Hash of handle => file path pairs - */ - public $files = array(); - - /** - * @var array Hash of handle => filename pairs - */ - public $filename = array(); - - public $files_inherit = array(); - public $files_template = array(); - public $inherit_root = ''; - - public $orig_tpl_inherits_id; - - /** - * @var string phpBB root path - */ - private $phpbb_root_path; - - /** - * @var phpEx PHP file extension - */ - private $phpEx; - - /** - * @var phpbb_config phpBB config instance - */ - private $config; - - /** - * @var user current user - */ - private $user; - - /** - * Constructor. - * - * @param string $phpbb_root_path phpBB root path - * @param user $user current user - */ - public function __construct($phpbb_root_path, $phpEx, $config, $user) - { - $this->phpbb_root_path = $phpbb_root_path; - $this->phpEx = $phpEx; - $this->config = $config; - $this->user = $user; - } - - /** - * Set template location - */ - public function set_template() - { - $template_path = $this->user->theme['template_path']; - if (file_exists($this->phpbb_root_path . 'styles/' . $template_path . '/template')) - { - $this->root = $this->phpbb_root_path . 'styles/' . $template_path . '/template'; - $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $template_path) . '_'; - - if ($this->orig_tpl_inherits_id === null) - { - $this->orig_tpl_inherits_id = $this->user->theme['template_inherits_id']; - } - - $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - - if ($this->user->theme['template_inherits_id']) - { - $this->inherit_root = $this->phpbb_root_path . 'styles/' . $this->user->theme['template_inherit_path'] . '/template'; - } - } - else - { - trigger_error('Template path could not be found: styles/' . $template_path . '/template', E_USER_ERROR); - } - - $this->context = new phpbb_template_context(); - - return true; - } - - /** - * Set custom template location (able to use directory outside of phpBB). - * - * Note: Templates are still compiled to phpBB's cache directory. - * - * @param string $template_path Path to template directory - * @param string $template_name Name of template - * @param string $fallback_template_path Path to fallback template - */ - public function set_custom_template($template_path, $template_name, $fallback_template_path = false) - { - // Make sure $template_path has no ending slash - if (substr($template_path, -1) == '/') - { - $template_path = substr($template_path, 0, -1); - } - - $this->root = $template_path; - $this->cachepath = $this->phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_'; - - if ($fallback_template_path !== false) - { - if (substr($fallback_template_path, -1) == '/') - { - $fallback_template_path = substr($fallback_template_path, 0, -1); - } - - $this->inherit_root = $fallback_template_path; - $this->orig_tpl_inherits_id = true; - } - else - { - $this->orig_tpl_inherits_id = false; - } - - $this->context = new phpbb_template_context(); - - return true; - } - - /** - * Sets the template filenames for handles. $filename_array - * should be a hash of handle => filename pairs. - * @param array $filname_array Should be a hash of handle => filename pairs. - */ - public function set_filenames(array $filename_array) - { - foreach ($filename_array as $handle => $filename) - { - if (empty($filename)) - { - trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR); - } - - $this->filename[$handle] = $filename; - $this->files[$handle] = $this->root . '/' . $filename; - - if ($this->inherit_root) - { - $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; - } - } - - return true; - } - - /** - * Clears all variables and blocks assigned to this template. - */ - public function destroy() - { - $this->context->clear(); - } - - /** - * Reset/empty complete block - * @param string $blockname Name of block to destroy - */ - public function destroy_block_vars($blockname) - { - $this->context->destroy_block_vars($blockname); - } - - /** - * Display handle - * @param string $handle Handle to display - * @param bool $include_once Allow multiple inclusions - * @return bool True on success, false on failure - */ - public function display($handle, $include_once = true) - { - $result = $this->call_hook($handle, $include_once); - if ($result !== false) - { - return $result[0]; - } - - $renderer = $this->_tpl_load($handle); - - if ($renderer) - { - $renderer->render($this->context, $this->get_lang()); - return true; - } - else - { - return false; - } - } - - /** - * Calls hook if any is defined. - * @param string $handle Template handle being displayed. - * @param bool $include_once Allow multiple inclusions - */ - private function call_hook($handle, $include_once) - { - global $phpbb_hook; - - if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) - { - if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__))) - { - $result = $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__)); - return array($result); - } - } - - return false; - } - - /** - * Obtains language array. - * This is either lang property of $user property, or if - * it is not set an empty array. - * @return array language entries - */ - public function get_lang() - { - if (isset($this->user->lang)) - { - $lang = $this->user->lang; - } - else - { - $lang = array(); - } - return $lang; - } - - /** - * Display the handle and assign the output to a template variable or return the compiled result. - * @param string $handle Handle to operate on - * @param string $template_var Template variable to assign compiled handle to - * @param bool $return_content If true return compiled handle, otherwise assign to $template_var - * @param bool $include_once Allow multiple inclusions of the file - * @return bool|string false on failure, otherwise if $return_content is true return string of the compiled handle, otherwise return true - */ - public function assign_display($handle, $template_var = '', $return_content = true, $include_once = false) - { - ob_start(); - $result = $this->display($handle, $include_once); - $contents = ob_get_clean(); - if ($result === false) - { - return false; - } - - if ($return_content) - { - return $contents; - } - - $this->assign_var($template_var, $contents); - - return true; - } - - /** - * Obtains a template renderer for a template identified by specified - * handle. The template renderer can display the template later. - * - * Template source will first be compiled into php code. - * If template cache is writable the compiled php code will be stored - * on filesystem and template will not be subsequently recompiled. - * If template cache is not writable template source will be recompiled - * every time it is needed. DEBUG_EXTRA define and load_tplcompile - * configuration setting may be used to force templates to be always - * recompiled. - * - * Returns an object implementing phpbb_template_renderer, or null - * if template loading or compilation failed. Call render() on the - * renderer to display the template. This will result in template - * contents sent to the output stream (unless, of course, output - * buffering is in effect). - * - * @param string $handle Handle of the template to load - * @return phpbb_template_renderer Template renderer object, or null on failure - * @uses template_compile is used to compile template source - */ - private function _tpl_load($handle) - { - if (!isset($this->filename[$handle])) - { - trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); - } - - // reload this setting to have the values they had when this object was initialised - // using set_template or set_custom_template, they might otherwise have been overwritten - // by other template class instances in between. - $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - - $compiled_path = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; - $this->files_template[$handle] = (isset($this->user->theme['template_id'])) ? $this->user->theme['template_id'] : 0; - - $recompile = defined('DEBUG_EXTRA') || - !file_exists($compiled_path) || - @filesize($compiled_path) === 0 || - ($this->config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($this->files[$handle])); - - if (!$recompile && $this->config['load_tplcompile']) - { - // No way around it: we need to check inheritance here - if ($this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) - { - $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $this->user->theme['template_inherits_id']; - } - $recompile = (@filemtime($compiled_path) < @filemtime($this->files[$handle])) ? true : false; - } - - // Recompile page if the original template is newer, otherwise load the compiled version - if (!$recompile) - { - return new phpbb_template_renderer_include($compiled_path, $this); - } - - // Inheritance - we point to another template file for this one. - if (isset($this->user->theme['template_inherits_id']) && $this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) - { - $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $this->user->theme['template_inherits_id']; - } - - $source_file = $this->_source_file_for_handle($handle); - - $compile = new phpbb_template_compile(); - - $output_file = $this->_compiled_file_for_handle($handle); - if ($compile->compile_file_to_file($source_file, $output_file) !== false) - { - $renderer = new phpbb_template_renderer_include($output_file, $this); - } - else if (($code = $compile->compile_file($source_file)) !== false) - { - $renderer = new phpbb_template_renderer_eval($code, $this); - } - else - { - $renderer = null; - } - - return $renderer; - } - - /** - * Resolves template handle $handle to source file path. - * @param string $handle Template handle (i.e. "friendly" template name) - * @return string Source file path - */ - private function _source_file_for_handle($handle) - { - // If we don't have a file assigned to this handle, die. - if (!isset($this->files[$handle])) - { - trigger_error("_source_file_for_handle(): No file specified for handle $handle", E_USER_ERROR); - } - - $source_file = $this->files[$handle]; - - // Try and open template for reading - if (!file_exists($source_file)) - { - trigger_error("_source_file_for_handle(): File $source_file does not exist", E_USER_ERROR); - } - return $source_file; - } - - /** - * Determines compiled file path for handle $handle. - * @param string $handle Template handle (i.e. "friendly" template name) - * @return string Compiled file path - */ - private function _compiled_file_for_handle($handle) - { - $compiled_file = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; - return $compiled_file; - } - - /** - * Assign key variable pairs from an array - * @param array $vararray A hash of variable name => value pairs - */ - public function assign_vars(array $vararray) - { - foreach ($vararray as $key => $val) - { - $this->assign_var($key, $val); - } - } - - /** - * Assign a single variable to a single key - * @param string $varname Variable name - * @param string $varval Value to assign to variable - */ - public function assign_var($varname, $varval) - { - $this->context->assign_var($varname, $varval); - } - - // Docstring is copied from phpbb_template_context method with the same name. - /** - * Assign key variable pairs from an array to a specified block - * @param string $blockname Name of block to assign $vararray to - * @param array $vararray A hash of variable name => value pairs - */ - public function assign_block_vars($blockname, array $vararray) - { - return $this->context->assign_block_vars($blockname, $vararray); - } - - // Docstring is copied from phpbb_template_context method with the same name. - /** - * Change already assigned key variable pair (one-dimensional - single loop entry) - * - * An example of how to use this function: - * {@example alter_block_array.php} - * - * @param string $blockname the blockname, for example 'loop' - * @param array $vararray the var array to insert/add or merge - * @param mixed $key Key to search for - * - * array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position] - * - * int: Position [the position to change or insert at directly given] - * - * If key is false the position is set to 0 - * If key is true the position is set to the last entry - * - * @param string $mode Mode to execute (valid modes are 'insert' and 'change') - * - * If insert, the vararray is inserted at the given position (position counting from zero). - * If change, the current block gets merged with the vararray (resulting in new key/value pairs be added and existing keys be replaced by the new value). - * - * Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array) - * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars) - * - * @return bool false on error, true on success - */ - public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert') - { - return $this->context->alter_block_array($blockname, $vararray, $key, $mode); - } - - /** - * Include a separate template - * @param string $filename Template filename to include - * @param bool $include True to include the file, false to just load it - * @uses template_compile is used to compile uncached templates - */ - public function _tpl_include($filename, $include = true) - { - $handle = $filename; - $this->filename[$handle] = $filename; - $this->files[$handle] = $this->root . '/' . $filename; - if ($this->inherit_root) - { - $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; - } - - $renderer = $this->_tpl_load($handle); - - if ($renderer) - { - $renderer->render($this->context, $this->get_lang()); - } - else - { - // trigger_error cannot be used here, as the output already started - echo 'template->_tpl_include(): Failed including ' . htmlspecialchars($handle) . "\n"; - } - } - - /** - * Include a php-file - */ - public function _php_include($filename) - { - if (is_absolute($filename)) - { - $file = $filename; - } - else - { - $file = $this->phpbb_root_path . $filename; - } - - if (!file_exists($file)) - { - // trigger_error cannot be used here, as the output already started - echo 'template->_php_include(): File ' . htmlspecialchars($file) . " does not exist\n"; - return; - } - include($file); - } -} -- cgit v1.2.1