Magento :: 模块中的布局

发布于 2024-12-01 06:40:05 字数 368 浏览 1 评论 0原文

是否可以在模块内创建布局文件?如何 ?

目的:
我想为产品添加某种统计命中计数器,并且我不想覆盖产品类,因为我正在使用的某些模块已经完成了这一点。因此,我认为最好有一个自定义模块,其中包含一个由布局语句调用的块。 当然,我可以轻松编辑我的私有 local.xml 或更改主题布局文件夹中的另一个布局 xml,但我希望此功能在所有主题中可用(独立于任何选定的主题)。

一些限制:

  • 所有代码都在一个模块中
  • ...以便它与主题无关
  • ...以便该模块可以与其他人共享,而无需他们更改任何内容(例如主题文件),以便安装/加载我的模块就足够了

我也会接受不同的统计命中计数器加载方法(使用相同的约束)

Is it possible to create a layout file inside of a module ? How ?

For what:
I want to add a some kind of statistics hit counter for products, and I don't want to override the products class, as that is already done by some module I'm using. Thus I thought it would be best to have a custom module with a block that would be called by a layout statement.
Of course I could easily edit my private local.xml or make changes to another layout-xml in the layout folder of my theme, but I want this feature to be available in all themes (independent of any selected theme).

Some constraints:

  • All code in one single module
  • ... so that it is theme independent
  • ... so that the module can be shared with others without them having to change anything (like theme files), so that the install/load of my module would be enough

I would also accept different approaches for my statistics hit counter loading (using the same constraints)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-12-08 06:40:05

是的,这是可能的。只需在以下路径中创建布局 xml 文件:/design/frontend/default/default/layout/yourlayout.xml(或任何主题名称),并在模块 etc/config.xml 中添加适当的语句:

<config> 
 <frontend>   
  <layout>
    <updates>
      <yourmoduleshortname>
         <file>yourlayout.xml</file>
      <yourmoduleshortname>
    </updates>
  </layout>  
 </frontend>
</config>

此示例适用于前端用户,但 adminhtml 布局可以以类似的方式更新。如果某些功能不起作用,请务必检查您的布局是否位于正确的主题/包目录中。

编辑:

第二种方法:

您可以使用自己的控制器,它将扩展核心功能(目录控制器之一) - 只需重写它(或只是产品视图)行动)。在其操作方法中添加如下内容:

$thiss->getLayout()->createBlock('namespacename/block','layout-block-name',
array('template' => 'relativepathtotemplate.phtml'));
$this->getLayout()->getBlock('content')->append($block);
run-original-parent-code();

第三种方法:

与前一种类似,但您可以使用一些事件观察器,并尝试 Mage::getSingleton('core/layout')< /code>,并在那里注入你的块。并非在所有情况下布局都可用(尝试 post_dispatch 系列)。

我真的不推荐第二种和第三种方法,因为如果其他人想找到这个“神奇”块的来源,它肯定会查找 int app/design/(...) 目录。在控制器或模型中找到它可能非常棘手...

如果您不想显示统计计数器,您还可以使用事件(如 post_dispatch)来计算控制器调度。只需创建一个附加到它的观察者,并将数据存储在数据库中。

Yes it is possible. Just create your layout xml file in the following path: /design/frontend/default/default/layout/yourlayout.xml(or whatever your theme name is), and add a proper statement in your modules etc/config.xml:

<config> 
 <frontend>   
  <layout>
    <updates>
      <yourmoduleshortname>
         <file>yourlayout.xml</file>
      <yourmoduleshortname>
    </updates>
  </layout>  
 </frontend>
</config>

This sample is for frontend user, but adminhtml layouts can be updated in a similar manner. If something doesn't work, be sure to check if your layout is in the proper theme/package directory.

Edit:

Second approach:

You can use a controller of your own, which will extend the core functionality (one of the catalog controllers) - just rewrite it (or just product view action). Inside its action method add something like this:

$thiss->getLayout()->createBlock('namespacename/block','layout-block-name',
array('template' => 'relativepathtotemplate.phtml'));
$this->getLayout()->getBlock('content')->append($block);
run-original-parent-code();

Third approach:

Similar to the previous one, but you can use some event observer, and try Mage::getSingleton('core/layout'), and inject your block there. Not in all events the layout will be already available (try the post_dispatch family).

I don't really recommend the second and third approach, because if someone else wants to find where this 'magic' block comes from, it will most surely look int app/design/(...) directory. Finding it in your controller or model, may be very tricky...

If you don't want to display your statistic counter, you can also use events (like post_dispatch) to count the controller dispatches. Just create an observer attached to it, and store your data in the DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文