布局对象是块对象的集合。这些块以父/子树的形式组织。
您的布局有一个块。您尚未向其中添加任何子块。因此,当您的块的模板尝试使用 getChildHtml 渲染子级时,它找不到一个子级,并且不会产生额外的输出。
此外,默认情况下,topLinks 块不会呈现任何内容。它需要添加链接。这通常通过其他 layout.xml 文件完成。
最后,最好通过调用来开始渲染
echo $block_header->toHtml();
下面是一个如何正确嵌套块以及调用它们的操作方法的示例,这样您就可以在正常的调度过程之外渲染您的块。它与您的不同之处在于我们创建了一个新的链接块,向其添加链接,并将其添加到您的顶级块中。
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
$layout = Mage::getSingleton('core/layout');
$block_header = $layout->createBlock('page/html_header')->setTemplate('page/html/header.phtml');
// <block type="" name="top.links" as="topLinks"/>
$block_links = $layout->createBlock('page/template_links','top.links')->setTemplate('page/template/links.phtml');
$block_header->setChild('topLinks',$block_links);
//<reference name="top.links">
// <action method="addLink" translate="label title" module="customer"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position></action>
//</reference>
$block_links->addLink('My Account','foo/baz/bar/','My Account','','',10);
echo $block_header->toHtml();