I have used below code in magento1 to load the static block with custom variables.
Here is the code of my m1 Block file.
 {
 $customer = Mage::getSingleton('customer/session')->getCustomer();
 $block = Mage::getModel('cms/block')
 ->setStoreId(Mage::app()->getStore()->getId())
 ->load('static_block_id');
 if($block->getIsActive()) {
 $array = array();
 $array['custom_value1'] = $formattedPrice = Mage::helper('core')->currency($customer->getNoofOrders(), true, false);
 $array['custom_value2'] = $formattedPrice = Mage::helper('core')->currency($customer->getSavingsToDate(), true, false);
 $array['registration_date'] = date('F Y', strtotime($customer->getRegistrationDate()));
 $filter = Mage::getModel('cms/template_filter');
 $filter->setVariables($array);
 // return the filtered block content.
 return $filter->filter($block->getContent());
}
Here getNoofOrders and getSavingsToDate are the custom customer attributes.
How can i use the same code in magento 2 format. If so please anybody help me with this how to set the custom variables and that is used in static blocks. Thanks
 asked Nov 2, 2018 at 7:12
 
 
 
 Jafar Pinjar 
 
 1,9117 gold badges71 silver badges147 bronze badges
 
 - 
 Please check my answer.Rohan Hapani– Rohan Hapani2018年11月02日 08:49:33 +00:00Commented Nov 2, 2018 at 8:49
- 
 Is it working ?Rohan Hapani– Rohan Hapani2018年11月02日 09:26:02 +00:00Commented Nov 2, 2018 at 9:26
1 Answer 1
Try to use this below code :
protected $_customerSession;
protected $_blockFactory;
protected $_priceFormat;
protected $_filterProvider;
protected $storeManager;
public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Customer\Model\Session $customerSession,
 \Magento\Cms\Model\BlockFactory $blockFactory,
 \Magento\Framework\Pricing\Helper\Data $priceFormat,
 \Magento\Cms\Model\Template\FilterProvider $filterProvider,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 array $data = []
) {
 parent::__construct($context, $data);
 $this->_customerSession = $customerSession;
 $this->_blockFactory = $blockFactory;
 $this->_priceFormat = $priceFormat;
 $this->_filterProvider = $filterProvider;
 $this->storeManager = $storeManager;
}
public function yourFunction()
{
 $customer = $this->_customerSession->getCustomer();
 $block = $this->_blockFactory->create()->setStoreId($this->storeManager->getStore()->getId())->load('static_block_id');
 if($block->getIsActive()) {
 $array = [];
 $array['custom_value1'] = $formattedPrice = $this->_priceFormat->currency($customer->getNoofOrders(), true, false);
 $array['custom_value2'] = $formattedPrice = $this->_priceFormat->currency($customer->getSavingsToDate(), true, false);
 $array['registration_date'] = date('F Y', strtotime($customer->getRegistrationDate()));
 $filter = $this->filterProvider->getBlockFilter()->setStoreId($this->storeManager->getStore()->getId())->setVariables($array)->filter($block->getContent());
 return $filter;
 }
}
Hope, It may be helpful for you.
 answered Nov 2, 2018 at 8:49
 
 
 
 Rohan Hapani 
 
 17.6k9 gold badges57 silver badges99 bronze badges
 
 - 
 Happy to help !! Happy coding :)Rohan Hapani– Rohan Hapani2018年11月02日 09:33:07 +00:00Commented Nov 2, 2018 at 9:33
default