Source for file Smarty3.php

Documentation is available at Smarty3.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Smarty3.php
  5.  *
  6.  *  @author     Sotaro Karasawa <sotaro.k@gmail.com>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: 92f2d101591a2c7b61fc5674f9b75c9b3eb6c3fa $
  10.  */
  11.  
  12. // {{{ Ethna_Renderer_Smarty3
  13. /**
  14.  *  Smarty 3.x
  15.  *
  16.  *  @author     Sotaro Karasawa <sotaro.k@gmail.com>
  17.  *  @package    Ethna
  18.  */
  19. {
  20.     /** @private    string compile directory  */
  21.     private $compile_dir;
  22.  
  23.     /** @protected  string path of smarty3 */
  24.     protected $engine_path = 'Smarty3/Smarty.class.php';
  25.  
  26.     /**
  27.      *  Constructor for Ethna_Renderer_Smarty3
  28.      *
  29.      *  @access public
  30.      */
  31.     public function __construct($controller)
  32.     {
  33.         parent::__construct($controller);
  34.  
  35.         // get renderer config
  36.         $smarty_config = isset($this->config['smarty3'])
  37.             ? $this->config['smarty3']
  38.             : array();
  39.         $this->loadEngine($smarty_config);
  40.  
  41.         $this->engine = new Smarty();
  42.  
  43.         // Configurerd by controller
  44.         $template_dir $controller->getTemplatedir();
  45.         $compile_dir $controller->getDirectory('template_c');
  46.  
  47.         $this->setTemplateDir($template_dir);
  48.         $this->compile_dir $compile_dir;
  49.  
  50.         $this->engine->template_dir $template_dir;
  51.         $this->engine->compile_dir $compile_dir;
  52.         $this->engine->compile_id md5($this->template_dir);
  53.  
  54.         if (isset($smarty_config['left_delimiter'])) {
  55.             $this->engine->left_delimiter $smarty_config['left_delimiter'];
  56.         }
  57.         if (isset($smarty_config['right_delimiter'])) {
  58.             $this->engine->right_delimiter $smarty_config['right_delimiter'];
  59.         }
  60.  
  61.         // make compile dir
  62.         if (is_dir($this->engine->compile_dir=== false{
  63.             Ethna_Util::mkdir($this->engine->compile_dir0755);
  64.         }
  65.  
  66.         $this->engine->plugins_dir array_merge(
  67.             $controller->getDirectory('plugins'),
  68.             array(ETHNA_BASE '/class/Plugin/Smarty'SMARTY_DIR 'plugins')
  69.         );
  70.     }
  71.  
  72.     /**
  73.      *  Display the template
  74.      *
  75.      *  @param  string  $template   template name
  76.      *  @param  bool    $capture    if true, not display but return as string
  77.      *
  78.      *  @access public
  79.      */
  80.     public function perform($template null$capture false)
  81.     {
  82.         if ($template === null && $this->template === null{
  83.             return Ethna::raiseWarning('template is not defined');
  84.         }
  85.  
  86.         if ($template !== null{
  87.             $this->template = $template;
  88.         }
  89.  
  90.         try {
  91.             if ((is_absolute_path($this->template&& is_readable($this->template))
  92.                 || is_readable($this->template_dir . $this->template)) {
  93.                     if ($capture === true{
  94.                         $captured $this->engine->fetch($this->template);
  95.                         return $captured;
  96.                     else {
  97.                         $this->engine->display($this->template);
  98.                     }
  99.             else {
  100.                 return Ethna::raiseWarning('template not found ' $this->template);
  101.             }
  102.         catch (SmartyCompilerException $e{
  103.             return Ethna::raiseWarning("smarty compile error: msg='{$e->getMessage()}'"500);
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * get tamplate variable
  109.      *
  110.      *  @param      string      $name  variable name
  111.      *  @return     mixed       variables
  112.      *  @access     public
  113.      */
  114.     public function getProp($name null)
  115.     {
  116.         $property $this->engine->get_template_vars($name);
  117.  
  118.         if ($property !== null{
  119.             return $property;
  120.         }
  121.         return null;
  122.     }
  123.  
  124.     /**
  125.      *  remove template variable
  126.      *
  127.      *  @param  name    variable name
  128.      *
  129.      *  @access public
  130.      */
  131.     public function removeProp($name)
  132.     {
  133.         $this->engine->clear_assign($name);
  134.     }
  135.  
  136.     /**
  137.      *  set array to template variable
  138.      *
  139.      *  @param  array   $array 
  140.      *
  141.      *  @access public
  142.      */
  143.     public function setPropArray($array)
  144.     {
  145.         $this->engine->assign($array);
  146.     }
  147.  
  148.     /**
  149.      *  set array to template variable by reference
  150.      *
  151.      *  @param  array   $array 
  152.      *  @access public
  153.      */
  154.     public function setPropArrayByRef(&$array)
  155.     {
  156.         $this->engine->assignByRef($array);
  157.     }
  158.  
  159.     /**
  160.      *  set template variable
  161.      *
  162.      *  @param  string  $name   variable name
  163.      *  @param  mixed   $value  value
  164.      *
  165.      *  @access public
  166.      */
  167.     public function setProp($name$value)
  168.     {
  169.         $this->engine->assign($name$value);
  170.     }
  171.  
  172.     /**
  173.      *  set template variable by reference
  174.      *
  175.      *  @param  string  $name   variable name
  176.      *  @param  mixed   $value  value
  177.      *
  178.      *  @access public
  179.      */
  180.     public function setPropByRef($name&$value)
  181.     {
  182.         $this->engine->assignByRef($name$value);
  183.     }
  184.  
  185.     /**
  186.      *  プラグインをセットする
  187.      *
  188.      *  @param  string  $name   plugin name
  189.      *  @param  string  $type   plugin type
  190.      *  @param  mixed   $plugin plugin
  191.      *  @TODO   i don't know whether this is working or not
  192.      *  @access public
  193.      */
  194.     public function setPlugin($name$type$plugin)
  195.     {
  196.         //プラグイン関数の有無をチェック
  197.         if (is_callable($plugin=== false{
  198.             return Ethna::raiseWarning('Does not exists.');
  199.         }
  200.  
  201.         //プラグインの種類をチェック
  202.         $register_method 'register_' $type;
  203.         if (method_exists($this->engine$register_method=== false{
  204.             return Ethna::raiseWarning('This plugin type does not exist');
  205.         }
  206.  
  207.         // フィルタは名前なしで登録
  208.         if ($type === 'prefilter' || $type === 'postfilter' || $type === 'outputfilter'{
  209.             parent::setPlugin($name$type$plugin);
  210.             $this->engine->$register_method($plugin);
  211.             return;
  212.         }
  213.  
  214.         // プラグインの名前をチェック
  215.         if ($name === ''{
  216.             return Ethna::raiseWarning('Please set plugin name');
  217.         }
  218.  
  219.         // プラグインを登録する
  220.         parent::setPlugin($name$type$plugin);
  221.         $this->engine->$register_method($name$plugin);
  222.     }
  223. }
  224. // }}}

Documentation generated on Fri, 11 Nov 2011 03:59:06 +0900 by phpDocumentor 1.4.3