がるの健忘録

エンジニアでゲーマーで講師で占い師なおいちゃんのブログです。

あきらめてSmarty触ってみました

んっと…まぁ「デザイナがなれてるからSmartyよろ」と言われると…後メンテ考えて「やだい」とは言いにくく。
今後も延々保守やりまくるならともかくとして、以降どうなるかわからないだけに余計にねぇ…。
ってなわけで。かなり嫌々ながらSmarty叩いてみたです。


………なんじゃこりゃぁ!!(ジーパン風)


キャッシュファイルすごいですぅ。
多分これって
from templatefile to PHP source code generate engine
の略なんじゃなかろうか…って感じですねはい。


Hello, {$name}!


Hello, <?php echo $this->_tpl_vars['name']; ?>
!
になるくらいはまぁいいとしませう。


Hello, {$name|nl2br}!

<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
smarty_core_load_plugins(array('plugins' => array(array('modifier', 'nl2br', 'in
dex.tpl', 5, false),)), $this); ?>
が付与された上で

Hello, <?php echo ( (is_array($_tmp=$this->_tpl_vars['name']) ) ? $this->_run_mod_
handler('nl2br', true, $_tmp) : smarty_modifier_nl2br($_tmp)); ?>
!
となります。

ifはまぁ流石に捻る余地が少ないらしく。


{if $name eq "Fred"}
Welcome Sir.
{elseif $name eq "Wilma"}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}

<?php if ($this->_tpl_vars['name'] == 'Fred'): ?>
Welcome Sir.
<?php elseif ($this->_tpl_vars['name'] == 'Wilma'): ?>
Welcome Ma'am.
<?php else: ?>
Welcome, whatever you are.
<?php endif; ?>
になる程度なのですが。


{foreach from=$custid item=curr_id}
id: {$curr_id}<br>
{/foreach}

<?php $_from = $this->_tpl_vars['custid']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }if (count($_from)):
foreach ($_from as $this->_tpl_vars['curr_id']):
?>
id: <?php echo $this->_tpl_vars['curr_id']; ?>
<br>
<?php endforeach; endif; unset($_from); ?>
に。このあたりから微妙なニュアンスが漂います。
おっかないのがsection。どっちもループだよねぇ、程度に思ってたのですが………甘いも甘いも大甘。

{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
が、なんと…

<?php unset($this->_sections['customer']);
$this->_sections['customer']['name'] = 'customer';
$this->_sections['customer']['loop'] = is_array($_loop=$this->_tpl_vars['custid']) ? count($_loop) : max(0, (int)$_loop); unset($_loop);
$this->_sections['customer']['show'] = true;
$this->_sections['customer']['max'] = $this->_sections['customer']['loop'];
$this->_sections['customer']['step'] = 1;
$this->_sections['customer']['start'] = $this->_sections['customer']['step'] > 0 ? 0 : $this->_sections['customer']['loop']-1;
if ($this->_sections['customer']['show']) {
$this->_sections['customer']['total'] = $this->_sections['customer']['loop'];
if ($this->_sections['customer']['total'] == 0)
$this->_sections['customer']['show'] = false;
} else
$this->_sections['customer']['total'] = 0;
if ($this->_sections['customer']['show']):

for ($this->_sections['customer']['index'] = $this->_sections['customer']['start'], $this->_sections['customer']['iteration'] = 1;
$this->_sections['customer']['iteration'] <= $this->_sections['customer']['total'];
$this->_sections['customer']['index'] += $this->_sections['customer']['step'], $this->_sections['customer']['iteration']++):
$this->_sections['customer']['rownum'] = $this->_sections['customer']['iteration'];
$this->_sections['customer']['index_prev'] = $this->_sections['customer']['index'] - $this->_sections['customer']['step'];
$this->_sections['customer']['index_next'] = $this->_sections['customer']['index'] + $this->_sections['customer']['step'];
$this->_sections['customer']['first'] = ($this->_sections['customer']['iteration'] == 1);
$this->_sections['customer']['last'] = ($this->_sections['customer']['iteration'] == $this->_sections['customer']['total']);
?>
id: <?php echo $this->_tpl_vars['custid'][$this->_sections['customer']['index']]; ?>
<br>
<?php endfor; endif; ?>

となります。………ありですかこんなの?
いやぁ…こえぇ。


…いかん本題忘れそうだ。

あきらめてSmarty触ってみました:本題

…まて。
まぁいいや(笑


んっと。何がいやって「MVCに入れ込みにくい」の"も"イヤでして。
具体的には。displayメソッドで「いきなり出力する」のってどうよ?
お願いだからHTML stringで復帰する関数ぷりぃづ!!


ってわけで作りました(2006/09/26追記。…いいから本文の後ろをご覧くださいませ)。
とれぬなら。うばってしまえ ほととぎす。

class mw_smarty extends Smarty
{
public function convert($resource_name, $cache_id = null, $compile_id = null)
{
//
ob_start();
ob_clean(); // 念のため

// 出力
$this->display($resource_name, $cache_id , $compile_id);

// げと
$html_string = ob_get_contents();

// きれいにして復帰
ob_clean();
return $html_string;
}

} // end of class

これでとりあえずMVCにのるってぇかViewクラス群の連中に「HTMLだよあとはよろ」って言えるです。
…ふぅ。


2006/09/26追記
id:elf さんからありがたいコメントを頂戴しました。
…私が欲しいメソッドすでに存在してます。
fetch()
ってのがそれに相当します。
失礼いたしました〜。

玉言のごときコメント、ありがとうございます > id:elf さん
# これで「Smarty嫌」な部分の一部はヘッジ出来たなぁ。…メインは相変わらずだとしても(笑

いい!!

むむぅ。すてぇきなフレームワーク発見。
世界最軽量のPHPMVCフレームワーク、その名も 「ちいたん」。公式Pageはこちら http://php.cheetan.net/
素晴らしい。なにがいいってこのシンプルさ加減と軽さ加減がぐっど。
インストールの仕方も何もかも一通りぐっど!!
MagicWeaponと根底に流れる部分に共通のものをたっぷりと感じるです。
久しぶりにいいもん見させていただきました。


これが「すべてだ」とは思いませんが。これくらい「思い切った」発想と思想ってすきだなぁ。