4

In layout.xml you can define children like this

<block...><block... /></block>

Is the same possible if I add a block in a static block? I only know this notation:

{{block type="cms/block" block_id="your_block_id"}}

Background: I try to build a frame with the parent block that can use one ore more children.

Tricky: The parent and the children have multiple parameters to manage the template.

1 Answer 1

1

No, the template directives in the CMS are not as flexible as the layout XML, the {{block}} creates a block and renders it immediately, which makes it impossible to create a hierarchy of multiple blocks with it. Also, with the parameters you can set data, but not call methods, so inserting existing blocks as child block is also not possible.

So what can you do instead?

Create a custom block type which creates its own children. If you need to different children in different static blocks, you can control it by parameters.

Example

Blocks

class Stack_Cms_Block_Child extends Mage_Core_Block_Template
{
}

class Stack_Cms_Block_Wrapper extends Mage_Core_Block_Template
{
    protected function _construct()
    {
        if ($this->getData('make_child')) {
            $child = $this->getLayout()->createBlock('stack_cms/child');
            $child->setData('something', $this->getData('child_something');
            $this->append($child);
        }
    }
}

CMS

{{block type="stack_cms/wrapper" make_child="true" child_something="42"}}
2
  • 1
    Why can't you use A CMS page's Layout Update XML?
    – tread
    Commented Oct 24, 2016 at 14:10
  • 2
    The question was about CMS blocks which can be included anywhere, not just on CMS pages. But even on CMS pages, there is no way to define child blocks in the XML and then display them within the content. Commented Oct 24, 2016 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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