[英]how to call block method from magento 1 template
I'm new to magento and always have issues. 我是magento的新手,总是有问题。
For now, I managed to add subscribe popup message and want to add a child block to my main block. 现在,我设法添加了订阅弹出消息,并希望将子块添加到我的主块。
Code:(my custom module is My_Module
) 代码:(我的自定义模块是My_Module
)
<reference name="before_body_end">
<block type="newsletter/subscribe" name="newsletter_popup" as="newsletter_popup" template="popup/subscribe.phtml">
<block type="Module/popup_newsletter" name="newsletter11" />
</block>
</reference name="before_body_end">
and in subscribe.phtml
I try the following: 在subscribe.phtml
我尝试以下内容:
var_dump($this->getChildHtml('newsletter11'))
but the result is: 但结果是:
string(0)"" 串(0) “”
I tried to load the block from template in this way also: 我试图以这种方式从模板加载块:
var_dump($this->getLayout()->createBlock('module/popup_newsletter'));
but the result is boolean(false).
但结果是boolean(false).
what I want to do is call a method from child block ( Newsletter.php
) and this out put some text, this block for now has these two methods: 我想要做的是从子块调用一个方法( Newsletter.php
),然后把这个文本放一些,这个块现在有这两个方法:
puplic function test(){
return 'this is test';
}
public function _toHtml()
{
return test();
}
I can't see were is my mistake. 我看不出是我的错。
Can any one help me thankfully, I don't know if you need more code to post. 任何人都可以帮助我,谢天谢地,我不知道你是否需要更多的代码发布。 Just let me know if you need 如果您需要,请告诉我
Thanks in advance. 提前致谢。
update: config.xml file: update: config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<My_Module>
<version>1.0.1</version>
</My_Module>
</modules>
<global>
<models>
<my_module>
<class>My_Module_Model</class>
</my_module>
</models>
<helpers>
<my_module>
<class>My_Module_Helper</class>
</my_module>
</helpers>
<blocks>
<my_module>
<class>My_Module_Block</class>
</my_module>
</blocks>
</global>
<frontend>
<routers>
<My_Module>
<use>standard</use>
<args>
<module>My_Module</module>
<frontName>my</frontName>
</args>
</My_Module>
</routers>
</frontend>
To create object for block in template (phmtl) file. 在模板(phmtl)文件中为块创建对象。 Try the following code 请尝试以下代码
$customBlock = $this->getLayout()->getBlock('block_name'); // You can use newsletter11 in that block_name
To call block function 调用块功能
echo $customBlock->test();
<?php echo $this->getLayout()
->createBlock('cms/block')
->setBlockId('seo-homepage')
->toHtml();
?>
Where seo-homepage is your static block identifier seo-homepage是你的静态块标识符
this worked for me 这对我有用
$customBlock = $this->getLayout()->getBlockSingleton('block-class-name'); $ customBlock = $ this-> getLayout() - > getBlockSingleton('block-class-name');
you can simply add block 你可以简单地添加块
$this->getLayout()->createBlock('route/path to the file') $ this-> getLayout() - > createBlock('route / path to the file')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.