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)

Purushotam Sharma
1,6772 gold badges28 silver badges62 bronze badges
asked Dec 17, 2018 at 22:35

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)) {....}

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.