在magento中跨不同的phtml文件访问变量

时间:2016-08-02 09:45:46

标签: php magento

我想推迟使用magto head.phtml文件的javascript文件,脚本和css由magneto head.phtml文件中的$this->getCssJsHtml();函数调用,所以只是简单的输出就像

$str  =  $this->getCssJsHtml();
$css  =  substr($str, 0, strpos($str, "<script"));
$js   =  substr($str,strpos($str, "<script") );

然后我将$ js保存到注册表中,如

Mage::register('hs_scripts'  ,  $js); 

并在footer.phtml我从注册表中调用它,如

echo Mage::registry("hs_scripts");

现在除非我启用磁条缓存,否则每件事都运行良好,当我启用磁条缓存然后打开然后打开主页然后,在每个下一页上只加载主页脚本

如果我禁用缓存,一切正常,所以我需要一种方法来存储1个phtml文件的数据,然后在其他文件上访问它

1 个答案:

答案 0 :(得分:1)

而是将脚本添加到要缓存的页脚块,尝试创建新块并将其添加到before_body_end块。

[theme] /layout/local.xml 文件:

<default>
    <reference name="before_body_end">
        <block type="core/template" name="footer.scripts" template="page/html/footer/scripts.phtml"/>
    </reference>
</default>

[主题] /template/page/html/footer/scripts.phtml 文件:

echo Mage::registry('hs_scripts');

祝你好运!