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: Ethna_Renderer_Smarty.php 453 2007-01-17 18:38:51Z ichii386 $
  10.  */
  11. require_once 'Smarty/Smarty.class.php';
  12. require_once ETHNA_BASE '/class/Ethna_SmartyPlugin.php';
  13.  
  14. // {{{ Ethna_Renderer_Smarty
  15. /**
  16.  *  Smartyレンダラクラス(Mojaviのまね)
  17.  *
  18.  *  @author     Kazuhiro Hosoi <hosoi@gree.co.jp>
  19.  *  @access     public
  20.  *  @package    Ethna
  21.  */
  22. {
  23.     /** @var    string compile directory  */
  24.     var $compile_dir;
  25.     
  26.     /**
  27.      *  Ethna_Renderer_Smartyクラスのコンストラクタ
  28.      *
  29.      *  @access public
  30.      */
  31.     function Ethna_Renderer_Smarty(&$controller)
  32.     {
  33.         parent::Ethna_Renderer($controller);
  34.         
  35.         $this->engine =new Smarty;
  36.         
  37.         $template_dir $controller->getTemplatedir();
  38.         $compile_dir $controller->getDirectory('template_c');
  39.  
  40.         $this->setTemplateDir($template_dir);
  41.         $this->compile_dir = $compile_dir;
  42.         $this->engine->template_dir $this->template_dir;
  43.         $this->engine->compile_dir $this->compile_dir;
  44.         $this->engine->compile_id md5($this->template_dir);
  45.  
  46.         // 一応がんばってみる
  47.         if (is_dir($this->engine->compile_dir=== false{
  48.             Ethna_Util::mkdir($this->engine->compile_dir0755);
  49.         }
  50.  
  51.         $this->engine->plugins_dir array_merge(array(SMARTY_DIR 'plugins'),
  52.                                                  $controller->getDirectory('plugins'));
  53.  
  54.         $this->_setDefaultPlugin();
  55.     }
  56.     
  57.     /**
  58.      *  デフォルトの設定.
  59.      *
  60.      *  @access public
  61.      */
  62.     function _setDefaultPlugin()
  63.     {
  64.         // default modifiers
  65.         $this->setPlugin('number_format','modifier','smarty_modifier_number_format');
  66.         $this->setPlugin('strftime','modifier','smarty_modifier_strftime');
  67.         $this->setPlugin('count','modifier','smarty_modifier_count');
  68.         $this->setPlugin('join','modifier','smarty_modifier_join');
  69.         $this->setPlugin('filter','modifier''smarty_modifier_filter');
  70.         $this->setPlugin('unique','modifier','smarty_modifier_unique');
  71.         $this->setPlugin('wordwrap_i18n','modifier','smarty_modifier_wordwrap_i18n');
  72.         $this->setPlugin('truncate_i18n','modifier','smarty_modifier_truncate_i18n');
  73.         $this->setPlugin('i18n','modifier','smarty_modifier_i18n');
  74.         $this->setPlugin('checkbox','modifier','smarty_modifier_checkbox');
  75.         $this->setPlugin('select','modifier','smarty_modifier_select');
  76.         $this->setPlugin('form_value','modifier','smarty_modifier_form_value');
  77.  
  78.         // default functions
  79.         $this->setPlugin('is_error','function','smarty_function_is_error');
  80.         $this->setPlugin('message','function','smarty_function_message');
  81.         $this->setPlugin('uniqid','function','smarty_function_uniqid');
  82.         $this->setPlugin('select','function','smarty_function_select');
  83.         $this->setPlugin('checkbox_list','function','smarty_function_checkbox_list');
  84.         $this->setPlugin('form_name','function','smarty_function_form_name');
  85.         $this->setPlugin('form_input','function','smarty_function_form_input');
  86.         $this->setPlugin('form_submit','function','smarty_function_form_submit');
  87.         $this->setPlugin('url','function','smarty_function_url');
  88.         $this->setPlugin('csrfid','function','smarty_function_csrfid');
  89.  
  90.         // default blocks
  91.         $this->setPlugin('form','block','smarty_block_form');       
  92.     }
  93.  
  94.     /**
  95.      *  ビューを出力する
  96.      *
  97.      *  @param  string  $template   テンプレート名
  98.      *  @param  bool    $capture    true ならば出力を表示せずに返す
  99.      *
  100.      *  @access public
  101.      */
  102.     function perform($template null$capture false)
  103.     {
  104.         if ($template === null && $this->template === null{
  105.             return Ethna::raiseWarning('template is not defined');
  106.         }
  107.  
  108.         if ($template !== null{
  109.             $this->template $template;
  110.         }
  111.  
  112.         if ((is_absolute_path($this->template&& is_readable($this->template))
  113.             || is_readable($this->template_dir $this->template)) {
  114.                 if ($capture === true{
  115.                     $captured $this->engine->fetch($this->template);
  116.                     return $captured;
  117.                 else {
  118.                     $this->engine->display($this->template);
  119.                 }
  120.         else {
  121.             return Ethna::raiseWarning('template not found ' $this->template);
  122.         }
  123.     }
  124.     
  125.     /**
  126.      * テンプレート変数を取得する
  127.      * 
  128.      *  @param string $name  変数名
  129.      *
  130.      *  @return mixed 変数 
  131.      *
  132.      *  @access public
  133.      */
  134.     function &getProp($name null)
  135.     {
  136.         $property =$this->engine->get_template_vars($name);
  137.  
  138.         if ($property !== null{
  139.             return $property;
  140.         }
  141.         return null;
  142.     }
  143.  
  144.     /**
  145.      *  テンプレート変数を削除する
  146.      * 
  147.      *  @param name    変数名
  148.      * 
  149.      *  @access public
  150.      */
  151.     function removeProp($name)
  152.     {
  153.         $this->engine->clear_assign($name);
  154.     }
  155.  
  156.     /**
  157.      *  テンプレート変数に配列を割り当てる
  158.      * 
  159.      *  @param array $array 
  160.      * 
  161.      *  @access public
  162.      */
  163.     function setPropArray($array)
  164.     {
  165.         $this->engine->assign($array);
  166.     }
  167.  
  168.     /**
  169.      *  テンプレート変数に配列を参照として割り当てる
  170.      * 
  171.      *  @param array $array 
  172.      * 
  173.      *  @access public
  174.      */
  175.     function setPropArrayByRef(&$array)
  176.     {
  177.         $this->engine->assign_by_ref($array);
  178.     }
  179.  
  180.     /**
  181.      *  テンプレート変数を割り当てる
  182.      * 
  183.      *  @param string $name 変数名
  184.      *  @param mixed $value 
  185.      * 
  186.      *  @access public
  187.      */
  188.     function setProp($name$value)
  189.     {
  190.         $this->engine->assign($name$value);
  191.     }
  192.  
  193.     /**
  194.      *  テンプレート変数に参照を割り当てる
  195.      * 
  196.      *  @param string $name 変数名
  197.      *  @param mixed $value 
  198.      * 
  199.      *  @access public
  200.      */
  201.     function setPropByRef($name&$value)
  202.     {
  203.         $this->engine->assign_by_ref($name$value);
  204.     }
  205.  
  206.     /**
  207.      *  プラグインをセットする
  208.      * 
  209.      *  @param string $name プラグイン名 
  210.      *  @param string $type プラグインタイプ
  211.      *  @param mixed $plugin プラグイン本体
  212.      * 
  213.      *  @access public
  214.      */
  215.     function setPlugin($name$type$plugin
  216.     {
  217.         //プラグイン関数の有無をチェック
  218.         if (is_callable($plugin=== false{
  219.             return Ethna::raiseWarning('Does not exists.');
  220.         }
  221.  
  222.         //プラグインの種類をチェック
  223.         $register_method 'register_' $type;
  224.         if (method_exists($this->engine$register_method=== false{
  225.             return Ethna::raiseWarning('This plugin type does not exist');
  226.         }
  227.  
  228.         // フィルタは名前なしで登録
  229.         if ($type === 'prefilter' || $type === 'postfilter' || $type === 'outputfilter'{
  230.             parent::setPlugin($name$type$plugin);
  231.             $this->engine->$register_method($plugin);
  232.             return;
  233.         }
  234.         
  235.         // プラグインの名前をチェック
  236.         if ($name === ''{
  237.             return Ethna::raiseWarning('Please set plugin name');
  238.         }
  239.        
  240.         // プラグインを登録する
  241.         parent::setPlugin($name$type$plugin);
  242.         $this->engine->$register_method($name$plugin);
  243.     }
  244. }
  245. // }}}
  246. ?>

Documentation generated on Thu, 08 May 2008 00:15:26 +0900 by phpDocumentor 1.4.2