I want to create a new directive to allow dynamic content to be added to CMS pages in Magento 2.1

I've found this article for Magento 1.7:

Add new custom CMS template directive {{cdnmedia}}

Can anyone tell me how the procedure varies from this for 2.1?

1 Answer 1


Well I had the same question. It looks like you have to override the Magento\Framework\Filter\Template class, and if you need the custom directive to work in email templates than you've got to go a bit further down the rabbit hole.

How to create a custom Filter directive (e.g. for emails)?

That does not seem like fun to me. There may be a cleaner way to do it, but I haven't been able to think of a way.

I have a an answer that's not as satisfying as you are hoping for, but it seems like a simple work around.

You can add a custom block class like so:


namespace Vendor\Module\Block;

use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Template\Context;

class MySweetBlock extends AbstractBlock
    * Constructor
    * @param Context $context
    * @param array $data
    public function __construct(Context $context, array $data = [])
        parent::__construct($context, $data);

    public function _toHtml(){
        // Pull out custom parameters
        $url = $this->getData('url');
        $width = $this->getData('width');
        $height = $this->getData('height');
        // Do the things
        return 'winning';

Then you can call you block like so:

{{block class="\\Vendor\\Module\\Block\\MySweetBlock" 
    height="100" }}

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.