diff --git a/Smarty.class.php b/Smarty.class.php index 53f2261b..d9d7a275 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -97,6 +97,7 @@ class Smarty var $cache_handler_func = null; // function used for cached content. this is // an alternative to using the built-in file // based caching. + var $check_if_modified = true; // respect If-Modified-Since headers on cached content var $default_template_handler_func = ''; // function to handle missing templates @@ -546,15 +547,26 @@ class Smarty $_smarty_results = $this->_process_cached_inserts($_smarty_results); } if ($_smarty_display) { - echo $_smarty_results; if ($this->debugging) { // capture time for debugging info $this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_get_microtime() - $debug_start_time; - echo $this->_generate_debug_output(); + $_smarty_results .= $this->_generate_debug_output(); } - return; + if( $this->check_if_modified ) { + global $HTTP_IF_MODIFIED_SINCE; + $last_modified_date = substr($HTTP_IF_MODIFIED_SINCE,0,strpos($HTTP_IF_MODIFIED_SINCE,'GMT')+3); + $gmt_mtime = gmdate('D, d M Y H:i:s', $this->_cache_info['timestamp']).' GMT'; + if( !$this->_cache_info['insert_tags'] + && $gmt_mtime == $last_modified_date ) { + header("HTTP/1.1 304 Not Modified"); + } + } + header("Content-Length: ".strlen($_smarty_results)); + header("Last-Modified: ".$gmt_mtime); + echo $_smarty_results; + return true; } else { return $_smarty_results; } diff --git a/docs.sgml b/docs.sgml index 17c46c6e..b081974c 100644 --- a/docs.sgml +++ b/docs.sgml @@ -1473,13 +1473,18 @@ function get_db_template ($tpl_name, &$tpl_source, &$tpl_timestamp, $get_source=true, &$smarty_obj) { if($get_source) { - // do database calls (or whatever) here to fetch your template, populating + // do database call here to fetch your template, populating // $tpl_source and $tpl_timestamp. - $tpl_source = "This is a simulation of a template fetched from a db."; - $tpl_timestamp = mktime(); + $sql = new SQL; + $sql->query("select tpl_source, tpl_timestamp from my_table + where tpl_name='$tpl_name'"); + extract($sql->record); } else { - // just populate $tpl_timestamp. - $tpl_timestamp = mktime(); + // do database call here to populate $tpl_timestamp. + $sql = new SQL; + $sql->query("select tpl_timestamp from my_table + where tpl_name='$tpl_name'"); + extract($sql->record); } return true; diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 53f2261b..d9d7a275 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -97,6 +97,7 @@ class Smarty var $cache_handler_func = null; // function used for cached content. this is // an alternative to using the built-in file // based caching. + var $check_if_modified = true; // respect If-Modified-Since headers on cached content var $default_template_handler_func = ''; // function to handle missing templates @@ -546,15 +547,26 @@ class Smarty $_smarty_results = $this->_process_cached_inserts($_smarty_results); } if ($_smarty_display) { - echo $_smarty_results; if ($this->debugging) { // capture time for debugging info $this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_get_microtime() - $debug_start_time; - echo $this->_generate_debug_output(); + $_smarty_results .= $this->_generate_debug_output(); } - return; + if( $this->check_if_modified ) { + global $HTTP_IF_MODIFIED_SINCE; + $last_modified_date = substr($HTTP_IF_MODIFIED_SINCE,0,strpos($HTTP_IF_MODIFIED_SINCE,'GMT')+3); + $gmt_mtime = gmdate('D, d M Y H:i:s', $this->_cache_info['timestamp']).' GMT'; + if( !$this->_cache_info['insert_tags'] + && $gmt_mtime == $last_modified_date ) { + header("HTTP/1.1 304 Not Modified"); + } + } + header("Content-Length: ".strlen($_smarty_results)); + header("Last-Modified: ".$gmt_mtime); + echo $_smarty_results; + return true; } else { return $_smarty_results; }