In Magento 2 we can add widgets to CMS pages and CMS blocks with a directive like this (assuming we created this custom widget with a module before):
{{widget type="Vendor\Module\Block\Widget\CustomWidget" some_addtribute="some-value"}}
When we add the same widget code in product or category description it is not rendered. So the output is the text string from above.
How can we extend Magento 2 in a way that it will render widgets in product or category descriptions as well?
Best Answer
Instead of instantiating the object manager and filterprovider etc. in your template, consider this:
In your block class (
Vendor\Module\Block\Widget\CustomWidget
) include the dependencies in your constructor:Now in your block class add a method to filter the input:
I assume that
getCurrentCategory()
is a method you've already created.Now in your template, you can simply do:
So now your template is neat and tidy and all dependencies and business logic are separated to you block class, as it should.