Magento 2 CMS Pages – How to Add a Template Block via Layout Update

cms-pagesmagento2

I'm trying to include a new template block in a CMS page in the admin via layout updates.
Here is the xml:

<block class="Magento\Catalog\Block\Product\View\Description" name="homepage.carousel" template="homepage/carousel.phtml" before="-" />

The carousel template has been added in [theme-folder]/Magento_Theme/templates/homepage/carousel.phtml. Then I run:

  1. bin/magento cache:flush
  2. bin/magento setup:static-content:deploy

No luck, so then I moved the template to [theme-folder]/Magento_Cms/templates/homepage/carousel.phtml. I re-run the above commands but still no luck. Then I changed the CMS page "New Theme" and set it to my custom theme and re-run the above commands and still no luck.
Any suggestions?

Best Answer

The error was that I forgot to wrap the XML inside a <referenceContainer name="content">. So the XML should look like this:

<referenceContainer name="content">
    <block class="Magento\Catalog\Block\Product\View\Description" name="homepage.carousel" template="Magento_Theme::homepage/carousel.phtml" before="-" />
</referenceContainer>

And the other thing is that you should add the magento module inside the template src E.g:

<block class="Magento\Catalog\Block\Product\View\Description" name="homepage.carousel" template="Magento_Theme::homepage/carousel.phtml" before="-" />   

Kudos for this last part to Ankit Shah

Related Topic