Magento 2 – Insert phtml in Custom CMS Page

cmsmagento2modulephtml

i had created a new module on Magento 2.
It have:

setup -> UpgradeData.php :

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /**@var \Magento\Cms\Model\Page $page */
        if (version_compare($context->getVersion(), '1.1') < 0) {
            $page = $this->_pageFactory->create();
            $page->setTitle('Voucher CMS')
                ->setIdentifier('voucher-cms')
                ->setIsActive(true)
                ->setPageLayout('1column')
                ->setStores(array(0))
                ->setContent('{{block class="Cincuenta\\Voucher\\Block\\Product\\View\\Extra" block_id="voucher-cms" template="Cincuenta_Voucher::voucher/voucher.phtml"}}
')
                ->save();
        }

        $setup->endSetup();
    }

It creates well my new CMS with the content.

I had a phtml on view -> frontend -> templates -> voucher -> voucher.phtml:

<h1> Test Voucher</h1>

and Block – Product -View – Extra.php:

<?php

namespace Cincuenta\Voucher\Block\Product\View;


class Extra extends \Magento\Catalog\Block\Product\View
{

}

Code on my CMS:

{{block class="Cincuenta\\Voucher\\Block\\Product\\View\\Extra" block_id="voucher-cms" template="Cincuenta_Voucher::voucher/voucher.phtml"}}

But when i try to see my H1 in my CMS, it don't give me nothing, but a few times gives me an error that i was calling a wrong block.

Best Answer

Your code looks fine. However, you said that your .phtml file is

view -> templates -> voucher -> voucher.phtml

Ideally, it should be here:

view -> frontend -> templates -> voucher -> voucher.phtml

Related Topic