Magento – Magento 2.0 Call cms page in phtml file

cmscms-blockcms-pagesmagento-2.0page

I have created one CMS page brand and I want to call brand CMS page in phtml file. So how can I call the CMS page?

I got following code but it's not working.

$cmsPage = Mage::getModel('cms/page')->load('Brand List','brand_list');
echo $cmsPage->getContent();

Best Answer

Your phtml is rendered by a block class.
You need to add this in your block class.

protected $pageFactory;
protected $brandPage;
public function __construct(
    ....
    \Magento\Cms\Model\PageFactory $pageFactory,
    ....
){
    ....
    $this->pageFactory = $pageFactory;
    ....
}

public function getBrandsPageContent()
{
    if (is_null($this->brandPage)) {
        $this->brandPage = $this->pageFactory->create();
        $this->brandPage->load('brand_list', 'identifier');
    }
    return $this->brandPage->getContent();
}

then in your phtml you just call echo $this->getBrandsPageContent()

Related Topic