-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getChildHtml, $useCache and child containers #4919
Conversation
I have now signed the CLA. |
For anyone else who is currently suffering with this issue, I have the following temporary solution so that you can keep developing. In the block that you're calling getChildHtml from, add the following code:
|
Closed as PR does not have changed files |
@bentideswell currently struggling with this bug as well. I'll try to free up some time to fix this issue. |
@tdgroot the fix posted above should resolve the issue. I've restructured my code not to need it but before I did this, the above worked well. |
@bentideswell I've created a pull request: #14029 |
[TSG-CSL3] For 2.3 (pr35)
When calling the following method, the $useCache parameter is not applied to child containers, meaning that any blocks that are part of a child container are always taken from the cache, regardless of the initial value of $useCache.
\Magento\Framework\View\Element\AbstractBlock::getChildHtml($alias = '', $useCache = true)
This occurs because the \Magento\Framework\View\Layout::renderNonCachedElement method does not take $useCache as a parameter. This method then calls _renderContainer, which also doesn't take $useCache as a parameter. When _renderContainer then calls renderElement, although renderElement can accept the $useCache parameter, the value has been lost and the $useCache value is not passed to this method. As $useCache has a default value of true, blocks that are child to the container (that is child to the original block) always come from the cache, even if the developer specifically stated not to use the cache in the first call to getChildHtml.
To reproduce this, setup a layout that has the following:
You will need to change Vendor_Module for the correct path to the template files in the above XML.
In block1.phtml, add the following code:
In block2.phtml, add the following code:
For the first round of calling getChildHtml, the second parameter is skipped so the default value of true is used. The expected output is that the random number will be the same each time as after the first call, the result will be loaded from the cache.
On the second round of calling getChildHtml, the second parameter is set as false so the expected result would be that the output would be different each time. This is not what happens though and the same value is returned each time.
If you try this again with my changes, when $useCache is set to false, the output is different each time, meaning the cache is skipped for all child blocks.
This is my first pull request but hopefully it is a useful one. It's a sticking point for a project I am working on so I am happy get this merged as quick as possible!