[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/template/ -> template.class.php (source)

   1  <?php
   2  
   3      /**
   4       * \defgroup Template
   5       *
   6       * The Template module provides classes for dealing with templates, which are built on
   7       * top of Smarty. 
   8       *
   9       * It also provides method for managing the cache, template validation sandboxes and for
  10       * dealing with template sets.
  11       *
  12       * @see TemplateService
  13       * @see Template
  14       * @see TemplateSets
  15       * @see Menu
  16       * @see MenuRenderer
  17       */
  18  
  19      lt_include( PLOG_CLASS_PATH.'class/template/smarty/Smarty.class.php' );
  20      lt_include( PLOG_CLASS_PATH.'class/config/config.class.php' );
  21      lt_include( PLOG_CLASS_PATH.'class/file/file.class.php' );
  22  
  23      // template load order constants
  24      define( "TEMPLATE_LOAD_ORDER_DEFAULT_FIRST", 1 );    
  25      define( "TEMPLATE_LOAD_ORDER_USER_FIRST", 2 );
  26      
  27      // name of the folder where the default template is stored
  28      define( "DEFAULT_TEMPLATE_FOLDER", "default" );
  29      
  30      // Smarty dynamic block function
  31  	function smarty_block_dynamic($param, $content, &$smarty) {
  32          return $content;
  33      }    
  34  
  35      /**
  36       * \ingroup Template
  37       * 
  38       * Wrapper around the Smarty class, inspired by the article
  39       * http://zend.com/zend/tut/tutorial-stump.php
  40       *
  41       * This class provides additional methods and initial values for the original Smarty
  42       * class, and reimplements the methods Smarty::fetch() and Smarty::display() so that they do 
  43       * not need an extra parameter.
  44       *
  45       * It is not recommended to create instances of this class directly but instead, use the factory
  46       * TemplateService which is able to generate different types of Template objects with some pre-set
  47       * values. The TemplateService class can also deal with cached and non-cached templates.
  48       *
  49       * @see TemplateService
  50       * @see CachedTemplate
  51       */
  52      class Template extends Smarty 
  53      {
  54  
  55          var $_templateFile = null;
  56          
  57          // logger object
  58          var $log = null;
  59  
  60          // whether to use the template load order settings
  61          var $useTemplateLoadOrder = false;
  62  
  63          /**
  64           * Constructor. 
  65           *
  66           * @param templateFile Complete path to the template file we are going to render
  67           */
  68          function Template( $templateFile )
  69          {
  70              // create the Smarty object and set the security values
  71              $this->Smarty();
  72              $this->caching = false;
  73              //$this->cache_lifetime =  $cacheLifetime;
  74              $config =& Config::getConfig();
  75              $this->cache_dir    = $config->getValue( 'temp_folder' );
  76  
  77              $this->_templateFile = $templateFile;            
  78  
  79              // enable the security settings
  80              $this->php_handling = false;
  81                  
  82              $this->security = (boolean)!$config->getValue( 'allow_php_code_in_templates', false );
  83              //$this->security = true;
  84              $this->secure_dir = Array( './imgs', './templates/admin', './templates/' );
  85  
  86              // default folders
  87              $this->compile_dir  = $config->getValue( 'temp_folder' );
  88              $this->template_dir = array( '.', $config->getValue( 'template_folder' ) );
  89              $this->config_dir   = $config->getValue( 'template_folder' );
  90              $this->compile_check = $config->getValue( 'template_compile_check', true );
  91              // this helps if php is running in 'safe_mode'
  92              $this->use_sub_dirs = false;
  93  
  94              // register dynamic block for every template instance
  95              $this->register_block('dynamic', 'smarty_block_dynamic', false);
  96          }
  97  
  98          /**
  99           * called for included templates
 100            * This has been reimplemented from Smarty::_smarty_include() so that we can define a set
 101           * of locations where template files can be located if the specified path and file do not exist.
 102           *
 103           * @see Smarty::_smarty_include
 104           * @private
 105           */
 106  	    function _smarty_include($params)
 107          {
 108              if( $this->useTemplateLoadOrder ) {
 109                  $config =& Config::getConfig();
 110                  $defaultTemplateFile = $config->getValue( "template_folder")."/".DEFAULT_TEMPLATE_FOLDER."/".basename( $params['smarty_include_tpl_file'] );
 111                  //print( "load order = ".$config->getValue( "template_load_order" ));                
 112                  if( $config->getValue( "template_load_order" ) == TEMPLATE_LOAD_ORDER_DEFAULT_FIRST ) {
 113                      // if the 'default' one should be included first, then check if it is available and if
 114                      // it is, go ahead. If it isn't then we'll just display an error
 115                      if( File::isReadable( $defaultTemplateFile )) {
 116                          $params['smarty_include_tpl_file'] = $defaultTemplateFile;
 117                      }
 118                  }
 119                  else {
 120                      // include the user's template unless it is not available, in which case we'll use the 
 121                      // default one
 122                      $readable = false;
 123                      foreach( $this->template_dir as $templateDir ) {                        
 124                          if( File::isReadable( $templateDir."/".$params['smarty_include_tpl_file'] )) {
 125                              $readable = true;
 126                              break;
 127                          }
 128                      }
 129                      if( !$readable ) {
 130                          // if the file wasn't found in any of the template folders, then we should be using the default one
 131                          $params['smarty_include_tpl_file'] = $defaultTemplateFile;                    
 132                      }
 133                  }
 134              }
 135              
 136              Smarty::_smarty_include( $params );
 137          }
 138  
 139          /**
 140           * By default templates are searched in the folder specified by the
 141           * template_folder configuration setting, but we can force Smarty to
 142           * look for those templates somewhere else. This method is obviously to be
 143           * used *before* rendering the template ;)
 144           *
 145           * @param templateFolder The new path where we'd like to search for templates
 146           * @return Returns always true.
 147           */
 148          function setTemplateDir( $templateDir )
 149          {
 150              $this->template_dir = array( '.', $templateDir );
 151  
 152              return true;
 153          }
 154  
 155          /**
 156           * Returns the name of the template file
 157           *
 158           * @return The name of the template file
 159           *
 160           * :TODO: 
 161           * This code could do with some refactoring, its' pretty similar to what we've got in Template::_smarty_include()
 162           */
 163          function getTemplateFile()
 164          {            
 165              if( $this->useTemplateLoadOrder ) {
 166                  $config =& Config::getConfig();
 167                  $defaultTemplateFile = $config->getValue( "template_folder")."/".DEFAULT_TEMPLATE_FOLDER."/".basename( $this->_templateFile );
 168                  if( $config->getValue( "template_load_order" ) == TEMPLATE_LOAD_ORDER_DEFAULT_FIRST ) {
 169                      if( File::isReadable( $defaultTemplateFile )) {
 170                          $this->_templateFile = $defaultTemplateFile;
 171                      }
 172                  }
 173                  else {                    
 174                      $readable = false;
 175                      foreach( $this->template_dir as $templateDir ) {                        
 176                          if( File::isReadable( $templateDir."/".$this->_templateFile )) {
 177                              $readable = true;
 178                              break;
 179                          }
 180                      }
 181                      if( !$readable ) {
 182                          // if the file wasn't found in any of the template folders, then we should be using the default one
 183                          $this->_templateFile = $defaultTemplateFile;
 184                      }
 185                  }                
 186              }
 187              
 188              return $this->_templateFile;
 189          }
 190  
 191          /**
 192           * Renders the template and returns the contents as an string
 193           *
 194           * @return The result as an string
 195           */
 196          function fetch()
 197          {
 198              return Smarty::fetch( $this->getTemplateFile());
 199          }
 200  
 201          /**
 202           * Displays the result of rendering the template
 203           *
 204           * @return I don't know :)
 205           */
 206          function display()
 207          {
 208              return Smarty::display( $this->getTemplateFile());
 209          }
 210          
 211          /**
 212           * the Template object is by default not cached
 213           *
 214           * @param viewId Not used
 215           * @return always false
 216           */
 217          function isCached( $viewId )
 218          {
 219              return false;
 220          }
 221      }
 222  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics