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:-
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:-
so please let me know why I'm not able to set value from the controller.