2

I just can't figure it out... This outputs the correct html:

file: /design/default/template/catalog/product/view.phtml

<?php 

    echo $this->getLayout()
        ->createBlock('core/template')
        ->setTemplate('catalog/product/view/teasers.phtml')
        ->toHtml(); 

?>

But if I declare the block in local.xml and use getChildHtml(), it doesn't work:

file: /design/default/layout/local.xml

<catalog_product_view translate="label">
    <reference name="content">
        <block  
            template="catalog/product/view/teasers.phtml"
            type="core/template"
            name="teasers" 
            as="teasers" 
        ></block>
    </reference>
</catalog_product_view>

file: /design/default/template/catalog/product/view.phtml

<?php 
    echo $this->getChildHtml('teasers'); 
?>

Do I need something else??

4

2 回答 2

1

This question doesn't quite make sense to an experienced Magento developer. Hopefully explaining why it doesn't make sense help you find the solution to your problem.

First, your path for local.xml is incorrect

/design/default/layout/local.xml

Magento's structure here is

app/design/frontend/package-name/theme-name/layout/local.xml

You're missing the package and/or theme name from your path. That makes it hard to give you advice here — are you sure Magento sees your local.xml file? (making it invalid XML and reloading looking for an error is a quick way to test this)

Secondly, you said you're using getChildHtml to render the block — but at the same time you're adding it to the content block. The content block is a test/list block that will automatically render any block added to it. The content block has no phtml template, therefore you can't call getChildHtml. Where are you calling getChildHtml from?

于 2013-06-21T19:33:39.500 回答
1

I figured out the problem.

First of all, the .phtml file was being displayed, but near the bottom of the page. Since I was calling getChildHtml near the top of the page, I never scrolled down enough to notice (big fat doh).

Secondly, I wasn't aware that you can't use getChildHtml for the content block. By trial and error I found the solution was in changing the reference from "content" to "product.info" (only 9 hours down the drain this time, not bad).

Layout file looks like this now:

<catalog_product_view translate="label">
    <reference name="product.info">
        <block  
            template="catalog/product/view/teasers.phtml"
            type="core/template"
            name="teasers" 
            as="teasers" 
        ></block>
    </reference>
</catalog_product_view>
于 2013-06-22T12:03:42.130 回答