1

Magento 1 Way :-

https://inchoo.net/magento/injecting-variables-into-a-magento-cms-static-block/

I need the same in Magento 2, how can I achieve? it's urgent.

Here is my afford:-

<?php

namespace Test\Clientform\Controller\Show;

use Magento\Framework\App\ObjectManager;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_filterProvider;
    protected $_storeManager;
    protected $_blockFactory;
    protected $_filterFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Cms\Model\BlockFactory $blockFactory,
        \Magento\Framework\Filter\Template $filterFactory,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->_filterProvider = $filterProvider;
        $this->_storeManager = $storeManager;
        $this->_blockFactory = $blockFactory;
        $this->_filterFactory = $filterFactory;
    }

    public function execute()
    {


        $blockId = 'client_intake_form';
        $html = '';
        if ($blockId) {
            $storeId = $this->_storeManager->getStore()->getId();
            /** @var \Magento\Cms\Model\Block $block */
            $block = $this->_blockFactory->create();
            $block->setStoreId($storeId)->load($blockId);

            $array['full_name'] = 'test';

             $this->_filterFactory->setVariables($array);
             // exit;
             //exit;
                $html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
        }
        echo   $html;

        // $resultPage = $this->_resultPageFactory->create();
        // return $resultPage;
    }



}

?>

When I'm hitting controller:-

http://127.0.0.1/Magento2/test/show/index

still not able to set value.

you can check here:-

http://nimb.ws/TEZVRp

To set value I've used \Magento\Framework\Filter\Template $filterFactory,

& calling setVariables() method.

When I'm setting values directly in the core class:-

\Magento\Framework\Filter\Template $filterFactory

e.g-

public function setVariables(array $variables)
    {
        $variables['full_name'] = 'test'; // added this line staticaly
        foreach ($variables as $name => $value) {
            $this->templateVars[$name] = $value;
        }
        return $this;
    }

Then value gets set, you can check here:-

http://nimb.ws/ytvcRV

so please let me know why I'm not able to set value from the controller.

2 Answers 2

4

I have created a sample code and its working fine for me:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$content = $objectManager->create('\Magento\Cms\Model\BlockFactory')->create();
$content->load('test_block', 'identifier'); // Load static block
$array['test'] = 'Test Block'; //Set variables
$filter = $objectManager->create('\Magento\Email\Model\Template\Filter');
$filter->setVariables($array);
$html = $filter->filter($content->getContent());
return $html;

Stati block code for test_block:

<p>{{var test}}</p>

You need to manage to add this your function.

1
  • You welcome. Happy Coding! Jul 31, 2018 at 13:21
3

With the help of @Sukumar finally, I reach to the solution.

Here it is:-

<?php

namespace Test\Clientform\Controller\Show;

use Magento\Framework\App\ObjectManager;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_filterProvider;
    protected $_storeManager;
    protected $_blockFactory;
    protected $_filterTemplate;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Cms\Model\BlockFactory $blockFactory,
        \Magento\Framework\Filter\Template $filterTemplate,
       // \Magento\Email\Model\Template\Filter $filterTemplate, // OR you can also use it
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->_filterProvider = $filterProvider;
        $this->_storeManager = $storeManager;
        $this->_blockFactory = $blockFactory;
        $this->_filterTemplate = $filterTemplate;
    }

    public function execute()
    {

        $blockId = 'client_intake_form';
        $html = '';
        if ($blockId) {
            $storeId = $this->_storeManager->getStore()->getId();
            /** @var \Magento\Cms\Model\Block $block */
            $block = $this->_blockFactory->create();
            $block->setStoreId($storeId)->load($blockId);

            $array['full_name'] = 'test';

             $this->_filterTemplate->setVariables($array);

              $html = $this->_filterTemplate->filter($block->getContent());

        }
        echo   $html;


    }


}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.