请遵循下面的代码,它取自我的Magento Commerce主题:
从layout/page.xml提取
<block type="page/html_header" name="header" as="header">
<block type="page/template_links" name="top.links" as="topLinks"/>
<block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
<block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
<block type="directory/currency" name="store_currency_selector" as="store_currency_selector" template="directory/currency_top.phtml"/>
<block type="core/text_list" name="top.menu" as="topMenu" translate="label">
<label>Navigation Bar</label>
<block type="page/template_links" name="top.links.mobile" as="topLinksMobile"/>
<block type="checkout/cart_sidebar" name="cart_sidebar_mobile" as="cartSidebarMobile" template="checkout/cart/topbar.phtml"/>
</block>
<block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
<label>Page Header</label>
<action method="setElementClass"><value>top-container</value></action>
</block>
<block type="checkout/cart_sidebar" name="cart_sidebar" as="topcart" template="checkout/cart/topbar.phtml"/>
</block>从模板/目录/导航/top.phtml中提取
<li class="level0 nav-2 active level-top first parent">
<a href="javascript:;">ACCOUNT</a>
<?php echo $this->getParentBlock()->getChildHtml('topLinksMobile'); ?>
</li>
<li class="level0 nav-3 active level-top first parent">
<a href="javascript:;">CART</a>
<?php echo $this->getChildHtml('cartSidebarMobile'); ?>
</li>基本上,我要做的是在"topMenu“块中创建两个子块,然后使用"getChildHtml”函数将它们打印到模板中。
不幸的是,我的函数调用失败了,因为在top.phtml生成内容之前加载了这两个块。
如果我做错了什么,请给我一些建议。
在此之前,非常感谢您。
发布于 2013-05-17 15:47:51
尝试调用以下文件template/page/html/topmenu.phtml中的函数
发布于 2013-05-17 17:30:56
我已经进步了一点。
阅读:Magento - display block but only show when I call it with getChildHtml
和:Understanding Magento Block and Block Type
我知道core/text_list会自动打印内容,所以我将类型改为"page/html_wrapper“。
问题是现在这两个元素的内容是重复的。一次在top.phtml的内容之前,第二次在调用getChildHtml时。
任何想法都将不胜感激。
发布于 2022-01-31 07:03:22
getChildHtml方法是块/模板的真正威力。它允许你渲染一个辅助块(“子”)中的所有块。在主块(“父”)内。块调用块调用块是创建页面的整个HTML布局的方式。
格式:
getChildHtml('block_name');?>
注意:在PHTML模板文件中使用$block而不是$this,因为根据Magento2编码标准,不鼓励使用$this
如果该命令可以在模板文件中的任何位置找到block_name,它将为您获取block_name的超文本标记语言,前提是block_name是当前块的子级。
例如,让我们看一看module-wishlist的模板文件(view.phtml)的摘录:
helper(\Magento\Wishlist\Helper\Data::class)->isAllow()):?>
<div class="toolbar wishlist-toolbar"><?= $block->getChildHtml('wishlist_item_pager'); ?></div>
<?= ($block->getChildHtml('wishlist.rss.link')) ?>在这里,我们可以看到类wishlist-toolbar的内容由块wishlist_item_pager和wishlist.rss.link呈现。这些块是子块,在位于vendor\magento\module-wishlist\view\frontend\layout):中的wishlist_index_index.xml中定义
Note: The getChildHtml method can only include blocks that are specified as sub-blocks (child) in the Layout. This allows Magento to only instantiate the blocks it needs, and also allows you to set different templates for blocks based on context.https://stackoverflow.com/questions/16595123
复制相似问题