Magento获取给定页面的布局 [英] Magento get layout for given page

查看:77
本文介绍了Magento获取给定页面的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Magento模块,我想从前端页面获取布局.前端页面是指所有类型的页面,即cms页面,类别,产品,购物车,我的帐户等.

I'm working on a Magento module where i want to fetch the layout from a frontend page. By frontend page i mean all types of pages, i.e. cms page, category, product, cart, my account, etc.

我已经阅读了Alan Storm出色的magento-nofrills电子书,并且正在使用他的Commercebug插件来帮助我开发magento网站.我也花了一些时间在stackoverflow上寻找类似的问题,如果我错过了,请原谅我.

I've read the excellent magento-nofrills ebook from Alan Storm and i'm using his Commercebug plugin to help me develop my magento site. I've also spent some time on stackoverflow looking for a similair question, please forgive me if i have missed it.

例如,如果我有一个页面ID或类别ID,如何从后端运行的代码中获取前端页面的布局?

If for example i have a page id or category id, how do i fetch the layout of that frontend page from code run in the backend?

我一直在处理请求,当我在前端运行此代码时,例如在查看目录页面时,我可以看到模块,控制器,布局句柄等已更改

I've been playing with the request and when i run this code in the frontend i can see that the module, controller, layouthandles etc are changed when for instance looking at a catalog page

$request = Mage::app()->getRequest();
$request->setModuleName('cms');
$request->setRouteName('cms');
$request->setControllerName('page');
$request->setActionName('view');
$request->setParam('page_id', 6);

但是当我从布局检查xml时,似乎无法强迫magento向我显示ID = 6的(在本示例中)cms页面的xml:

But when i inspect the xml from the layout, i can't seem to force magento to show me the xml for (in this example) cms page with id=6:

Mage::app()->getLayout()->getNode()->asXml();

也许我在想的太复杂了.我想检查某个页面是否具有侧边栏,该页面和侧边栏中显示了哪些块和元素(仅活动块,而不是忽略的块).

Maybe i'm thinking way too complicated though. I would like to check if a certain page has sidebars, which blocks and elements are shown on the page and in the sidebars (only active blocks, not the ignored ones).

提前谢谢! 蒂姆

推荐答案

在getLayout()之前调用loadLayout().

Call loadLayout() before getLayout().

您可以通过调用getBlock($ block_name)来检查布局中的某个块是否处于活动状态:

You can check if a block is active in the layout by calling getBlock($block_name):

$left_block = $this->loadLayout()->getLayout()->getBlock('left');

如果该块不是布局的一部分(或被忽略),则$ left_block将为false. 您可以将getBlock()与getSortedChildren()结合使用以查看其子块:

If the block isn't part of the layout (or is ignored) then $left_block will be false. You can use getBlock() in conjunction with getSortedChildren() to see its child blocks:

$this->loadLayout()->getLayout()->getBlock('content')->getSortedChildren();

上面的代码将在"content"块中返回一个由块名组成的字符串数组.仅返回活动块的名称.

The above will return a string array of block names within the "content" block. Only names of active blocks will be returned.

这篇关于Magento获取给定页面的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