attiquer
9/2/2016 - 12:33 PM

CMS, Phtml templates initialization

CMS, Phtml templates initialization

=> Let's say you have slider.phtml file under <vendor>/<theme>/Magento_CMS/templates and you want to show slider.phtml file that calls home_slider static block:
    <?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('home_slider')->toHtml(); ?>

=> If it's a custom module let's say Firstfound_Helloworld and doesn't involve static block:
    echo $this->getLayout()->createBlock('Firstfound\HelloWorld\Block\HelloWorld')->setTemplate('Firstfound_Helloworld::helloworld.phtml')->toHtml();

=> Now call this file in <vendor>/<theme>/Magento_CMS/layout/cms_index_index.xml file
        <referenceContainer name="main">
            <block class="Magento\Framework\View\Element\Template" name="homepage_slider" template="Magento_Cms::main-slider.phtml" />
        </referenceContainer>
        
=> To show a phtml block in cms page or static block
{{block class="Firstfound\HelloWorld\Block\HelloWorld" name="your_block_name" template="Firstfound_Helloworld::helloworld.phtml"}}

=> Show CMS block in CMS page
{{block class="Magento\\Cms\\Block\\Block" block_id="block-id"}}