1

I can't find a clear guide as to how to link a custom XML file to a custom PHTML template file in Magento 2.1.

I created a test.phtml in

app/code/Custom/Module/view/frontend/templates/ 

and also test_index_index.xml in

app/code/Custom/Module/view/frontend/layout/

The purpose is to create a new page that will be linked to from the top.links header links menu section.

  1. My first questions is, what PATH will be correct to link to this page from the menu?

In my

app/design/frontend/Custom/theme/Magento_Customer/layout/default.xml 

I added the link like this:

<block class="Magento\Framework\View\Element\Html\Link" name="test" after="register-link">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">Test Page</argument>
                <argument name="path" xsi:type="string" translate="true">**What is the correct path to use here?**</argument>
            </arguments>
        </block>

I couldn't get it to work. This is the best way I'd like to link to the new page.

Then, in order to link to test.phtml from the menu link I created a new cms page from

Magento Admin >> Content >> Pages >> Add New Page.

Then, in Content > WYSIWYG editor in code mode I called the template file like this:

{{block class="Magento\Framework\View\Element\Template" name="test" 
template="Custom_Module::test.phtml"}}

And used the cms page url-key as path in

  <argument name="path" xsi:type="string" translate="true">test</argument> 

This successfully links to the new page. However, now with the page being a CMS page using 1column.xml I'd like to make modifications to it's appearance via XML layout. For a start, I'd like to remove the breadcrumbs from the page. However, the XML file I created for the page does not have effect.

My test_index_index.xml:

<?xml version="1.0" encoding="utf-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <head>
<title>My First New Mage Custom Page</title>
 </head>
 <body>
 <referenceContainer name="content">
  <block class="Custom\Module\View\Element\Template" name="test" template="Custom_Module::test.phtml">
  </block>
  </referenceContainer>
  <referenceBlock name="breadcrumbs" remove="true"/> <!--this will work if success-->
  </body>
  </page>
  1. My second question is how to link the above mentioned XML layout to my PHTML template file?

I tried naming the file cms_test.xml , cms_test_index.xml --> no effect. The XML file does not link to the phtml file. How can I achieve that?

  • So, as I understand, in order to "attach" a custom xml layout file to a phtml template file we need to create a controller for it in Block, thus we need to create a new custom module for this task. Or, create new custom reusable layout template and register it in Magento system, so that we can choose it as layout for our new CMS page. Or, we can still use our custom phtml template file by calling it from the WYSIWYG editor of our new cms page. As for layout we can use XML Update. Does anyone know in which XML file I can see those updates in the file system? – Iveta Allogenes Sep 29 '17 at 13:20

Your Answer

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

Browse other questions tagged or ask your own question.