3 * Ethna_Renderer_Rhaco.php (experimental)
5 * @author TSURUOKA Naoya <tsuruoka@php.net>
6 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
11 set_include_path(get_include_path() . PATH_SEPARATOR . BASE . '/lib/rhaco');
13 require_once 'rhaco/Rhaco.php';
14 require_once 'rhaco/tag/TemplateParser.php';
15 require_once ETHNA_BASE . '/class/Ethna_SmartyPlugin.php';
23 class Ethna_Renderer_Rhaco extends Ethna_Renderer
25 /** @var string compile directory */
26 var $compile_dir = '';
28 var $template_dir = '';
35 var $smarty_plugin_list = array();
38 * Rhaco TemplateParser
39 * @var TemplateParser_Ethna $engine
45 * Ethna_Renderer_Rhacoクラスのコンストラクタ
49 function Ethna_Renderer_Rhaco(&$controller)
51 parent::Ethna_Renderer($controller);
53 $this->template_dir = $controller->getTemplatedir() . '/';
54 $this->compile_dir = $controller->getDirectory('template_c');
56 Rhaco::constant('TEMPLATE_PATH', $this->template_dir);
58 $this->engine =& new TemplateParser_Ethna();
61 $this->setTemplateDir($template_dir);
62 $this->compile_dir = $compile_dir;
64 $this->engine->template_dir = $this->template_dir;
65 $this->engine->compile_dir = $this->compile_dir;
66 $this->engine->compile_id = md5($this->template_dir);
69 if (is_dir($this->engine->compile_dir) === false) {
70 Ethna_Util::mkdir($this->engine->compile_dir, 0755);
73 $this->_setDefaultPlugin();
80 * @param string $template テンプレート名
81 * @param bool $capture true ならば出力を表示せずに返す
83 function perform($template = null, $capture = false)
85 if ($template === null && $this->template === null) {
86 return Ethna::raiseWarning('template is not defined');
89 if ($template !== null) {
90 $this->template = $template;
93 if ((is_absolute_path($this->template) && is_readable($this->template))
94 || is_readable($this->template_dir . $this->template)) {
95 if ($capture === true) {
96 $captured = $this->engine->read($this->template);
99 $captured = $this->engine->read($this->template);
103 return Ethna::raiseWarning('template not found ' . $this->template);
112 * @param string $name 変数名
115 function &getProp($name = null)
117 $property =& $this->engine->variables[$name];
119 if ($property !== null) {
133 function removeProp()
135 $this->engine->clearVariable(func_num_args());
141 * @param array $array
144 function setPropArray($array)
146 $this->engine->setVariable($array);
150 * テンプレート変数に配列を参照として割り当てる
152 * @param array $array
156 function setPropArrayByRef(&$array)
158 //$this->engine->assign_by_ref($array);
164 * @param string $name 変数名
165 * @param mixed $value 値
169 function setProp($name, $value)
171 $this->engine->setVariable($name, $value);
179 * @param string $name 変数名
180 * @param mixed $value 値
182 function setPropByRef($name, &$value)
184 $this->engine->setVariable($name, $value);
185 //$this->engine->assign_by_ref($name, $value);
193 function setPlugin($name, $type, $plugin)
195 //Smartyプラグイン関数の有無をチェック
196 if (is_callable($plugin) === false) {
197 return Ethna::raiseWarning('Does not exists.');
200 $this->smarty_plugin_list[$name] = array(
211 function getPluginList()
213 return $this->smarty_plugin_list;
221 function _setDefaultPlugin()
225 $this->setPlugin('number_format','modifier','smarty_modifier_number_format');
226 $this->setPlugin('strftime','modifier','smarty_modifier_strftime');
227 $this->setPlugin('count','modifier','smarty_modifier_count');
228 $this->setPlugin('join','modifier','smarty_modifier_join');
229 $this->setPlugin('filter','modifier', 'smarty_modifier_filter');
230 $this->setPlugin('unique','modifier','smarty_modifier_unique');
231 $this->setPlugin('wordwrap_i18n','modifier','smarty_modifier_wordwrap_i18n');
232 $this->setPlugin('truncate_i18n','modifier','smarty_modifier_truncate_i18n');
233 $this->setPlugin('i18n','modifier','smarty_modifier_i18n');
234 $this->setPlugin('checkbox','modifier','smarty_modifier_checkbox');
235 $this->setPlugin('select','modifier','smarty_modifier_select');
236 $this->setPlugin('form_value','modifier','smarty_modifier_form_value');
240 $this->setPlugin('is_error','function','smarty_function_is_error');
241 $this->setPlugin('message','function','smarty_function_message');
242 $this->setPlugin('uniqid','function','smarty_function_uniqid');
243 $this->setPlugin('select','function','smarty_function_select');
244 $this->setPlugin('checkbox_list','function','smarty_function_checkbox_list');
245 $this->setPlugin('form_name','function','smarty_function_form_name');
246 $this->setPlugin('form_input','function','smarty_function_form_input');
247 $this->setPlugin('form_submit','function','smarty_function_form_submit');
248 $this->setPlugin('url','function','smarty_function_url');
249 $this->setPlugin('csrfid','function','smarty_function_csrfid');
252 $this->setPlugin('form','block','smarty_block_form');
254 $this->engine->setSmartyPluginList($this->getPluginList());
261 * TemplateParser_Ethna
263 class TemplateParser_Ethna extends TemplateParser
266 * smarty_function list
270 var $smarty_plugin_list = array();
273 * fake property for Smaty
277 var $_tag_stack = array();
280 * setSmartyPluginList
283 function setSmartyPluginList($plugin_list)
285 if (!is_array($plugin_list)) {
288 $this->smarty_plugin_list = $plugin_list;
292 * getSmartyPluginList
296 function getSmartyPluginList()
298 return $this->smarty_plugin_list;
302 * smarty_function dispatcher
307 function _exec9002_smartyfunctions($src)
309 $tag = new SimpleTag();
310 $smarty_plugin_list = $this->getSmartyPluginList();
312 foreach($smarty_plugin_list as $name => $plugin_config) {
314 while ($tag->set($src, $this->_getTagName($name))) {
316 if ($plugin_config['type'] == 'function') {
318 $param = $tag->toHash();
321 $plugin_config['plugin']($param, $this),
325 } else if ($plugin_config['type'] == 'block') {
327 $repeat_before = true;
328 $repeat_after = false;
329 $param_list = $tag->getParameter();
330 foreach ($param_list as $param_tag) {
331 $param[$param_tag->getName()] = $param_tag->getValue();
333 $content = $tag->getValue();
335 //before(not return value)
336 $result = $plugin_config['plugin']($param, $content, $this, $repeat_before);
339 $result = $plugin_config['plugin']($param, $content, $this, $repeat_after);
355 * TemplateParser _getTagName
358 * @param string $value
360 function _getTagName($value)
362 return sprintf("rt:%s",$value);