CakePHP 完全 smarty 化への道(2) ビフォーアフター

CakePHP 完全 smarty 化が完了しました。
ファイルまとめて使い方書けたら公開しようと思います。
需要の有無は関係ない!


CakePHP 完全 smarty 化の意義は

  1. bake による自動生成の元のテンプレートを smarty テンプレートとして切り出すことで、元ファイルに手を加えやすくなる
    • その結果、bake コマンドに見切りを付けるタイミングを、後ろにずらせる。
  2. php コードと html の分離
    • デザイナさんに与える脅威の減少
    • 何より、読みやス

他にもいろいろあると思うけど、眠くて思い浮かばないです。



1.については以下を見比べていただければと。
まるまる貼っつけたので見辛いですが。

bake 元のテンプレート

index view について。

before

/cake/console/tasks/view.php

		//-------------------------[INDEX]-------------------------//
		$indexView = null;
		$indexView .= "<div class=\"{$pluralName}\">\n";
		$indexView .= "<h2>List " . $pluralHumanName . "</h2>\n\n";
		$indexView .= "<table cellpadding=\"0\" cellspacing=\"0\">\n";
		$indexView .= "\t<tr>\n";
		foreach ($fieldNames as $fieldName) {
			$indexView .= "\t\t<th><?php echo \$paginator->sort('{$fieldName['name']}');?></th>\n";
		}
		$indexView .= "\t\t<th>Actions</th>\n";
		$indexView .= "\t</tr>\n";
		$indexView .= "<?php foreach (\${$pluralName} as \${$singularName}): ?>\n";
		$indexView .= "\t<tr>\n";
		$count = 0;
		foreach($fieldNames as $field => $value) {
			if(isset($value['foreignKey'])) {
				$otherModelName = $this->_modelName($value['model']);
				$otherModelKey = Inflector::underscore($value['modelKey']);
				$otherModelObj =& ClassRegistry::getObject($otherModelKey);
				$otherControllerName = $this->_controllerName($value['modelKey']);
				$otherControllerPath = $this->_controllerPath($otherControllerName);
				if(is_object($otherModelObj)) {
					$displayField = $otherModelObj->getDisplayField();
					$indexView .= "\t\t<td><?php echo \$html->link(\$".$singularName."['{$otherModelName}']['{$displayField}'], array('controller'=> '{$otherControllerPath}', 'action'=>'view', \$".$singularName."['{$otherModelName}']['{$otherModelObj->primaryKey}'])); ?></td>\n";
				} else {
					$indexView .= "\t\t<td><?php echo \$".$singularName."['{$modelObj->name}']['{$field}']; ?></td>\n";
				}
				$count++;
			} else {
				$indexView .= "\t\t<td><?php echo \$".$singularName."['{$modelObj->name}']['{$field}']; ?></td>\n";
			}
		}
		$indexView .= "\t\t<td class=\"actions\">\n";
		$indexView .= "\t\t\t<?php echo \$html->link('View', array('action'=>'view', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])); ?>\n";
		$indexView .= "\t\t\t<?php echo \$html->link('Edit', array('action'=>'edit', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}'])); ?>\n";
		$indexView .= "\t\t\t<?php echo \$html->link('Delete', array('action'=>'delete', \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}']), null, 'Are you sure you want to delete #' . \$".$singularName."['{$modelObj->name}']['{$modelObj->primaryKey}']); ?>\n";
		$indexView .= "\t\t</td>\n";
		$indexView .= "\t</tr>\n";
		$indexView .= "<?php endforeach; ?>\n";
		$indexView .= "</table>\n\n";
		$indexView .= "</div>\n";
		$indexView .= "<div class=\"paging\">\n";
		$indexView .= "<?php echo \$paginator->prev('<< previous', array(), null, array('class'=>'disabled'));?>\n";
		$indexView .= "|\n";
		$indexView .= "<?php echo \$paginator->next('next >>', array(), null, array('class'=>'disabled'));?>\n";
		$indexView .= "</div>\n";
		$indexView .= "<div class=\"actions\">\n";
		$indexView .= "\t<ul>\n";
		$indexView .= "\t\t<li><?php echo \$html->link('New {$singularHumanName}', array('action'=>'add')); ?></li>\n";
		$indexView .= "\t</ul>\n";
		$indexView .= "</div>";
after

/cake/console/libs/templates/views/smarty/index.tpl

<div class="{$pluralName}">
<h2>List {$pluralHumanName} </h2>

