0

I would like add a number of blocks into a custom block without having to create a template that literally has <?= $this->getChildHtml() ?> - is this possible?

  • I have added a layout update in my config.xml
  • I have create a core/template block with template test.phtml
  • The test.phtml contains only `getChildHtml() ?>

This works wonderfully, however I don't want to have to have the test.phtml I would like the children to just display automatically.

I have tried setting output="toHtml" which does not work.

4

edit

You can achieve this using page/html_wrapper default block. This block will render >all its child blocks without using a template and without using the call >getChildHtml().

if you are using core/template block as your parent block, it is not good to think about avoiding getChildHtml() method, since it is vastly using in lot of places in core Magento.

But if you are using custom block, there is a way to do this. The answer lies in core Magento itself. For example, if you look on the block Mage_Page_Block_Html_Wrapper block class, you can see a method _toHtml().

protected function _toHtml()
{
    $html = empty($this->_children) ? '' : trim($this->getChildHtml('', true, true));
    if ($this->_dependsOnChildren && empty($html)) {
        return '';
    }
    if ($this->_isInvisible()) {
        return $html;
    }
    $id          = $this->hasElementId() ? sprintf(' id="%s"', $this->getElementId()) : '';
    $class       = $this->hasElementClass() ? sprintf(' class="%s"', $this->getElementClass()) : '';
    $otherParams = $this->hasOtherParams() ? ' ' . $this->getOtherParams() : '';
    return sprintf('<%1$s%2$s%3$s%4$s>%5$s</%1$s>', $this->getElementTagName(), $id, $class, $otherParams, $html);
}

This method actually renders all its child block to html. Plus the all those blocks will come under a wrapper element(by default it uses div). Similarly you can use this method in your custom block

<?php

class Namspace_Modulename_Block_Custom_Block extends Mage_Core_Block_Abstract
{
    protected function _toHtml()
    {
        $html = empty($this->_children) ? '' : trim($this->getChildHtml('', true, true));
        return $html;
    }
}

Now if you put any number of block inside your custom block, it will automatically get rendered.

<layout>
    <[HANDLE_THAT_YOU_NEED]>
        <reference name="content">
            <block type="your_block_reference/custom_block" name="some.name">
                <!-- put any number of blocks here -->
            </block>
        </reference>
    </[HANDLE_THAT_YOU_NEED]>
</layout>

There is another alternative method that you can use. Just simply put only one statement inside your custom block template

<div><?php echo $this->getChildHtml('',true) ?></div>

This will also do the trick

  • This is similar to Arit Bera's answer from another angle, it looks like there is not way of achieving what I want without having a phtml template. – ash Aug 29 '14 at 11:21
  • no you dont need any phtml template, if you use the first method. For example wrapper block does not use any templates – Rajeev K Tomy Aug 29 '14 at 11:40
  • I've updated your answer as you're solution is basically what I had provided; except I placed it in app/code/local/Mage/Core/Block/Childhtml.php – ash Aug 29 '14 at 11:47
  • he he. good finding. you dont need to use any template for my answer also. And creating a module is not a big mistake, rather it is the real way to do that. The best pratice is the module way, not the "local" way. And this solution is just replicating my point of view in different way :) – Rajeev K Tomy Aug 29 '14 at 11:48
  • 1
    ash, I suggest you to use page/html_wrapper block. This will do the trick for you. You dont need to create your own custom block for that. – Rajeev K Tomy Aug 29 '14 at 11:57
2

Hi i read you question properly . i give simple way to print the number of blocks.

First of all you should to know about different type of blocks in magento.

       For understanding more about magento block types following are some built-in block types which are widely used in layout.

core/template: This block renders a template defined by its template attribute. The majority of blocks defined in the layout are of type or subtype of core/template.

page/html: This is a subtype of core/template and defines the root block. All other blocks are child blocks of this block.

page/html_head: Defines the HTML head section of the page which contains elements for including JavaScript, CSS etc.

page/html_header: Defines the header part of the page which contains the site logo, top links, etc.

page/template_links: This block is used to create a list of links. Links visible in the footer and header area use this block type.

core/text_list: Some blocks like content, left, right etc. are of type core/text_list. When these blocks are rendered, all their child blocks are rendered automatically without the need to call thegetChildHtml() method.

page/html_wrapper: This block is used to create a wrapper block which renders its child blocks inside an HTML tag set by the action setHtmlTagName. The default tag is if no element is set.

page/html_breadcrumbs: This block defines breadcrumbs on the page.

page/html_footer: Defines footer area of page which contains footer links, copyright message etc.

core/messages: This block renders error/success/notice messages.

page/switch: This block can be used for the language or store switcher.

0

Ash, i suggest that createblock class which inherite core/template and on this class on create a __construct() function and set a default templatethen by default it take this template.

just example

<?php
Class Mymodulenamespace_Mymodulename_Block_Myclass extends Mage_Core_Block_Template{
  public function __construct()
    {
        parent::__construct();
        $this->setTemplate('folerpath/test.phtml');
    }

}

  • So basically, no it is not possible to simplify what i've done. Shame that. Also your proposal is more work than what I already have :) – ash Aug 29 '14 at 10:45
  • the question is how to render child blocks without using getchildhtml() method and not how to set a template. I would downvote this answer, if you are not @AmitBera, since I have studied a lot from you :) – Rajeev K Tomy Aug 29 '14 at 12:02

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.