Magento:为静态块小部件注册自定义模板文件

Magento: register custom template files for static block widget

我开始学习如何使用 Magento。我喜欢静态块的概念,并且使用静态块小部件将它们添加到 cms 页面非常容易。 该小部件为我提供了一个下拉菜单,我可以在其中选择它应该使用的模板文件。默认情况下,只有默认模板文件位于 cms/widget/static_block/default.phtml

对话框如下:

如何将自己的模板文件添加到下拉列表中?

我知道小部件只会创建以下行:

{{widget type="cms/widget_block" template="cms/widget/static_block/default.phtml" block_id="8"}}

而且我知道我可以轻松更改 "template" 属性。但我认为这不是一个干净的方式,因为在未来,用户不知道正确的文件名,他们只使用下拉菜单会容易得多。

希望我能解释一下问题,感谢您的帮助!

出于此回答的目的,我假设您了解 Magento 扩展程序的工作原理,但如果不了解,也有很多关于此主题的好答案。

小部件由 widget.xml 文件定义,因此您需要将 widget.xml 文件添加到新扩展或现有扩展的 etc 子目录(即 app/code/local/My/MyExtension/etc ).由于 CMS 静态块小部件已在 app/code/core/Mage/Cms/etc/widget.xml 中定义为 <cms_static_block>,您的新 widget.xml 文件将只是将新模板节点注入到此小部件的现有 XML 配置中.这是您的新 widget.xml 的样子:

<?xml version="1.0"?>
<widgets>
    <cms_static_block>
        <parameters>
            <template>
                <values>
                    <custom translate="label">
                        <value>cms/widget/static_block/custom.phtml</value>
                        <label>My Extension - Custom CMS Block Template</label>
                    </custom>
                </values>
            </template>
        </parameters>
    </cms_static_block>
</widgets>

您可能已经知道,<value> 节点指的是自定义模板的相对位置。所以在我上面的例子中,它在这里引用了一个模板文件(无论你的自定义 package/theme 目录是什么):app/design/frontend/my_package/my_theme/template/cms/widget/static_block/custom.phtml