[PATCH] use realpath in templates_c/ and cache/

Hans Jürgen von Lengerke <[email protected]>
Newsgroups gmane.comp.php.smarty.devel
Message-ID <[email protected]>
Hello,

the attached patch adds the capability to use several template
directories together with one single compile and cache dir. This is done
by using the realpath to the template file instead of crc32 as a
compile/cache key. The patch adds a new, readily documented option to
smarty, $use_realpath, that is set to FALSE by default. Setting this to
TRUE will result in the described behaviour. This works also with
$use_sub_dirs set to FALSE, albeit this is not recommended because it
results in very long filenames.

I know there was discussion before whether this behaviour should be
added to Smarty and Messju said no because it would slow down 
performance 
(http://marc.theaimsgroup.com/?l=smarty-dev&m=106867297300354&w=2).
Well, for one I think this performance decrease is neglectible and since
this is configurable now one can just leave it off.

This patch is also helpful if you need to find compiled and cached files
quickly for debugging templates.

I hope this patch can be added. The patch is against current CVS
development version.

Hans

-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
patch.use_realpath.txt (text/plain, 4.6 KB)
Index: docs/en/programmers/api-variables.xml
===================================================================
RCS file: /repository/smarty/docs/en/programmers/api-variables.xml,v
retrieving revision 1.2
diff -u -r1.2 api-variables.xml
--- docs/en/programmers/api-variables.xml	13 Apr 2004 11:39:11 -0000	1.2
+++ docs/en/programmers/api-variables.xml	22 Apr 2004 20:58:37 -0000
@@ -37,6 +37,7 @@
 &programmers.api-variables.variable-error-reporting;
 &programmers.api-variables.variable-compile-id;
 &programmers.api-variables.variable-use-sub-dirs;
+&programmers.api-variables.variable-use-realpath;
 &programmers.api-variables.variable-default-modifiers;
 &programmers.api-variables.variable-default-resource-type;
 </chapter>
@@ -59,4 +60,4 @@
 vim600: syn=xml fen fdm=syntax fdl=2 si
 vim: et tw=78 syn=sgml
 vi: ts=1 sw=1
--->
\ No newline at end of file
+-->
Index: libs/Smarty.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty.class.php,v
retrieving revision 1.487
diff -u -r1.487 Smarty.class.php
--- libs/Smarty.class.php	16 Apr 2004 08:06:40 -0000	1.487
+++ libs/Smarty.class.php	22 Apr 2004 20:58:37 -0000
@@ -296,6 +296,17 @@
     var $use_sub_dirs          = false;
 
     /**
+     * This tells Smarty to use the full path to a template in the cache/
+     * and templates_c/ directories. This is useful if you want to use
+     * one single templates_c/ directory for different template
+     * directories because it avoids clashes when the filenames relative
+     * to two $include_dir are the same.
+     *
+     * @var boolean
+     */
+    var $use_realpath          = false;
+
+    /**
      * This is a list of the modifiers to apply to all template variables.
      * Put each modifier in a separate array element in the order you want
      * them applied. example: <code>array('escape:"htmlall"');</code>
@@ -1747,11 +1758,18 @@
         if(isset($auto_source)) {
             // make source name safe for filename
             $_filename = urlencode(basename($auto_source));
-            $_crc32 = crc32($auto_source) . $_compile_dir_sep;
-            // prepend %% to avoid name conflicts with
-            // with $params['auto_id'] names
-            $_crc32 = '%%' . substr($_crc32,0,3) . $_compile_dir_sep . '%%' . $_crc32;
-            $_return .= $_crc32 . $_filename;
+            if ($this->use_realpath) {
+                // use realpath to template file instead of crc32 generated directory
+                $_filename = realpath($this->template_dir) . DIRECTORY_SEPARATOR . $_filename;
+                $_filename = str_replace(DIRECTORY_SEPARATOR, $_compile_dir_sep, $_filename);
+                $_return .= $_filename;
+            } else {
+                $_crc32 = crc32($auto_source) . $_compile_dir_sep;
+                // prepend %% to avoid name conflicts with
+                // with $params['auto_id'] names
+                $_crc32 = '%%' . substr($_crc32,0,3) . $_compile_dir_sep . '%%' . $_crc32;
+                $_return .= $_crc32 . $_filename;
+            }
         }
 
         return $_return;
Index: libs/core/core.read_cache_file.php
===================================================================
RCS file: /repository/smarty/libs/core/core.read_cache_file.php,v
retrieving revision 1.16
diff -u -r1.16 core.read_cache_file.php
--- libs/core/core.read_cache_file.php	16 Apr 2004 08:02:04 -0000	1.16
+++ libs/core/core.read_cache_file.php	22 Apr 2004 20:58:37 -0000
@@ -27,8 +27,14 @@
         return false;
     }
 
-    if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) {
-        list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']];
+    $_content_cache_id = $params['tpl_file'].','.$params['cache_id'].','.$params['compile_id'];
+    if ($smarty->use_realpath) {
+        $_content_cache_file = $smarty->_get_auto_filename("", $params['tpl_file'], "");
+        $_content_cache_id = $_content_cache_file.','.$params['cache_id'].','.$params['compile_id'];
+    }
+
+    if (isset($content_cache[$_content_cache_id])) {
+        list($params['results'], $smarty->_cache_info) = $content_cache[$_content_cache_id];
         return true;
     }
 
@@ -100,7 +106,7 @@
         }
     }
     $params['results'] = $cache_split[1];
-    $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info);
+    $content_cache[$_content_cache_id] = array($params['results'], $_cache_info);
 
     $smarty->_cache_info = $_cache_info;
     return true;
docs.en.programmers.api-variables.variable-use-realpath.xml.txt (text/plain, 935 B)
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: $ -->
     <sect1 id="variable.use.realpath">
      <title>$use_realpath</title>
      <para>
   This tells Smarty to use the full path to a template in the cache/
   and templates_c/ directories. This is useful if you want to use
   one single templates_c/ directory for different template
   directories because it avoids clashes when the filenames relative
   to two $include_dir are the same.
      </para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.