Magento 新闻模块开发教程 (八) 建立模板文件

在创建好控制器和模型等后,自然会想到应该创建视图文件。

在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文件来显示某个区块。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值