Source for file Smarty.php

Documentation is available at Smarty.php

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

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