OSDN Git Service

75979a4d346d6df5a2d160582bf5745575ba6944
[ethna/ethna.git] / class / Renderer / 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 class Ethna_Renderer_Smarty extends Ethna_Renderer
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_dir, 0755);
49         }
50
51         $this->engine->plugins_dir = array_merge(
52             $controller->getDirectory('plugins'),
53             array(ETHNA_BASE . '/class/Plugin/Smarty', SMARTY_DIR . 'plugins')
54         );
55     }
56     
57     /**
58      *  ビューを出力する
59      *
60      *  @param  string  $template   テンプレート名
61      *  @param  bool    $capture    true ならば出力を表示せずに返す
62      *
63      *  @access public
64      */
65     function perform($template = null, $capture = false)
66     {
67         if ($template === null && $this->template === null) {
68             return Ethna::raiseWarning('template is not defined');
69         }
70
71         if ($template !== null) {
72             $this->template = $template;
73         }
74
75         if ((is_absolute_path($this->template) && is_readable($this->template))
76             || is_readable($this->template_dir . $this->template)) {
77                 if ($capture === true) {
78                     $captured = $this->engine->fetch($this->template);
79                     return $captured;
80                 } else {
81                     $this->engine->display($this->template);
82                 }
83         } else {
84             return Ethna::raiseWarning('template not found ' . $this->template);
85         }
86     }
87     
88     /**
89      * テンプレート変数を取得する
90      * 
91      *  @param string $name  変数名
92      *
93      *  @return mixed 変数
94      *
95      *  @access public
96      */
97     function &getProp($name = null)
98     {
99         $property =& $this->engine->get_template_vars($name);
100
101         if ($property !== null) {
102             return $property;
103         }
104         return null;
105     }
106
107     /**
108      *  テンプレート変数を削除する
109      * 
110      *  @param name    変数名
111      * 
112      *  @access public
113      */
114     function removeProp($name)
115     {
116         $this->engine->clear_assign($name);
117     }
118
119     /**
120      *  テンプレート変数に配列を割り当てる
121      * 
122      *  @param array $array
123      * 
124      *  @access public
125      */
126     function setPropArray($array)
127     {
128         $this->engine->assign($array);
129     }
130
131     /**
132      *  テンプレート変数に配列を参照として割り当てる
133      * 
134      *  @param array $array
135      * 
136      *  @access public
137      */
138     function setPropArrayByRef(&$array)
139     {
140         $this->engine->assign_by_ref($array);
141     }
142
143     /**
144      *  テンプレート変数を割り当てる
145      * 
146      *  @param string $name 変数名
147      *  @param mixed $value 値
148      * 
149      *  @access public
150      */
151     function setProp($name, $value)
152     {
153         $this->engine->assign($name, $value);
154     }
155
156     /**
157      *  テンプレート変数に参照を割り当てる
158      * 
159      *  @param string $name 変数名
160      *  @param mixed $value 値
161      * 
162      *  @access public
163      */
164     function setPropByRef($name, &$value)
165     {
166         $this->engine->assign_by_ref($name, $value);
167     }
168
169     /**
170      *  プラグインをセットする
171      * 
172      *  @param string $name プラグイン名
173      *  @param string $type プラグインタイプ
174      *  @param mixed $plugin プラグイン本体
175      * 
176      *  @access public
177      */
178     function setPlugin($name, $type, $plugin) 
179     {
180         //プラグイン関数の有無をチェック
181         if (is_callable($plugin) === false) {
182             return Ethna::raiseWarning('Does not exists.');
183         }
184
185         //プラグインの種類をチェック
186         $register_method = 'register_' . $type;
187         if (method_exists($this->engine, $register_method) === false) {
188             return Ethna::raiseWarning('This plugin type does not exist');
189         }
190
191         // フィルタは名前なしで登録
192         if ($type === 'prefilter' || $type === 'postfilter' || $type === 'outputfilter') {
193             parent::setPlugin($name, $type, $plugin);
194             $this->engine->$register_method($plugin);
195             return;
196         }
197         
198         // プラグインの名前をチェック
199         if ($name === '') {
200             return Ethna::raiseWarning('Please set plugin name');
201         }
202        
203         // プラグインを登録する
204         parent::setPlugin($name, $type, $plugin);
205         $this->engine->$register_method($name, $plugin);
206     }
207 }
208 // }}}
209 ?>