Source for file Ethna_Renderer_Smarty.php

Documentation is available at Ethna_Renderer_Smarty.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_Renderer_Smarty.php
  5.  *
  6.  *  @author     Kazuhiro Hosoi <hosoi@gree.co.jp>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id$
  10.  */
  11. require_once 'Smarty/Smarty.class.php';
  12.  
  13. // {{{ Ethna_Renderer_Smarty
  14. /**
  15.  *  Smartyレンダラクラス(Mojaviのまね)
  16.  *
  17.  *  @author     Kazuhiro Hosoi <hosoi@gree.co.jp>
  18.  *  @access     public
  19.  *  @package    Ethna
  20.  */
  21. {
  22.     /** @var    string compile directory  */
  23.     var $compile_dir;
  24.     
  25.     /**
  26.      *  Ethna_Renderer_Smartyクラスのコンストラクタ
  27.      *
  28.      *  @access public
  29.      */
  30.     function Ethna_Renderer_Smarty(&$controller)
  31.     {
  32.         parent::Ethna_Renderer($controller);
  33.         
  34.         $this->engine =new Smarty;
  35.         
  36.         // ディレクトリ関連は Controllerによって実行時に設定
  37.         // TODO: iniファイルによって上書き可にするかは要検討
  38.         $template_dir $controller->getTemplatedir();
  39.         $compile_dir $controller->getDirectory('template_c');
  40.  
  41.         $this->setTemplateDir($template_dir);
  42.         $this->compile_dir = $compile_dir;
  43.         $this->engine->template_dir $this->template_dir;
  44.         $this->engine->compile_dir $this->compile_dir;
  45.         $this->engine->compile_id md5($this->template_dir);
  46.  
  47.         //  デリミタは Ethna_Config を見る
  48.         $smarty_config = isset($this->config['smarty'])
  49.                        ? $this->config['smarty']
  50.                        : array();
  51.         if (array_key_exists('left_delimiter'$smarty_config)) {
  52.             $this->engine->left_delimiter $smarty_config['left_delimiter'];
  53.         }
  54.         if (array_key_exists('right_delimiter'$smarty_config)) {
  55.             $this->engine->right_delimiter $smarty_config['right_delimiter'];
  56.         }
  57.  
  58.         // コンパイルディレクトリは必須なので一応がんばってみる
  59.         if (is_dir($this->engine->compile_dir=== false{
  60.             Ethna_Util::mkdir($this->engine->compile_dir0755);
  61.         }
  62.  
  63.         $this->engine->plugins_dir array_merge(
  64.             $controller->getDirectory('plugins'),
  65.             array(ETHNA_BASE '/class/Plugin/Smarty'SMARTY_DIR 'plugins')
  66.         );
  67.     }
  68.     
  69.     /**
  70.      *  ビューを出力する
  71.      *
  72.      *  @param  string  $template   テンプレート名
  73.      *  @param  bool    $capture    true ならば出力を表示せずに返す
  74.      *
  75.      *  @access public
  76.      */
  77.     function perform($template null$capture false)
  78.     {
  79.         if ($template === null && $this->template === null{
  80.             return Ethna::raiseWarning('template is not defined');
  81.         }
  82.  
  83.         if ($template !== null{
  84.             $this->template $template;
  85.         }
  86.  
  87.         if ((is_absolute_path($this->template&& is_readable($this->template))
  88.             || is_readable($this->template_dir $this->template)) {
  89.                 if ($capture === true{
  90.                     $captured $this->engine->fetch($this->template);
  91.                     return $captured;
  92.                 else {
  93.                     $this->engine->display($this->template);
  94.                 }
  95.         else {
  96.             return Ethna::raiseWarning('template not found ' $this->template);
  97.         }
  98.     }
  99.     
  100.     /**
  101.      * テンプレート変数を取得する
  102.      * 
  103.      *  @param string $name  変数名
  104.      *
  105.      *  @return mixed 変数 
  106.      *
  107.      *  @access public
  108.      */
  109.     function &getProp($name null)
  110.     {
  111.         $property =$this->engine->get_template_vars($name);
  112.  
  113.         if ($property !== null{
  114.             return $property;
  115.         }
  116.         return null;
  117.     }
  118.  
  119.     /**
  120.      *  テンプレート変数を削除する
  121.      * 
  122.      *  @param name    変数名
  123.      * 
  124.      *  @access public
  125.      */
  126.     function removeProp($name)
  127.     {
  128.         $this->engine->clear_assign($name);
  129.     }
  130.  
  131.     /**
  132.      *  テンプレート変数に配列を割り当てる
  133.      * 
  134.      *  @param array $array 
  135.      * 
  136.      *  @access public
  137.      */
  138.     function setPropArray($array)
  139.     {
  140.         $this->engine->assign($array);
  141.     }
  142.  
  143.     /**
  144.      *  テンプレート変数に配列を参照として割り当てる
  145.      * 
  146.      *  @param array $array 
  147.      * 
  148.      *  @access public
  149.      */
  150.     function setPropArrayByRef(&$array)
  151.     {
  152.         $this->engine->assign_by_ref($array);
  153.     }
  154.  
  155.     /**
  156.      *  テンプレート変数を割り当てる
  157.      * 
  158.      *  @param string $name 変数名
  159.      *  @param mixed $value 
  160.      * 
  161.      *  @access public
  162.      */
  163.     function setProp($name$value)
  164.     {
  165.         $this->engine->assign($name$value);
  166.     }
  167.  
  168.     /**
  169.      *  テンプレート変数に参照を割り当てる
  170.      * 
  171.      *  @param string $name 変数名
  172.      *  @param mixed $value 
  173.      * 
  174.      *  @access public
  175.      */
  176.     function setPropByRef($name&$value)
  177.     {
  178.         $this->engine->assign_by_ref($name$value);
  179.     }
  180.  
  181.     /**
  182.      *  プラグインをセットする
  183.      * 
  184.      *  @param string $name プラグイン名 
  185.      *  @param string $type プラグインタイプ
  186.      *  @param mixed $plugin プラグイン本体
  187.      * 
  188.      *  @access public
  189.      */
  190.     function setPlugin($name$type$plugin
  191.     {
  192.         //プラグイン関数の有無をチェック
  193.         if (is_callable($plugin=== false{
  194.             return Ethna::raiseWarning('Does not exists.');
  195.         }
  196.  
  197.         //プラグインの種類をチェック
  198.         $register_method 'register_' $type;
  199.         if (method_exists($this->engine$register_method=== false{
  200.             return Ethna::raiseWarning('This plugin type does not exist');
  201.         }
  202.  
  203.         // フィルタは名前なしで登録
  204.         if ($type === 'prefilter' || $type === 'postfilter' || $type === 'outputfilter'{
  205.             parent::setPlugin($name$type$plugin);
  206.             $this->engine->$register_method($plugin);
  207.             return;
  208.         }
  209.         
  210.         // プラグインの名前をチェック
  211.         if ($name === ''{
  212.             return Ethna::raiseWarning('Please set plugin name');
  213.         }
  214.        
  215.         // プラグインを登録する
  216.         parent::setPlugin($name$type$plugin);
  217.         $this->engine->$register_method($name$plugin);
  218.     }
  219. }
  220. // }}}
  221. ?>

Documentation generated on Fri, 11 Nov 2011 04:00:49 +0900 by phpDocumentor 1.4.3