Magento Block

1.首先启用block配置

/* 模板.phtml文件中使用的方法在此文件中声明,此案例中此文件将计算结果传递到.phtml文件 */
app/code/local/Hellokey/Counter/Block/Counter.php
/* 主要的插件配置文件 */
app/code/local/Hellokey/Counter/etc/config.xml

<blocks>
    <hellokey>
	<class>Hellokey_Counter_Block</class>
    </hellokey>
</blocks>
</global> 

会加载下面所有的block文件。注意大小写标签

/* 前台显示模板的layout */
app/design/frontend/default/default/layout/counter.xml  Block/Counter.php启用配置

<block type="Hellokey/counter" name="counter" template="customer/index.phtml"/>

有时候会出现一种情况,一个block只需要模板文件而不需要php文件,比如只是需要加一个flash,最多有些简单的php代码,但一个block,type是不可缺少的,那怎么办呢。其实Magento已经给我们提供了一种通用方案,当不需要特别指定type时,可以用core/template 代替,系统自带的模板里就有这种用法

 

2.我们就在counter/index/index这个path下的indexAction中接收这个参数

app/code/local/Hellokeykey/Counter/controllers/IndexController.php文件中代码片段

public function indexAction()
{
    if ($this->getRequest()->isPost()){ /* 首先判断是否有post发生 */
        $nu1 = $this->getRequest()->getParam('NUM1'); /* 截获参数 */
        $nu2 = $this->getRequest()->getParam('NUM2');
        Mage::register('nu1', $nu1);/* 传递到block */
        Mage::register('nu2', $nu2);
    }
 
    $this->loadLayout();
    $this->renderLayout();
}

既然在controllers中有了传递到block的方法,下一步就在block中获取
app/code/local/Hellokeykey/Counter/Block/Counter.php文件中代码片段

protected $nu1 = null ;
protected $nu2 = null;
public function showresult(){
    $this->nu1 = Mage::registry('nu1'); /* 获得从controllers中传递来的参数 */
    $this->nu2 = Mage::registry('nu2');
    return $this->nu1 * $this->nu2;
}

protected function _prepareCollection()  
{
    $roleId = $this->getRequest()->getParam('rid');
    Mage::register('RID', $roleId);
    $collection = Mage::getModel('api/roles')->getUsersCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function  _toHtml()  //相当于block xml 中的temple="..detail.phtml"
{
	$this->setTemplate("virtualcurrency/customer/accounts/detail.phtml");       
	$html = parent::_toHtml();
	return $html;
}
 

此block文件中的showresult()方法在phtml文件中可以试用$this->直接调用。

这样我们就差最后一步就是在我们的模板文件中输出结果了在app/design/frontend/default/default/template/customer/index.phtml文件中添加输出结果的代码

<ul>
<li>result:<?php echo $this->showresult(); ?></li>
</ul>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值