通过自定义模块和块插入开放图标签
-
12-12-2019 - |
题
我正在使用 Magento EE 1.6
当我使用自定义块类在我的内部显示一些标签时 <head>
, ,块不会显示。但如果我使用 "page/html"
类型,一切都很好......我需要我的自定义类向块添加一些功能(激活、获取图像 url 等)。
请帮助我理解我错过了什么。这是我到目前为止所做的:
应用程序/etc/modules/Wbx_Social.xml:
<?xml version="1.0"?>
<config>
<modules>
<Wbx_Social>
<active>true</active>
<codePool>local</codePool>
</Wbx_Social>
</modules>
</config>
应用程序/代码/本地/Wbx/Social/etc/config.xml :
<?xml version="1.0"?>
<config>
<modules>
<Wbx_Social>
<version>0.1.0</version>
</Wbx_Social>
</modules>
<global>
[...]
<blocks>
<wbx_social>
<class>Wbx_Social_Block</class>
</wbx_social>
</blocks>
</global>
<frontend>
<layout>
<updates>
<wbx_social>
<file>wbx_social.xml</file>
</wbx_social>
</updates>
</layout>
</frontend>
</config>
应用程序/代码/本地/Wbx/Social/Block/Opengraph.php:
class Wbx_Social_Block_Opengraph extends Mage_Core_Block_Template
{
/**
* @return int
*/
public function isActive()
{
return true; // Will be replaced with store config value
}
/* And other useful functions */
}
应用程序/设计/前端/响应/默认/布局/wbx_social.xml:
<?xml version="1.0"?>
<layout>
<default>
<reference name="head">
<block type="wbx_social/opengraph" name="opengraph" as="opengraph" template="social/opengraph.phtml"/>
</reference>
</default>
</layout>
最后是模板:应用程序/设计/前端/响应/默认/模板/社交/opengraph.phtml:
<?php /* @var $this Wbx_Social_Block_Opengraph */ ?>
<?php if ($this->isActive()) : ?>
<meta property="fb:app_id" content="an id"/>
<meta property="og:type" content="website"/>
<meta property="og:site_name" content="a name"/>
<?php endif; ?>
以防万一,我没有忘记 Wbx_Social_Helper_Data
空帮手。
在 wbx_social.xml
, ,当我尝试更改块类时 type="core/template"
或者 type="page/html"
(例如),该块正确显示(当然没有任何自定义函数的访问)
我真的需要一个提示,我已经花了几个小时了......
解决方案 2
好吧,我找到了问题的根源。问题确实出在我的街区 Wbx_Social_Block_Opengraph
实现了一个 getUrl()
方法。大错 ! Mage_Core_Block_Abstract
(我的块的祖先)已经有一个 getUrl()
方法但具有不同的签名。我真正的大问题是:为什么我没有收到任何错误/警告?本来可以节省很多时间...
无论如何,我改变了自己的方法 getOgUrl()
瞧!我的标签按照我想要的方式显示......
非常感谢您的建议,伙计们!
其他提示
Magento 中有多种块类型。参见app\code\core\Mage\Core\Block\
其中之一是“列表”。它获取所有孩子并一一输出。这 content
块的工作原理如下。
但大多数块都有“模板”类型。这意味着它们有一些关联的 .phtml 模板并呈现它们。例如,您的块有这样的类型。
要在 head ( type="template" ) 中添加一些块,您必须编辑 head.phtml 文件并使用以下命令调用子块 $this->getChildHtml('opengraph')