题
我想要实现的是我在 cms/homepage/design 中有一些 html 代码,我想将其插入到主页中的某个元素之后。这是 xml 代码:
<cms_index_index translate="label">
<reference name="content">
<block type="sama_productblocks/list" name="sama_productblocks_newproducts" after="call_to_actions" template="sama_productblocks/list.phtml">
<action method="setIdentifier"><id>homepage-latest</id></action>
</block>
<block type="core/template" name="top_banners" before="-" template="homepage/topbanners.phtml">
<block type="experius_linkmanager/link_list" name="homepage_banner">
<action method="setIdentifier"><key>hoesjesoutlet-homepage_banner</key></action>
</block>
<block type="experius_linkmanager/link_list" name="homepage_small">
<action method="setIdentifier"><key>hoesjesoutlet-homepage_small</key></action>
</block>
</block>
<block type="core/template" name="cta_banners" after="modelfilter_search" template="homepage/cta.phtml">
<block type="experius_linkmanager/link_list" name="homepage_cta">
<action method="setIdentifier"><key>hoesjesoutlet-homepage_cta</key></action>
</block>
</block>
<block type="core/template" name="home_text" after="modelfilter_search" template="homepage/text.phtml">
<!-- Here should I call the html code -->
</block>
</reference>
</cms_index_index>
text.phtml 的内容应该是设计选项卡中的 html 代码。好吧,我不想将其从设计中删除并将其全部放入 html 代码中。有没有一个函数或方法可以从主页调用设计内容?这是template/homepage/text.phtml的内容
<div class="cta_row">
<?php // echo $this->getChildHtml('homepage_text');
// here i want to put the html code from the design tab from homepage
?>
</div>
解决方案
将 html 内容放入新的 phtml 文件中。你应该这样做。
文件: app\design\frontend\{your_package}\{your_theme}\template\homepage\my_homepage.phtml
<!-- put your html code inside this file -->
即你的html代码在里面 my_homepage.phtml
模板。现在像这样更新 xml 代码的最后一部分。
<block type="core/template" name="home_text" after="modelfilter_search" template="homepage/text.phtml">
<!-- call to your custom html code block -->
<block type="core/template" name="my.homepage.block" as="homepage_text" template="homepage/my_homepage.phtml" />
</block>
现在请确保您的 text.phtml
保留这个电话..
<?php echo $this->getChildHtml('homepage_text') ?>
然后清除所有缓存并重新加载页面。你完成了。
编辑
如果您希望这些 html 代码可以从管理员编辑,那么正如您在评论中所述,将这些 html 代码放入带有标识符的静态块中 homepage_custom_code
(你可以在这里使用任何东西)然后像这样改变你的xml代码。
布局更新方法
<block type="core/template" name="home_text" after="modelfilter_search" template="homepage/text.phtml">
<!-- call to your custom html code block -->
<block type="cms/block" name="my.homepage.sb" as="homepage_text">
<action method="setBlockId">
<block_id>homepage_custom_code</block_id>
</action>
</block>
然后调用您的自定义块 text.phtml
就像我们上面做的一样^^^^。
硬编码方法 :(我讨厌这个。)
否则直接在你的内部调用你的静态块 text.phtml
像这样。
<?php
$this->getLayout()->createBlock(
'cms/block',
'my_custom_text_from_sb',
array('block_id' => 'homepage_custom_code')
)
->toHtml()
?>
两种方法都有效。但我推荐第一种方法。对我来说,它看起来更清晰。