Orion PHP  0.11.12
The PHP5.3 framework
template.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * Orion Template Controller class.
00005  *
00006  * Extend this class to create a new controller.
00007  *
00008  * @author Thibaut Despoulain
00009  * @license BSD 4-clauses
00010  * @version 0.11.12
00011  */
00012 
00013 namespace Orion\Core\Controller;
00014 
00015 use \Orion\Core;
00016 
00017 abstract class Template extends Core\Controller
00018 {
00019 
00020     /**
00021      * Template renderer (OrionTemplate::SMARTY by default). Can be overriden in the module
00022      *
00023      * @var string
00024      */
00025     protected $renderer = Core\Renderer::DEFAULT_RENDERER;
00026 
00027     /**
00028      * Template renderer instance
00029      * @var object
00030      */
00031     protected $tpl = null;
00032 
00033     /**
00034      * Base template name, used as a parent template for local views.
00035      * Set this to null if view have no parent template.
00036      * @var string
00037      */
00038     protected $template = null;
00039 
00040     /**
00041      * Main module function, executed right after module loading by Orion.
00042      * Handles route parsing and function callbacks.
00043      */
00044     public function load()
00045     {
00046         if ( $this->tpl == null )
00047             $this->setRenderer( $this->renderer );
00048 
00049         if ( $this->template == null )
00050             $this->setTemplate( Core\Context::getDefaultTemplate() );
00051         else
00052             $this->setTemplate( $this->template );
00053 
00054         if ( $this->route == null )
00055             throw new Core\Exception( 'No route object found in module.', E_USER_ERROR, get_class( $this ) );
00056 
00057         $function = $this->route->decode();
00058 
00059         if ( Core\Tools::startWith( $function->getName(), '__' ) )
00060             throw new Core\Exception( 'Trying to access a resticted function.', E_USER_ERROR, get_class( $this ) );
00061 
00062         if ( Core\Tools::startWith( $function->getName(), self::FUNCTION_PREFIX ) )
00063             throw new Core\Exception( 'Function name in rule must be declared without function prefix ' . self::FUNCTION_PREFIX . '.', E_USER_ERROR, get_class( $this ) );
00064 
00065         if ( !is_callable( array( $this, self::FUNCTION_PREFIX . $function->getName() ) ) )
00066             Core\Context::redirect( 404 );
00067 
00068         Core\Tools::callClassMethod( $this, self::FUNCTION_PREFIX . $function->getName(), $function->getArgs() );
00069     }
00070 
00071     /**
00072      * assigns a Smarty variable.
00073      *
00074      * @param array|string $block the template variable name(s)
00075      * @param mixed $value the value to assign
00076      * @param boolean $nocache if true any output of this variable will be not cached
00077      */
00078     protected function assign( $block, $content, $nocache=false )
00079     {
00080         if ( $this->tpl == null )
00081             $this->setRenderer( $this->renderer );
00082         $this->tpl->assign( $block, $content, $nocache );
00083     }
00084 
00085     /**
00086      * Clears cache of given view
00087      * @param string $view
00088      * @param string $id 
00089      */
00090     protected function clearCache( $view, $id=null )
00091     {
00092         try
00093         {
00094             $filename = Core\Context::$PATH . \Orion::MODULE_PATH . $this->name . DIRECTORY_SEPARATOR . $view . \Orion::VIEW_EXT . '.tpl';
00095 
00096             $this->tpl->clearCache( $filename, $id );
00097         }
00098         catch ( Core\Exception $e )
00099         {
00100             throw $e;
00101         }
00102         catch ( \Exception $e )
00103         {
00104             throw new Core\Exception( $e->getMessage(), E_USER_WARNING, $this->name );
00105         }
00106     }
00107 
00108     /**
00109      * Shortcuts the includeCSS method of the renderer, using a relative file path.
00110      * @param string $file The CSS file path, relative to the module directory
00111      */
00112     protected function includeCSS( $file )
00113     {
00114         $filename = Core\Context::getModuleAbsolutePath() . $file;
00115         $this->tpl->includeCSS( $filename );
00116     }
00117 
00118     /**
00119      * Displays template (shortlink for $this->tpl->display(...)).
00120      * @deprecated Use displayView() and local views instead
00121      * @param string $template the resource handle of the template file  or template object
00122      * @param mixed $id cache id to be used with this template
00123      */
00124     protected function render( $template, $id=null, $compile_id=null )
00125     {
00126         $this->tpl->render( $template, $id, $compile_id );
00127     }
00128 
00129     /**
00130      * Displays view (shortlink for $this->tpl->display('file:'.$local_view_file)).
00131      *
00132      * @param string $view The name of the view file to display, without any extension or path. For example, to load local index.view.tpl, use $this->displayView('index').
00133      * @param mixed $cache_id cache id to be used with this template
00134      * @param mixed $compile_id compile id to be used with this template
00135      * @param object $parent next higher level of Smarty variables
00136      */
00137     protected function renderView( $view, $id=null, $compile_id=null )
00138     {
00139         try
00140         {
00141             $filename = Core\Context::$PATH . \Orion::MODULE_PATH . $this->name . DS . $view . '.tpl';
00142 
00143             if ( !file_exists( $filename ) )
00144                 throw new Core\Exception( 'View file [' . $filename . '] does not exist.', E_USER_WARNING, $this->name );
00145 
00146             $this->tpl->renderView( $filename, $this->template, $id, $compile_id );
00147         }
00148         catch ( Core\Exception $e )
00149         {
00150             throw $e;
00151         }
00152         catch ( \Exception $e )
00153         {
00154             throw new Core\Exception( $e->getMessage(), E_USER_WARNING, $this->name );
00155         }
00156     }
00157 
00158     /**
00159      * Tests if given view is cached
00160      * @param string $view
00161      * @param string $cache_id
00162      * @param string $template_id
00163      * @return boolean 
00164      */
00165     protected function isCached( $view, $cache_id=null, $template_id=null )
00166     {
00167         try
00168         {
00169             $filename = Core\Context::$PATH . \Orion::MODULE_PATH . $this->name . DS . $view . '.tpl';
00170 
00171             if ( !file_exists( $filename ) )
00172                 throw new Core\Exception( 'View file [' . $filename . '] does not exist.', E_USER_WARNING, $this->name );
00173 
00174             return $this->tpl->isViewCached( $filename, $this->template, $cache_id, $template_id );
00175         }
00176         catch ( Core\Exception $e )
00177         {
00178             throw $e;
00179         }
00180         catch ( \Exception $e )
00181         {
00182             throw new Core\Exception( $e->getMessage(), E_USER_WARNING, $this->name );
00183         }
00184     }
00185 
00186     /**
00187      * Get current template name
00188      * @return string
00189      */
00190     public function getTemplate()
00191     {
00192         return $this->template;
00193     }
00194 
00195     /**
00196      * Override current renderer and reset template variable
00197      * @param string $renderer
00198      */
00199     protected function setRenderer( $renderer )
00200     {
00201         $this->renderer = $renderer;
00202         $this->tpl = Core\Renderer::setRenderer( $renderer );
00203         if ( $this->template != null )
00204             $this->tpl->addTemplateDir( Core\Context::getTemplatePath( $this->template ) );
00205         $this->tpl->addTemplateDir( Core\Context::getModulePath() );
00206     }
00207 
00208     /**
00209      * Set a new template theme.
00210      * @param string $name 
00211      */
00212     public function setTemplate( $name )
00213     {
00214         if ( $name == $this->template )
00215             return false;
00216 
00217         if ( !file_exists( Core\Context::getTemplateFilePath( $name ) ) )
00218             throw new Core\Exception( 'Template not found in [' . Core\Context::getTemplateFilePath( $name ) . ']', E_USER_WARNING, $this->name );
00219 
00220         $this->template = $name;
00221 
00222         if ( $this->tpl != null )
00223             $this->tpl->addTemplateDir( Core\Context::getTemplatePath( $this->template ) );
00224     }
00225 
00226 }
00227 
00228 ?>