1

Can I include a template into the xml for designing a specific static block? This would enable a non technical user to create a static block in the admin, without having to add a into the html part of the admin/cms editor? So in the template there would just be the html markup for the part ? Is that possible?

<?xml version="1.0"?> 
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>         
<referenceContainer name="content">
        <block name="cms.block1" before="-" class="Magento\Cms\Block\Block" cacheable="true">
            <arguments>
                <argument name="block_id" xsi:type="string">cms_block_1</argument>
            </arguments>
        </block>

      <block name="cms.block2" after="cms.block1" class="Magento\Cms\Block\Block" cacheable="true">
            <arguments>
                <argument name="block_id" xsi:type="string">cms_block_2</argument>
            </arguments>
        </block>
    </referenceContainer>
    </body>
</page>

This is my xml which handles the static blocks on the homepage. Now I added the manually into the static blocks.

So how to call a .phtml file? And how would that file look like?

Can I just have:

<div class=""></div> in that file or 
do i need something like: <div class="">cms_block_1</div> ?
1

You'll create a template file in the concerned Module in your custom theme. Let's say you want to add a CMS block cms_block_1 to home page you'll create a template mytemplate.phtml under <theme>/Magento_CMS/templates directory with following:

<div class="my-class">
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('cms_block_1')->toHtml(); ?>
</div>

Now in <theme>/Magento_CMS/layout/cms_index_index.xml file add

<referenceContainer name="content">
<block  class="Magento\Framework\View\Element\Template" name="my.block1" template="Magento_Cms::mytemplate.phtml" before="-" />
</referenceContainer>
  • Tried that and its giving me this output in the frontend: code echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('cms_block_1')->toHtml(); – mr_typo3 Nov 9 '16 at 10:06
  • PHP tags were missing, see my edit above! – Devtype Nov 9 '16 at 10:16
  • I already added them myself. But still getting the output? Will try again and remove all cached files before (not only the pub/static). – mr_typo3 Nov 9 '16 at 10:20
  • Ok it works. Do I need to have a separate template for every cms-block or can I combine lets say block_1 block_2 into that one file? To get it working with the xml as in my question above? So It will have: code <div class="my-class"> <?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('cms_block_1')->toHtml(); ?> </div> and code <div class="my-other_class"> <?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('cms_block_2')->toHtml(); ?> </div> – mr_typo3 Nov 9 '16 at 10:31
  • Awesome, you can add as many cms blocks as you need and your code seems to be correct :) – Devtype Nov 9 '16 at 10:36

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.