我有一批经销商商店。我已经实施了一个函数,该函数可以计算出所有商店与客户位置的距离,并且我有各个距离。我在block.php文件中添加了逻辑。

现在,我希望从模板文件访问此数组。我该如何实施?

我从网络获得的所有答案与想要的内容完全相反 - 传递数据从模板文件传递到阻止文件

任何帮助,将不胜感激..

有帮助吗?

解决方案

该模板是块类上下文中的渲染器,这意味着您投入的任何信息 $block->setMyPersonalVar($value) 可以阅读 $this->getMyPersonalValue() 在模板内。因此,只要您对$ block中的块类引用,就可以在其上设置任何值。

这可以在块类本身中使用,例如 toHtml 或在控制器类中 $this->getLayout()->getBlock('nameInLayout')->setMyPersonalValue() 或您想要的任何地方,例如在观察者中 Mage::app()->getLayout()->getBlock('nameInLayout')->setMyPersonalValue()

替代方法是使用 Mage::register('name', $value)Mage::registry('name')

如果这不起作用,我的第一个猜测是:该代码以错误的顺序执行

其他提示

好的,让我们考虑一个例子:

您的块文件 由返回您的功能组成 “大批”, , 喜欢

public function someFunction(){
    //your logic
    return $your_array;
}

在您的config.xml中

<config>
...
    <frontend>
    ...
        <layout>
            <updates>
                <yournamespace_yourmodule module="Yournamspace_YourModule">
                    <file>something.xml</file>
                </yournamespace_yourmodule>
            </updates>
         </layout>

在app/design/frontend/default/your_theme/layout/somings.xml中

<?xml version="1.0" encoding="UTF-8"?>
    <layout version="0.1.0">
        <block_in_which_you_want_your_template>
            <reference name="parent_block_name">
                <block type="yourmodule/block_path" name="unique_name" template="path/to/your/template.phtml" /> <!-- important part of this answer -->
            </reference>
            ...

现在在您的template.phtml中

称呼 $this->someFunction() 获取数组。

键是用theings.xml中的模板文件声明块,以便可以用对象在模板中访问块中的所有成员($this).

许可以下: CC-BY-SA归因
scroll top