在本系列的第一部分中,我们学习了 magento 模块开发的基础知识,包括 magento 目录结构、自定义模块结构,并创建了一个基本的“hello world”模块,以了解控制器如何在 magento 中工作。
在本文中,我们将学习如何创建块和布局文件。具体来说,我们将了解布局文件和块文件在 Magento 中如何工作,并且我们将了解布局文件的渲染。
如果您正在寻找快速解决方案,Envato Market 上有大量 Magento 主题和模板。这是为您的项目快速构建高质量低多边形项目集合的好方法。

但是,继续教程吧!首先,我们将了解布局文件和块文件是什么以及它们在 Magento 中渲染前端页面时如何有用,然后我们将了解如何将它们包含在我们的自定义模块中。
顾名思义,布局文件在渲染 Magento 的首页时非常有用。布局文件是 XML 文件,位于应用 > 设计 > 前端 > 界面 > 主题 > 布局 中。 在这里,您可以看到任何给定模块都有许多布局文件。每个 Magento 模块都有自己的布局文件,就像客户模块有 customer.xml 布局文件一样,目录模块有 catalog.xml 布局文件等。这些布局文件包含结构块和内容块。
如果您想知道为什么 Magento 需要这些块,您可以在本系列的第一部分中了解更多信息。
让我们通过一个示例来深入了解布局文件。转到应用 > 设计 > 前端 > 基础 > 布局 并打开 customer.xml 我>文件。在这里,所有块都围绕主
请参阅以下代码片段:
<!--
New customer registration
-->
<customer_account_create translate="label">
<label>Customer Account Registration Form</label>
<!-- Mage_Customer -->
<remove name="right"/>
<remove name="left"/>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="customer/form_register" name="customer_form_register" template="customer/form/register.phtml">
<block type="page/html_wrapper" name="customer.form.register.fields.before" as="form_fields_before" translate="label">
<label>Form Fields Before</label>
</block>
</block>
</reference>
</customer_account_create>
句柄是 Magento 在调用特定模块时识别要加载的块的主要实体。
每个处理特定于页面的内容嵌套块。某些布局文件包含
句柄内部有不同的块,它们指定调用该块时要渲染的模板文件。块有两种类型:
在布局文件中,我们仅定义内容块 ,然后将它们包装在结构器块中。 例如,如果有人正在调用客户注册页面,并且我们希望将其加载到左侧、右侧、内容或页脚,我们会将该块包装在其各自的结构块中。在这里,我们在“内容”块内包装了两个块,这是一个结构块。
块包含如下属性:
当您将一个块包装在另一个块下时,该包装块称为子块。每当我们的模块调用父块时,就会自动调用子块。
<block type='core/template' name='parent' template='parent.phtml'> <block type='core/template' name='child' template='child.phtml'/> </block>
您也可以在模板文件中使用以下语法单独调用子块 echo $this->getChildHtml('child');
打开page.xml 布局文件,你会发现
<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
Magento 从根块开始渲染。所有其他块都是根块的子块。根块定义页面的结构。在这里,您可以看到当前它设置为 3columns.phtml,您可以将其更改为 1column.phtml、2columns-right.phtml 或2columns-left.phtml.
对于任何特定页面,您可以将 CSS 和 JavaScript 文件添加到布局标记中,如下所示:
<customer_account_create> <reference name='head'> <action method="addCss"><stylesheet>css/styles.css</stylesheet></action> <action method="addJs"><script>varien/js.js</script></action> </reference> </customer_account_create>
在这里您可以看到我们在客户帐户页面的 head 中添加了一个 CSS 文件和一个 JavaScript 文件。
块类用于定义特定于特定块的功能。块类文件位于应用程序>代码>本地/社区/核心>您的模块命名空间>您的模块名称>块目录中。这些文件包含我们可以直接与 $this 块特定模板文件中的关键字。让我们通过一个例子来了解块类。
转到位于 app > design > frontend > base > default > layout 目录中的 review.xml 文件,并找到以下代码行:
<!--
Customer account home dashboard layout
-->
<customer_account_index>
<!-- Mage_Review -->
<reference name="customer_account_dashboard">
<block type="review/customer_recent" name="customer_account_dashboard_info1" as="info1" template="review/customer/recent.phtml"/>
</reference>
</customer_account_index>
在这里您可以看到引用模板 review/customer_recent 的块 review/customer_recent ">最近.phtml。 转到应用 > 设计 > 前端 > 基础 > 默认 > 模板 > 审核 > 客户 并打开 最近的.phtml。
在此文件中,您可以看到使用 $this 关键字调用两个函数。它们是 $this->getCollection() 和 $this->count()。 这些函数在其块类文件 recent.php 中定义,该文件位于 应用 > 代码 > 核心 > Mage > 审查 > 阻止 > 客户 目录。
这里,块 type = "review/customer_recent" 指的是在 recent. 文件中定义的 Mage_Review_Block_Customer_Recent 块类。无论您在此类中定义什么函数,都可以直接在相应的模板文件中使用 $this 来使用它。
最后,我们留下了带有控制器的自定义“Hello World”模块。在这里,我们创建了自定义模块的布局文件。所以让我们创建它。
要创建布局文件,我们需要首先创建块类文件。在添加类文件之前,我们需要告诉模块我们正在包含块文件。因此,转到 app > code > local > Chiragdodia > Mymodule > etc > config.xml 并添加以下内容代码行:
<frontend>
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file> <!-- Our layout file name-->
</mymodule>
</updates>
</layout>
</frontend>
<global>
<blocks>
<mymodule>
<class>Chiragdodia_Mymodule_Block</class>
</mymodule>
</blocks>
</global>
最终的 XML 文件包含以下代码行:
<?xml version="1.0"?>
<config>
<modules>
<Chiragdodia_Mymodule>
<version>0.1.0</version> <!-- Version of module -->
</Chiragdodia_Mymodule>
</modules>
<frontend>
<routers>
<mymodule>
<use>standard</use>
<args>
<module>Chiragdodia_Mymodule</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file> <!-- Our layout file name-->
</mymodule>
</updates>
</layout>
</frontend>
<global>
<blocks>
<mymodule>
<class>Chiragdodia_Mymodule_Block</class>
</mymodule>
</blocks>
</global>
</config>
接下来,转到 app > code > local > Chiragdodia > Mymodule > Block 并创建文件 Mymodule.php 包含以下代码行
<?php
class Chiragdodia_Mymodule_Block_Mymodule extends Mage_Core_Block_Template
{
public function myfunction()
{
return "Hello tuts+ world";
}
}
这里我们声明了类 Chiragdodia_Mymodule_Block_Mymodule ,其中包含函数 myfunction ,我们可以直接从布局模板文件中调用它。
转到app > design > frontend > default > default > layout 并创建 mymodule.xml 文件,其中包含以下代码行
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_index_index>
<reference name="content">
<block type="mymodule/mymodule" name="mymodule" template="mymodule/mymodule.phtml" />
</reference>
</mymodule_index_index>
</layout>
转到应用 > 设计 > 前端 > 默认 > 默认 > 模板 并创建 mymodule.phtml 文件。在此文件中,我们将调用我们在块类中声明的函数 myfunction 。
<?php
echo $this->myfunction();
?>
如果到目前为止一切都正确,您将通过访问 URL yoursite.com/index.php/mymodule/index 看到具有三列布局的输出。
在某些 Magento 版本中,默认主题不包含布局和模板目录。在这种情况下,您可以在app > design > frontend > base 目录中创建布局和模板文件。
这就是 Magento 中布局的工作原理。在上一篇文章中,我们创建了简单的“Hello World”模块,在本文中我们使用布局文件创建它。 Magento 的布局结构一开始有点难以理解,但是一旦你开始修改它,你就会很容易地理解它背后的想法。
在这篇文章中,我附上了我们迄今为止创建的模块演示文件。如果您对此特定问题有任何疑问,请随时发表评论并提出任何问题。
上文就是使用Magento进行自定义布局和模板设计的内容了,文章的版权归原作者所有,如有侵犯您的权利,请及时联系本站删除,更多相关magento模板的资讯,请关注收藏西西下载站。