如何从magento 1模板调用块方法

时间:2022-03-31 11:15:14

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)""

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).

但结果是布尔值(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>

4 个解决方案

#1


3  

To create object for block in template (phmtl) file. Try the following code

在模板(phmtl)文件中创建块的对象。请尝试以下代码

$customBlock = $this->getLayout()->getBlock('block_name'); // You can use newsletter11 in that block_name

To call block function

调用块功能

echo $customBlock->test();

#2


2  

<?php echo $this->getLayout()
            ->createBlock('cms/block')
            ->setBlockId('seo-homepage')
            ->toHtml();
?>

Where seo-homepage is your static block identifier

seo-homepage是你的静态块标识符

#3


1  

this worked for me

这对我有用

$customBlock = $this->getLayout()->getBlockSingleton('block-class-name');

$ customBlock = $ this-> getLayout() - > getBlockSingleton('block-class-name');

#4


0  

you can simply add block

你可以简单地添加块

$this->getLayout()->createBlock('route/path to the file')

$ this-> getLayout() - > createBlock('route / path to the file')

#1


3  

To create object for block in template (phmtl) file. Try the following code

在模板(phmtl)文件中创建块的对象。请尝试以下代码

$customBlock = $this->getLayout()->getBlock('block_name'); // You can use newsletter11 in that block_name

To call block function

调用块功能

echo $customBlock->test();

#2


2  

<?php echo $this->getLayout()
            ->createBlock('cms/block')
            ->setBlockId('seo-homepage')
            ->toHtml();
?>

Where seo-homepage is your static block identifier

seo-homepage是你的静态块标识符

#3


1  

this worked for me

这对我有用

$customBlock = $this->getLayout()->getBlockSingleton('block-class-name');

$ customBlock = $ this-> getLayout() - > getBlockSingleton('block-class-name');

#4


0  

you can simply add block

你可以简单地添加块

$this->getLayout()->createBlock('route/path to the file')

$ this-> getLayout() - > createBlock('route / path to the file')