[pLog-svn] r4430 - in plog/trunk: class/data/validator class/template locale templates/admin

oscar at devel.lifetype.net oscar at devel.lifetype.net
Mon Dec 25 14:20:50 GMT 2006


Author: oscar
Date: 2006-12-25 14:20:49 +0000 (Mon, 25 Dec 2006)
New Revision: 4430

Modified:
   plog/trunk/class/data/validator/templatesetvalidator.class.php
   plog/trunk/class/template/cachedtemplate.class.php
   plog/trunk/class/template/template.class.php
   plog/trunk/class/template/templateservice.class.php
   plog/trunk/locale/locale_en_UK.php
   plog/trunk/templates/admin/globalsettings_templates.template
Log:
This is my Christmas present to you all :-) "partial" templates, whereby users can now create templates that only include the changed files and everything else, if not found in the user template, is loaded from the templates/default/. If the template file does not exist then a normal Smarty error will be shown.

The way this feature works is controlled by a new configuration setting: template_load_order which can have two values. The first value forces the Template class to first look in the templates/default/ folder looking for the requested template and if not available, then loads the file from the user's template folder. The second value works the other way around, loading from the user first and if the file cannot be found then loading from the default folder.

This feature also allows for site-wide commentform.template, which in addition cannot be overriden by users. There's also lots of other applications of this feature, hopefully people will find it useful.


Modified: plog/trunk/class/data/validator/templatesetvalidator.class.php
===================================================================
--- plog/trunk/class/data/validator/templatesetvalidator.class.php	2006-12-24 04:04:40 UTC (rev 4429)
+++ plog/trunk/class/data/validator/templatesetvalidator.class.php	2006-12-25 14:20:49 UTC (rev 4430)
@@ -40,16 +40,9 @@
         var $_fullName;
 
         // these are the basic files that should be present in every
-        // template set
-        var $_basicFiles = Array( 'main.template',
-                                  'postandcomments.template',
-                                  'commentarticle.template',
-                                  'posttrackbacks.template',
-                                  'error.template',
-								  'album.template',
-								  'albums.template',
-								  'resource.template'
-                                 );
+        // template set. This array is now empty since Lifetype 1.2 supports "partial" templates
+		// that only provide the needed files while everything else loaded from templates/default/
+        var $_basicFiles = Array();
 
 
     	function TemplateSetValidator( $templateName, $folder )
@@ -84,4 +77,4 @@
             return true;
         }
     }
-?>
+?>
\ No newline at end of file

Modified: plog/trunk/class/template/cachedtemplate.class.php
===================================================================
--- plog/trunk/class/template/cachedtemplate.class.php	2006-12-24 04:04:40 UTC (rev 4429)
+++ plog/trunk/class/template/cachedtemplate.class.php	2006-12-25 14:20:49 UTC (rev 4430)
@@ -45,7 +45,7 @@
          */
         function fetch( $cacheId )
         {
-        	return Smarty::fetch( $this->_templateFile, $cacheId );
+        	return Smarty::fetch( $this->getTemplateFile(), $cacheId );
         }
 
 		/**
@@ -56,7 +56,7 @@
 		 */
 		function isCached( $cacheId )
 		{
-			$isCached = $this->is_cached( $this->_templateFile, $cacheId );
+			$isCached = $this->is_cached( $this->getTemplateFile(), $cacheId );
 			
 			return $isCached;
 		}

Modified: plog/trunk/class/template/template.class.php
===================================================================
--- plog/trunk/class/template/template.class.php	2006-12-24 04:04:40 UTC (rev 4429)
+++ plog/trunk/class/template/template.class.php	2006-12-25 14:20:49 UTC (rev 4430)
@@ -18,7 +18,15 @@
 
     lt_include( PLOG_CLASS_PATH.'class/template/smarty/Smarty.class.php' );
     lt_include( PLOG_CLASS_PATH.'class/config/config.class.php' );
+    lt_include( PLOG_CLASS_PATH.'class/file/file.class.php' );
+
+	// template load order constants
+	define( "TEMPLATE_LOAD_ORDER_DEFAULT_FIRST", 1 );	
+	define( "TEMPLATE_LOAD_ORDER_USER_FIRST", 2 );
 	
