修改layout中的xml代码
<reference name=”content”>
<block type=”cms/block” name=”blockname” before=”-”>
<action method=”setBlockId”><id>block_id</id></action>
</block>
</reference>
通过修改reference 中的name属性值调整主体位置,通过block中的before或after属性值调整相对顺序
CMS 页以及static block还可以通过如下方法调用:
{{block type=”cms/block” name=”user-block” block_id=”one-block”}}(后台内容中引用)
(其中的name为自定义的,block_id 为静态块的 identifier)
2、直接在模板的 .phtml 中调用
<?php echo $this->getLayout()
->createBlock(‘cms/block’)
->setBlockId(‘block_id’)
->toHtml(); ?>
<?php echo $this->getLayout()->createBlock('banner/banner')->setTemplate('unibanner/banner.phtml')->setBannerGroupCode('paper_banner')->toHtml();?>
3、在对应的 .xml 中的目标位置调用
调用cmsblock
<block type=”cms/block” name=”blockname” as=”blockname” before=”-”>
<action method=”setBlockId”><id>block_id</id></action>
</block>
调用自己写好的模板,在模块中写好block,以及template后在layout对应起来。
模板中还有一种<block type="banner/banner" name="banner.left" template="unibanner/banner.phtml"/>
4、使用 getChildHtml() 方法调用(类似方法3):
首先在page.xml文件中定义一个childhtml名称
<block type="page/html_header" name="other_header" as="other_header" template="page/html/other_header.phtml">
<block type="core/text_list" name="top.menu" as="topMenu" translate="label">
<label>Navigation Bar2</label>
<block type="catalog/navigation" name="catalog.topnav1" template="catalog/navigation/top.phtml"/>
</block>
</block>
</reference>
后在模板中<?php echo $this->getChildHtml('topMenu')?>
Magento中的block类
Block分类:
继承分支:Mage_Core_Block_Abstract,自定义的Mage_core_Block_Text
1.没有template的block,default节点下的left,content,只能包含子block,在其父block的template文件输出
例如:
3columns.phtml<?php echo $this->getChildHtml(‘left’) ?>
2.有template的block有2种:
-----容器blcok(有自己的template文件,可以布局也可以直接内容,包括子block,在它自己的template中可以通过<?php echo $this->getChildHtml(‘l子blcok的别名’) ?>
-----纯内容block,有自身的template(也就是自身的html内容),template直接包含内容,没有子block。
一般我们调用block基本方法:
第一种:后台文本编辑器中调用block
在CMS页面中添加Static Block同样非常简单,上面提到过所有的Static Block都属于cms/block类型,name属性可以随便取,建议保持一定的相关性,并且全局唯一,block_id属性即建立Static Block时填写的identifier。
{{blocktype=”cms/block” name=”user-block” block_id=”one-block”}}
第二种:模板中调用
模板文件中使用createBlock()创建一个Block,然后再将Static Block赋予新创建的Block,然后toHtml()输出。使用该种方法不需要在布局文件中提前引用该Static Block。
<?phpecho$this->getLayout()->createBlock(‘cms/block’)->setBlockId(‘block_id’)->toHtml();?>
第三种:
通过getChildHtml()方法调用,使用该方法的前提是在布局文件中,需要调用的Static Block已经在某一个Block内形成了父子关系,然后可以在父Block中使用getChildHtml()方法。
|
<block type=”cms/block” name=”block.name” as="blockname"before=”-”>
<action method=”setBlockId”><id>block_id</id></action>
</block>
</reference>
<?phpecho $this->getChildHtml(‘blockname’) ?>
第四种:类似3(自定义的Mage_core_Block_Text)
1)<blocktype="core/text_list" name="top.menu"as="topMenu" translate="label">
<label>Navigation Bar</label>
</block>
<referencename="top.menu">
<block type="catalog/navigation"name="catalog.topnav" template="catalog/navigation/top.phtml"/>
</reference>
<?phpecho $this->getChildHtml(‘topMenu') ?>
2) <blocktype="core/text_list" name="content-bottom"as="content-bottom" translate="label">
<label>Content BottomColumn</label>
</block>
后台页面中的design调用
<referencename="content-bottom">
<blocktype="cms/block" name="content-box"before="footer_links">
<actionmethod="setBlockId"><block_id>content-box</block_id></action>
</block>
</reference>
<divclass="content-bootm"><?php echo $this->getChildHtml('content-bottom')?></div>