Static Block是Magento提供的非常实用的一种Block类型。通过Static Block,可以让不懂代码的店铺管理员轻易修改网站中的一些动态内容,而不需要修改Magento的核心代码。Magento Static Block的创建相当简单,通过后台CMS->Static Block即可查看或新建Static Block。建立完毕之后,可以在任意的Magento页面添加该段代码。
本文从创建Static Block开始讲起,然后包括在布局文件,模板文件及CMS页面中添加创建的Static Block。最后,通过这些知识,运用到实际的Magento模版开发当中,我们讲述如何在Magento的首页添加一个幻灯片。
创建Static Block
在后台菜单cms–>static block位置,新建立一个static block,如下图,并根据如下提示,了解每个域的大致作用。
Block Title:随便填写,方便识别位置与作用
Identifier:全局唯一,用于在模板或布局文件中调用
Store View:可见、可调用范围
Content:这里只需要填写HTML代码即可,没有太特殊的东西。值得注意的是这里引用图片,添加链接的方式。
添加图片方式,
<img src=”{{media url=”home_banner.jpg”}}” alt=”" width=”950″ height=”280″ />
添加链接方式,
<a href=”{{store url=’customer/account/login/’}}”>Login</a>
在布局文件中添加Static Block
Static Block在CMS菜单下,其统一的Block类型是type=”cms/block”,在布局文件中看到的此类Block一律是在CMS下的Static Block中定义的。Static Block在布局文件中的添加方式如下。
01
02
03
04
05
|
< block type = "cms/block" name = "block_name" >
< action method = "setBlockId" >
< id >block_id</ id >
< action >
</ block >
|
<id>标签内的值即在创建Static Block时填写的Identifier。另外这里定义了block的name属性,用于在模板文件中通过getChildHtml()方法引用该Static Block。
在模板文件中调用Static Block
在模板文件中调用Static Block有两种方法。
a)第一种是通过getChildHtml()方法调用,使用该方法的前提是在布局文件中,需要调用的Static Block已经在某一个Block内形成了父子关系,然后可以在父Block中使用getChildHtml()方法。
01
|
echo $this ->getChildHtml( 'block_name' )
|
b)第二种方法是在模板文件中使用createBlock()创建一个Block,然后再将Static Block赋予新创建的Block,然后toHtml()输出。使用该种方法不需要在布局文件中提前引用该Static Block。
01
|
echo $this ->getLayout()->createBlock( 'cms/block' )->setBlockId( 'foot_links' )->toHtml()
|
在CMS页面添加Static Block
在CMS页面中添加Static Block同样非常简单,上面提到过所有的Static Block都属于cms/block类型,name属性可以随便取,建议保持一定的相关性,并且全局唯一,block_id属性即建立Static Block时填写的identifier。
{{block type=”cms/block” name=”cms_test_block” block_id=”order_form”}}
相关推荐
Ability to place any block in any position by easily changing CMS page layout Unlimited number of blocks inside each position Unlimited number of content items inside each block Block items can ...
magento中block应用 magento中block应用 magento中block应用
Chapter 3, Enabling Performance in Magento 2, explains how to configure different types of caching options. In Magento 2, the Full Page Cache (FPC) can be handled by Varnish to give your store a ...
Chapter 3, Enabling Performance in Magento 2, explains how to configure different types of caching options. In Magento 2, the Full Page Cache (FPC) can be handled by Varnish to give your store a ...
Apress.The.Definitive.Guide.to.Magento.Dec.2009.pdf Apress于2009重磅推出的Magento上手书籍,原书地址:http://www.apress.com/book/view/9781430272298
这是一个关于matento在代码中调用cms块得说明
Magento user guide, useful Magento doc to instructor user build and manage their stores
You’ll start by getting a general understanding of what Magento is, why and how you should use it, and whether it is possible and feasible to migrate from an old web store to Magento 2. As you work ...
magento入门原版书.。。The Definitive Guide to Magento.pdf
NULL 博文链接:https://justcoding.iteye.com/blog/1886183
NULL 博文链接:https://justcoding.iteye.com/blog/1711166
对于首次安装magento过程中出现的问题的小结
magento数据结构分析 magento数据字典
两本关于Magento的点子书 希望大家喜欢 Apress.The.Definitive.Guide.to.Magento.Dec Magento 1.3 Theme Design
learning to use Magento. You’ll find lots of step-by-step instructions, screenshots and examples, plus a comprehensive online configuration reference that is linked throughout the material. After ...
magik shoes magento 模板, magento 1.7 模板
magento的SMTP插件magento的SMTP插件magento的SMTP插件
Guide to Programming with magento.pdf。编程手册
深入理解Magento 由精东博客下载下来制作成pdf