The post will guide how to use a template processor to filter variables, widgets in tag {{…}} to HTML.
Use filter display content of a block
<?php
require dirname(__FILE__) . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
class Outslide extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend');
// $this->_state->setAreaCode('adminhtml');
// $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
// $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
$block = $this->_objectManager->create('\Magento\Cms\Model\Block');
$filterProvider = $this->_objectManager->create('Magento\Cms\Model\Template\FilterProvider');
$storeManager = $this->_objectManager->create('Magento\Store\Model\StoreManagerInterface');
$blockId = 1; // Id of block you want get content
$storeId = $storeManager->getStore()->getId();
$block->setStoreId($storeId)->load($blockId);
$content = $block->getContent();
$html = $filterProvider->getBlockFilter()->setStoreId($storeId)->filter($content);
echo $html;
//the method must end with this line
return $this->_response;
}
}
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Outslide');
$bootstrap->run($app);
Use filter display content of a page
<?php
require dirname(__FILE__) . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
class Outslide extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend');
// $this->_state->setAreaCode('adminhtml');
// $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
// $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
$page = $this->_objectManager->create('\Magento\Cms\Model\Page');
$filterProvider = $this->_objectManager->create('Magento\Cms\Model\Template\FilterProvider');
$storeManager = $this->_objectManager->create('Magento\Store\Model\StoreManagerInterface');
$pageId = 1; // Id of Page you want get content
$storeId = $storeManager->getStore()->getId();
$page->setStoreId($storeId)->load($pageId);
$content = $page->getContent();
$html = $filterProvider->getBlockFilter()->setStoreId($storeId)->filter($content);
echo $html;
//the method must end with this line
return $this->_response;
}
}
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Outslide');
$bootstrap->run($app);
Use filter display description product with a programmatically
<?php
require dirname(__FILE__) . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
class Outslide extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend');
// $this->_state->setAreaCode('adminhtml');
// $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
// $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
$productRepository = $this->_objectManager->create('Magento\Catalog\Model\ProductRepository');
$filterProvider = $this->_objectManager->create('Magento\Cms\Model\Template\FilterProvider');
$storeManager = $this->_objectManager->create('Magento\Store\Model\StoreManagerInterface');
// YOU WANT TO LOAD BY ID?
$id = 1;
// YOU WANT TO LOAD BY SKU?
$sku = "YOUR SKU HERE";
if($id) {
$product = $productRepository->getById($id);
}
if($sku) {
$product = $productRepository->get($sku);
}
$descriptionAttributeCode = "description";
$storeId = $storeManager->getStore()->getId();
$description = $product->setStoreId($storeId)->getData($descriptionAttributeCode);
// echo $description;
$html = $filterProvider->getBlockFilter()->setStoreId($storeId)->filter($description);
echo $html;
echo 'Done!';
//the method must end with this line
return $this->_response;
}
}
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Outslide');
$bootstrap->run($app);