+	// name of the folder where the default template is stored
+	define( "DEFAULT_TEMPLATE_FOLDER", "default" );
+	
 	// Smarty dynamic block function
 	function smarty_block_dynamic($param, $content, &$smarty) {
     	return $content;
@@ -49,6 +57,9 @@
         // logger object
         var $log = null;
 
+		// whether to use the template load order settings
+		var $useTemplateLoadOrder = false;
+
         /**
          * Constructor. 
          *
@@ -56,11 +67,6 @@
          */
         function Template( $templateFile )
         {
-            // initialize logging
-            // :TODO: we need define a global logging switch like logging_enabled
-	        // lt_include( PLOG_CLASS_PATH.'class/logger/loggermanager.class.php' );
-            // $this->log =& LoggerManager::getLogger( "default" );
-
             // create the Smarty object and set the security values
             $this->Smarty();
             $this->caching = false;
@@ -72,11 +78,6 @@
 
             // enable the security settings
             $this->php_handling = false;
-            // code is not allowed in the templates by default, unless specified otherwise
-            /*if( $config->getValue( 'allow_php_code_in_templates', false ))
-            	$this->security = true;
-            else
-            	$this->security = false;*/
             	
             $this->security = (boolean)!$config->getValue( 'allow_php_code_in_templates', false );
             //$this->security = true;
@@ -91,9 +92,42 @@
             $this->use_sub_dirs = false;
 
 			// register dynamic block for every template instance
-			$this->register_block('dynamic', 'smarty_block_dynamic', false);            
+			$this->register_block('dynamic', 'smarty_block_dynamic', false);
         }
 
+	    /**
+	     * called for included templates
+	 	 * This has been reimplemented from Smarty::_smarty_include() so that we can define a set
+	     * of locations where template files can be located if the specified path and file do not exist.
+	     *
+		 * @see Smarty::_smarty_include
+		 * @private
+	     */
+	    function _smarty_include($params)
+	    {
+			if( $this->useTemplateLoadOrder ) {
+				$config =& Config::getConfig();
+				$defaultTemplateFile = $config->getValue( "template_folder")."/".DEFAULT_TEMPLATE_FOLDER."/".basename( $params['smarty_include_tpl_file'] );
+				//print( "load order = ".$config->getValue( "template_load_order" ));				
+				if( $config->getValue( "template_load_order" ) == TEMPLATE_LOAD_ORDER_DEFAULT_FIRST ) {
+					// if the 'default' one should be included first, then check if it is available and if
+					// it is, go ahead. If it isn't then we'll just display an error
+					if( File::isReadable( $defaultTemplateFile )) {
+						$params['smarty_include_tpl_file'] = $defaultTemplateFile;
+					}
+				}
+				else {
+					// include the user's template unless it is not available, in which case we'll use the 
+					// default one
+					if( !File::isReadable( $params['smarty_include_tpl_file'] )) {
+						$params['smarty_include_tpl_file'] = $defaultTemplateFile;
+					}					
+				}
+			}
+			
+			Smarty::_smarty_include( $params );
+	    }
+
         /**
          * By default templates are searched in the folder specified by the
          * template_folder configuration setting, but we can force Smarty to
@@ -116,7 +150,22 @@
          * @return The name of the template file
          */
         function getTemplateFile()
-        {
+        {			
+			if( $this->useTemplateLoadOrder ) {
+				$config =& Config::getConfig();
+				$defaultTemplateFile = $config->getValue( "template_folder")."/".DEFAULT_TEMPLATE_FOLDER."/".basename( $this->_templateFile );
+				if( $config->getValue( "template_load_order" ) == TEMPLATE_LOAD_ORDER_DEFAULT_FIRST ) {
+					if( File::isReadable( $defaultTemplateFile )) {
+						$this->_templateFile = $defaultTemplateFile;
+					}
+				}
+				else {
+					if( !File::isReadable( $config->getValue( "template_folder" )."/".$this->_templateFile )) {
+						$this->_templateFile = $defaultTemplateFile;
+					}
+				}				
+			}
+			
             return $this->_templateFile;
         }
 
@@ -127,7 +176,7 @@
          */
         function fetch()
         {
-            return Smarty::fetch( $this->_templateFile );
+            return Smarty::fetch( $this->getTemplateFile());
         }
 
         /**
@@ -137,7 +186,7 @@
          */
         function display()
         {
-            return Smarty::display( $this->_templateFile );
+            return Smarty::display( $this->getTemplateFile());
         }
         
         /**
@@ -151,4 +200,4 @@
             return false;
         }
     }
-?>
+?>
\ No newline at end of file

Modified: plog/trunk/class/template/templateservice.class.php
===================================================================
--- plog/trunk/class/template/templateservice.class.php	2006-12-24 04:04:40 UTC (rev 4429)
+++ plog/trunk/class/template/templateservice.class.php	2006-12-25 14:20:49 UTC (rev 4430)
@@ -143,7 +143,7 @@
 
 			$t = $this->Template( $templateName, 'admin', $blogInfo );
 			$t->assign( 'admintemplatepath', TemplateSetStorage::getAdminTemplateFolder());
-
+			
 			return $t;
 		}
 
@@ -182,6 +182,8 @@
 
 			// change a few things...
 			$t = $this->_configureTemplateSettings( $t, $blogInfo );
+			
+			$t->useTemplateLoadOrder = true;
 
             return $t;
         }
@@ -254,6 +256,8 @@
 
 			// change a few things...
 			$t = $this->_configureTemplateSettings( $t, $blogInfo );
+			
+			
 
             return $t;
 		}
@@ -315,4 +319,4 @@
 			return $t;
 		}
     }
-?>
+?>
\ No newline at end of file

Modified: plog/trunk/locale/locale_en_UK.php
===================================================================
--- plog/trunk/locale/locale_en_UK.php	2006-12-24 04:04:40 UTC (rev 4429)
+++ plog/trunk/locale/locale_en_UK.php	2006-12-25 14:20:49 UTC (rev 4430)
@@ -1217,4 +1217,8 @@
 
 $messages['error_resource_not_whitelisted_extension'] = 'The type of the file is not one of the allowed ones.';
 $messages['help_upload_allowed_files'] = 'Space-separated list of file types that are allowed to be uploaded. Usage of \'*\' and \'?\' is allowed. If both upload_forbidden_file and this option are set, the whitelist (upload_allowed_files) takes precedence over the blacklist [Default = None]';
+
+$messages['help_template_load_order'] = 'Defines in which order template files are searched and loaded. If using \'Load default templates first\', LifeType will try to find files first in the templates/default/ folder and if not available there, then it will load the user\'s template files. If the same template file exists in both places, the default one takes precedence. If set to \'Load user templates first\', user templates are always loaded first and if unavailable, then the default one is used. If the same template file exists in both places, the user template always takes precedence.';
+$messages['template_load_order_user_first'] = 'Load default templates first';
+$messages['template_load_order_default_first'] = 'Load user templates first';
 ?>
\ No newline at end of file

Modified: plog/trunk/templates/admin/globalsettings_templates.template
===================================================================
--- plog/trunk/templates/admin/globalsettings_templates.template	2006-12-24 04:04:40 UTC (rev 4429)
+++ plog/trunk/templates/admin/globalsettings_templates.template	2006-12-25 14:20:49 UTC (rev 4430)
@@ -68,4 +68,13 @@
     <div class="formHelp">{$locale->tr("help_http_cache_lifetime")}</div>
     <input style="width:100%" type="text" id="config[http_cache_lifetime]" name="config[http_cache_lifetime]" value="{$http_cache_lifetime}"/>
    </div>
+   <!-- template_load_order -->
+   <div class="field">
+    <label for="config[template_load_order]">template_load_order</label>
+    <div class="formHelp">{$locale->tr("help_template_load_order")}</div>
+     <select name="config[template_load_order]">
+       <option value="1" {if $template_load_order == 1} selected="selected" {/if} />{$locale->tr("template_load_order_user_first")}</option>
+       <option value="2" {if $template_load_order == 2} selected="selected" {/if} />{$locale->tr("template_load_order_default_first")}</option>
+     </select>
+   </div>
  </div>



More information about the pLog-svn mailing list