仅在 Widget 页面上加载样式表

发布于 2024-10-20 14:52:52 字数 685 浏览 2 评论 0 原文

如何仅在包含自定义小部件的页面上将样式表动态加载到 Magento 的“head”部分?

对于我的脚本,我这样做:

if($this->getLayout()->getBlock('namespace_module_scripts')==false) {
    $block = $this->getLayout()
        ->createBlock('core/template', 'namespace_module_scripts')
        ->setTemplate('namespace/module/scripts.phtml');
    $this->getLayout()->getBlock('before_body_end')->insert($block);
}

这是我的 Widget 块类中 _prepareLayout() 的当前内容。我尝试对样式使用相同的方法,将 before_body_end 替换为 head,这对于放置 Widget 实例或手动将块放置在我的布局 xml 中非常有效,但是当插入在页面内容中内联小部件,头部部分似乎在小部件之前渲染,因此我的样式模板无法插入到头部块中。

我的(希望)临时解决方案是在布局 xml 中的每个页面上加载样式。有更好的方法吗?

How can I dynamically load stylesheets into the "head" section in Magento only on pages that include a custom Widget?

For my scripts I'm doing this:

if($this->getLayout()->getBlock('namespace_module_scripts')==false) {
    $block = $this->getLayout()
        ->createBlock('core/template', 'namespace_module_scripts')
        ->setTemplate('namespace/module/scripts.phtml');
    $this->getLayout()->getBlock('before_body_end')->insert($block);
}

This is the current content of _prepareLayout() in my Widget block class. I tried using the same method for styles, swapping before_body_end for head, and that worked great for placing Widget Instances or manually placing the block in my layout xml, however when inserting a widget inline in a Page's content the head section seems to be rendered prior to the Widget, and therefore my styles template can't be inserted into the head block.

My (hopefully) temporary solution is to load the styles on every page in my layout xml. Is there a better way to do this?

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

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

发布评论

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

评论(1

凑诗 2024-10-27 14:52:52

看来您对通过标签插入 CMS 页面的小部件非常熟悉

{{widget...}}}

。与实例不同,或者通过包布局 XML 更新文件直接添加实例,Magento 似乎可以动态渲染这些块。也就是说,直到在 CMS 内容块上调用 toHtml 时,Magento 才会实例化 {{widget...}} 块。到那时,头部块已经呈现,并且以编程方式操作它不再有任何效果。

这是我要采取的方法。听起来添加 javascript 仍然有效(这是有道理的,因为 CMS 内容块将在 before_body_end 块之前渲染。)研究 通过 Javascript 加载 CSS 文件(旧链接,如果你想要 web 2ish 的东西,请谷歌一下)。这样你就可以添加一个可以添加 CSS 文件的 javascript 块。这样,您的所有代码仍然局限于小部件,并且您仍然可以验证 html 输出。这是一种非常常见的技术。

其他方法包括

  1. 刚刚将样式添加到内联文档中。糟糕的 HTML 实践以及所有这些(样式不在头部),但您不会是第一个这样做的人。

  2. 有一个始终添加到头部的块,并且包含条件渲染逻辑。在渲染之前,让它检查小部件的布局对象(通过类型),或者让它检查此请求是否针对 CMS 页面,如果是,则检查 CMS 页面的内容是否有指令 ({{widget ...}}) 用于渲染您的小部件。如果任一为 true,则呈现 CSS 链接。如果不是,则不会呈现链接。

It looks like you're SOL with widgets you're inserting into CMS pages via the

{{widget...}}}

tag. Unlike instances, or adding it directly via a Package Layout XML update file, Magento appears to render these blocks on the fly. That is, its not until toHtml is called on the CMSs content block that Magento instantiates a {{widget...}} block. By then the head block has already rendered, and manipulating it programmatically no longer has any effect.

Here's the approach I'd take. It sounds adding javascript still works (which makes sense, as the CMS content block will be rendering before the before_body_end block.) Look into techniques for loading CSS files via Javascript (older link, google around if you want something web 2ish). That way you can add a javascript block that can add a CSS file. This way all your code is still confined to the widget, and you still get validating html output. It's a pretty common technique.

Other approaches include

  1. Just added your styles to the document inline. Bad HTML practices and all that (style aren't in the head), but you wouldn't be the first person to do this.

  2. Having a block that's always added to the head, and contains conditional rendering logic. Immediately prior to rendering, have it check the layout object for your widget (via the type) OR have it check if this request is for a CMS page, and if it is check CMS page's content for a directive ({{widget...}}) for rendering your widget. If either is true, have CSS link rendered. If it isn't, the link doesn't render.

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