3

I have a static cms page. I want to display that page content in my phtml file. Is there way to do this by using that page identifier?

I did something like that for cms blocks.

echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('my_block')->toHtml();
5

Add following code in your Block class, here Form class is an example

<?php
namespace Vendor\Module\Block;


class Form extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Cms\Model\Template\FilterProvider
     */
    protected $_filterProvider;

    /**
     * @var \Magento\Cms\Model\Page
     */
    protected $_page;

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * Page factory
     *
     * @var \Magento\Cms\Model\PageFactory
     */
    protected $_pageFactory;

    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Cms\Model\Page $page
     * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Cms\Model\PageFactory $pageFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Cms\Model\Page $page,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Cms\Model\PageFactory $pageFactory,
        array $data = []
    ) {
        parent::__construct($context, $data);

        $this->_filterProvider = $filterProvider;
        $this->_storeManager = $storeManager;
        $this->_pageFactory = $pageFactory;
    }

    /**
     * Retrieve Page instance
     *
     * @return \Magento\Cms\Model\Page
     */
    public function getPage($identifier)
    {
        if (!$this->hasData('page')) {
            /** @var \Magento\Cms\Model\Page $page */
            $page = $this->_pageFactory->create();
            $page->setStoreId($this->_storeManager->getStore()->getId())->load($identifier, 'identifier');
            $this->setData('page', $page);
        }
        return $this->getData('page');
    }

    public function getContent($identifier)
    {
        return $this->_filterProvider->getPageFilter()->filter($this->getPage($identifier)->getContent());
    }
}

Now you need to just following code for display cms page inside your corresponding phtml file

<?php echo $block->getContent('privacy-policy-cookie-restriction-mode');?>
  • How can you achieve this if the phtml is shared around pages, and to only content for the page you are viewing? – heady12 Jan 17 at 21:52
2

You have one block related to this template

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

public function getCmsPageContent($identifier)
{
    if ($identifier) {
        $content = $this->pageFactory->create();
        $content->load($identifier, 'identifier');
    }
    return $content->getContent();
}

You can use this block function in template using below code:

echo $block->getCmsPageContent('my_block');
2

Add following code to display cms static page content in any phtml file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$content = $objectManager->create('\Magento\Cms\Model\PageFactory')->create();
$content->load('page-identifier', 'identifier');
echo $content->getContent();

Note : if we use "\Magento\Cms\Model\PageFactory" so compulsory create it Like $objectManager->create('\Magento\Cms\Model\PageFactory')->create(); other wise use following code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$content = $objectManager->create('\Magento\Cms\Model\Page');
$content->load('page-identifier', 'identifier');
echo $content->getContent();
0

This is the correct code for display page content in phtml file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
           $content = $objectManager->create('\Magento\Cms\Model\PageFactory')->create();
           $content->load('page-identifier', 'identifier');
           echo $content->getContent();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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