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:
/** * Example constructor. * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider * @param \Magento\Catalog\Helper\Output $catalogOutputHelper * @param \Magento\Framework\View\Element\Template $context * @param array $data */ public function __construct( \Magento\Cms\Model\Template\FilterProvider $filterProvider, \Magento\Catalog\Helper\Output $catalogOutputHelper, \Magento\Framework\View\Element\Template $context, array $data = [] ) { $this->filterProvider = $filterProvider; $this->catalogOutputHelper = $catalogOutputHelper; \Magento\Framework\View\Element\Template::__construct($context, $data); }
Now in your block class add a method to filter the input:
/** * @param string $description * @return string */ public function getFilteredContent(string $description) { return $this->filterProvider ->getBlockFilter() ->filter( $this->catalogOutputHelper->categoryAttribute( $this->getCurrentCategory(), $description, 'description' ) ); }
I assume that getCurrentCategory() is a method you've already created.
Now in your template, you can simply do:
echo $block->getFilteredDescription($_description);
So now your template is neat and tidy and all dependencies and business logic are separated to you block class, as it should.