<table cellpadding="0" cellspacing="0">
	<tr>
{foreach from=$fieldNames key="key" item=fieldName}
		<th>{ldelim}$paginator->sort('{$fieldName.name}'){rdelim}</th>
{/foreach}
		<th>Actions</th>
	</tr>

	{ldelim}foreach from=${$pluralName} item="{$singularName}"{rdelim}
	<tr>
{foreach from=$fieldNames key="field" item="value"}
	{if $value.foreignKey}
		{assign var="param" value=$this->_index1($value, $singularName)}
		{if $this->call("is_object", $param.otherModelObj)}
			{assign var="displayField" value=$param.otherModelObj->getDisplayField()}
			<td>{ldelim}$html->link(${$singularName}.{$param.otherModelName}.{$displayField}, $view->_array('controller=>{$param.otherControllerPath}', 'action=>view', ${$singularName}.{$param.otherModelName}.{$param.otherModelObj->primaryKey}))}</td>
		{else}
			<td>{ldelim}${$singularName}.{$modelObj->name}.{$field}}</td>
		{/if}

	{else}
	<td>{ldelim}${$singularName}.{$modelObj->name}.{$field}{rdelim}</td>
	{/if}
{/foreach}

		<td class="actions">
		{ldelim}$html->link('View', $view->_array('action=>view', ${$singularName}.{$modelObj->name}.{$modelObj->primaryKey})){rdelim}
		{ldelim}$html->link('Edit', $view->_array('action=>edit', ${$singularName}.{$modelObj->name}.{$modelObj->primaryKey})){rdelim}
		{ldelim}$html->link('Delete', $view->_array('action=>delete', ${$singularName}.{$modelObj->name}.{$modelObj->primaryKey}), null, "Are you sure you want to delete #`${$singularName}.{$modelObj->name}.{$modelObj->primaryKey}`"){rdelim}
		</td>
	</tr>
	{ldelim}/foreach{rdelim}

</table>
</div>
<div class="paging">
{ldelim}$paginator->prev('<< previous', $view->_array(), null,  $view->_array('class=>disabled')){rdelim}
|
{ldelim}$paginator->next('next >>',  $view->_array(), null,  $view->_array('class=>disabled')){rdelim}
</div>
<div class="actions">
	<ul>
		<li>{ldelim}$html->link('New {$singularHumanName}', $view->_array('action=>add')){rdelim}</li>
	</ul>
</div>

bake で書き出される view ファイル

before

index.ctp

<div class="users">
<h2>List Users</h2>

<table cellpadding="0" cellspacing="0">
	<tr>
		<th><?php echo $paginator->sort('id');?></th>
		<th><?php echo $paginator->sort('group_id');?></th>
		<th><?php echo $paginator->sort('loginid');?></th>
		<th><?php echo $paginator->sort('loginpass');?></th>
		<th><?php echo $paginator->sort('name');?></th>
		<th><?php echo $paginator->sort('comment');?></th>
		<th>Actions</th>
	</tr>
<?php foreach ($users as $user): ?>
	<tr>
		<td><?php echo $user['User']['id']; ?></td>
		<td><?php echo $html->link($user['Group']['name'], array('controller'=> 'groups', 'action'=>'view', $user['Group']['id'])); ?></td>
		<td><?php echo $user['User']['loginid']; ?></td>
		<td><?php echo $user['User']['loginpass']; ?></td>
		<td><?php echo $user['User']['name']; ?></td>
		<td><?php echo $user['User']['comment']; ?></td>
		<td class="actions">
			<?php echo $html->link('View', array('action'=>'view', $user['User']['id'])); ?>
			<?php echo $html->link('Edit', array('action'=>'edit', $user['User']['id'])); ?>
			<?php echo $html->link('Delete', array('action'=>'delete', $user['User']['id']), null, 'Are you sure you want to delete #' . $user['User']['id']); ?>
		</td>
	</tr>
<?php endforeach; ?>
</table>

</div>
<div class="paging">
<?php echo $paginator->prev('<< previous', array(), null, array('class'=>'disabled'));?>
|
<?php echo $paginator->next('next >>', array(), null, array('class'=>'disabled'));?>
</div>
<div class="actions">
	<ul>
		<li><?php echo $html->link('New User', array('action'=>'add')); ?></li>
	</ul>
</div>
after
<div class="users">
<h2>List Users </h2>

<table cellpadding="0" cellspacing="0">
	<tr>
		<th>{$paginator->sort('id')}</th>
		<th>{$paginator->sort('group_id')}</th>
		<th>{$paginator->sort('loginid')}</th>
		<th>{$paginator->sort('loginpass')}</th>
		<th>{$paginator->sort('name')}</th>
		<th>{$paginator->sort('comment')}</th>
		<th>Actions</th>
	</tr>

	{foreach from=$users item="user"}
	<tr>
		<td>{$user.User.id}</td>
												<td>{$html->link($user.Group.name, $view->_array('controller=>groups', 'action=>view', $user.Group.id))}</td>
		
			<td>{$user.User.loginid}</td>
			<td>{$user.User.loginpass}</td>
			<td>{$user.User.name}</td>
			<td>{$user.User.comment}</td>
	
		<td class="actions">
		{$html->link('View', $view->_array('action=>view', $user.User.id))}
		{$html->link('Edit', $view->_array('action=>edit', $user.User.id))}
		{$html->link('Delete', $view->_array('action=>delete', $user.User.id), null, "Are you sure you want to delete #`$user.User.id`")}
		</td>
	</tr>
	{/foreach}

</table>
</div>
<div class="paging">
{$paginator->prev('<< previous', $view->_array(), null,  $view->_array('class=>disabled'))}
|
{$paginator->next('next >>',  $view->_array(), null,  $view->_array('class=>disabled'))}
</div>
<div class="actions">
	<ul>
		<li>{$html->link('New User', $view->_array('action=>add'))}</li>
	</ul>
</div>

ね、ね、すっきりしたよね(自己暗示)。


・・・そろそろモヒカンでも生えてくるんだろうか。