在创建好控制器和模型等后,自然会想到应该创建视图文件。
在app\design\frontend\default\default\template\news目录下创建news.phtml
<h3><?php echo $this->__('Latest news from Voodoo') ?></h3>
<table>
<?php foreach($this->getNewsList() as $item): ?>
<tr>
<td>
<img src="<?php echo Mage::getBaseUrl('media') . 'news'. DS . $item['filename'] ?>" alt="<?php echo $item['title']?>" width="64" height="64" style="border: 1px solid#d5d5d5;padding:3px;margin-right: 10px;" />
</td>
<td>
<h5><?php echo $item['title'] ?></h5>
<p>
<?php echo $this->limitCharacter($item['content'], 180, "<a href=".$this->getBaseUrl('')."news/index/view/id/".$item['news_id'].">read more</a>")?>
</p>
</td>
</tr>
<?php endforeach; ?>
</table>
然后再同样目录下创建view.phtml文件:
<?php $item = $this->getNews(); ?>
<h3><?php echo $item['title']; ?></h3>
<p style="font-size: small;color: #777">published on: <?php echo date('M jS, Y', strtotime($item['update_time'])); ?> <a href="/news">Back to all news</a></p>
<div>
<span style="float: left">
<img src="<?php echo Mage::getBaseUrl('media') . 'news' . DS . $item['filename'] ?>" alt="<?php echo $item['title']?>" width="128" height="128" style="border: 1px solid#d5d5d5;padding:3px;margin-right: 10px;" />
</span>
<span>
<p><?php echo $item['content']; ?></p>
</span>
<p style="clear: both"></p>
</div>
<a href="<?php echo $this->getBaseUrl('')?>news">Back to all news</a>
第三步回到app\design\frontend\default\default\layout下创建News.xml布局文件
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
</default>
<news_index_index>
<reference name="root">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
</reference>
<reference name="content">
<block type="news/news" name="news" template="news/news.phtml" />
</reference>
</news_index_index>
<news_index_view>
<reference name="content">
<block type="news/news" name="news" template="news/view.phtml" />
</reference>
</news_index_view>
</layout>
这样我们的视图层就完成了。Magento主要靠.xml文件来布局页面,.phtml文件来显示某个区块。