2

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?

asked Jan 12, 2017 at 1:06

1 Answer 1

0

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:

<?php
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" 
 url="https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" 
 width="100" 
 height="100" }}
answered Apr 29, 2018 at 23:03

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.