0

How can I access variables defined in the parent template inside a child template?

Example -> I added a new block with name product.info.grouped.news_flag as child of the block with name product.info.grouped in the catalog.xml layout of my design:

<PRODUCT_TYPE_grouped translate="label" module="catalog">
    <label>Catalog Product View (Grouped)</label>
    <reference name="product.info">
        <block type="catalog/product_view_type_grouped" name="product.info.grouped" as="product_type_data" template="catalog/product/view/type/grouped.phtml">
            <block type="core/text_list" name="product.info.grouped.extra" as="product_type_data_extra" translate="label">
                <label>Product Extra Info</label>
            </block>

            <!-- My new block -->
            <block type="core/template"
                   name="product.info.grouped.news_flag"
                   as="product_type_data_news_flag"
                   template="catalog/product/view/type/news_flag.phtml"/>

        </block>
    </reference>
</PRODUCT_TYPE_grouped>

This is the part in the template file of the parent block where I call my new block with name product.info.grouped.news_flag:

catalog/product/view/type/grouped.phtml

<?php 
    $testVar = 123;
    echo $this->getChildHtml('product_type_data_news_flag');
?>

As you can see I created a variable named $testVar with value 123.

But If I try to call the value in the child block product_type_data_news_flag then I get null:

catalog/product/view/type/news_flag.phtml

<?php
    $a = $testVar;
    echo $a; // returns null

How can I pass a variable from the parent block/template to the child block/template?

2

I see at least two possibilities :

1) In the parent block, set your data with some $this->setMyVariable('my_value') or using layout xml declaration. Then, in your child block, you should get the value with some $this->getParentBlock()->getMyVariable()

2) Or you can use the method used here

1

Alternatively, you can use Mage::register('some_var', $someVar) on parent block and Mage::registry('some_var') on child block to get the value. But in general, Julien's answer suits better.

0

I found a way. I changed the type of the child block class to the same type as the parent block.

<PRODUCT_TYPE_grouped translate="label" module="catalog">
    <label>Catalog Product View (Grouped)</label>
    <reference name="product.info">
        <block type="catalog/product_view_type_grouped" name="product.info.grouped" as="product_type_data" template="catalog/product/view/type/grouped.phtml">
            <block type="core/text_list" name="product.info.grouped.extra" as="product_type_data_extra" translate="label">
                <label>Product Extra Info</label>
            </block>

            <!-- My new block -->
            <block type="catalog/product_view_type_grouped"
                   name="product.info.grouped.news_flag"
                   as="product_type_data_news_flag"
                   template="catalog/product/view/type/news_flag.phtml"/>

        </block>
    </reference>
</PRODUCT_TYPE_grouped>

Now I can override the block class and define my variables there so I can use it in all childs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.