がるの健忘録

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

あきらめて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; ?>

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


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