0

I'm probably thinking about this the wrong way, but I have two blocks and one is the child of the other. In the parent's template file, I'm calling getChildHtml() and rendering the child block that way.

However, before rendering the child block, I'd like to be able to check first if a certain condition is true for the child block. Is there a way for it to return false/true, and for the parent block to check that?

(Answer would preferably be particular to the above scenario rather than suggesting a completely different approach, though I'm always happy to hear thoughts)

1 Answer 1

2

you can get access to the child block instance using $child = $this->getChildBlock('child_name_here');
So you can add this method into the child block class:

public function isItTrue($paramsIfNeeded)
{
    return true; //or false. or whatever.
}

Now you can check if ($child->getIsItTrue($oaramsIfNeeded)) {....}

4
  • That's awesome -- I feel like my understanding of Magento just increased exponentionally haha. Thank you!
    – brackfost
    Dec 18, 2018 at 13:43
  • I actually couldn't get getChild() to work in the template file, but getChildBlock() worked.
    – brackfost
    Dec 18, 2018 at 14:21
  • @BrockfastCowboy. You might be right
    – Marius
    Dec 18, 2018 at 14:26
  • 1
    Might be? : ) (Thanks again)
    – brackfost
    Dec 18, 2018 at 14:51

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.