3

I've been learning the ropes in theming in magento 2, I've started to get the hang of it with my own theme using Snowdog as my parent and styling in sass.

My question is, I came to remove the welcome message yesterday and couldn't find any xml related to this. So I went in search and found it in the header.phtml file.

 <?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var \Magento\Theme\Block\Html\Header $block
 */
$welcomeMessage = $block->getWelcome();
?>
<?php switch ($block->getShowPart()):
    case 'welcome': ?>
        <li class="greet welcome" data-bind="scope: 'customer'">
            <!-- ko if: customer().fullname  -->
            <span data-bind="text: new String('<?php echo $block->escapeHtml(__('Welcome, %1!', '%1'));?>').replace('%1', customer().firstname)">
            </span>
            <!-- /ko -->
            <!-- ko ifnot: customer().fullname  -->
            <span data-bind="html:'<?=$block->escapeHtml($welcomeMessage) ?>'"></span>
            <!-- /ko -->
        </li>
        <script type="text/x-magento-init">
        {
            "*": {
                "Magento_Ui/js/core/app": {
                    "components": {
                        "customer": {
                            "component": "Magento_Customer/js/view/customer"
                        }
                    }
                }
            }
        }
        </script>
    <?php break; ?>

    <?php case 'other': ?>
        <?php echo $block->getChildHtml(); ?>
    <?php break; ?>

<?php endswitch; ?>

But then i realised there isn't any other php or html inside of this file, the whole of the header is built with xml.

Why is it built with xml? Are there any benefits to this? Should I continue to use xml, or should i look at doing it with php - html?

Thanks.

  • This is not xml this is knockout js used here. What do you want to do? – Rakesh Jesadiya Aug 31 '16 at 16:40
  • Why is the entire header built with xml? Are there any benefits to this? Should I continue to use xml, or should i look at doing it with php - html? – andy jones Sep 1 '16 at 7:04
  • No, this is not xml code this is used knockout js – Rakesh Jesadiya Sep 1 '16 at 7:06
  • <!-- ko if: customer().fullname --> this is not xml this is knockout js. using knockout js they get full name – Rakesh Jesadiya Sep 1 '16 at 7:07
  • plz tell me what do you want to do? – Rakesh Jesadiya Sep 1 '16 at 7:16
5

With regards to the XML, that block is called header and is set in

\vendor\magento\module-theme\view\frontend\layout\default.xml

You can remove it via a theme override at

\app\design\frontend\YOURNAME\YOURTHEME\Magento_Theme\layout\default.xml

with the following xml:

<referenceBlock name="header" remove="true"/>

This seems to be the only thing that block is used for, though I do have a slight wariness that it's called "header" rather than "welcome" or something that suggests such a simple purpose. That said, a code search does appear to confirm that this is the only use.

As for "why is it built using xml" - I'd suggest a read of the official documentation - http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/layout-overview.html

Dividing the page up into lots of individual XML blocks is a tidier solution than large PHP includes, even if it is fiddler to learn than, for example, the messier, more forgiving, approach of Wordpress.

IMPORTANT UPDATE: I've noticed an oddity in that removing this and the breadcrumbs seems to stop the meta tags working. See: Removing welcome message and breadcrumbs stops the title tag appearing on category page This may mean that overriding the template file is the better approach (or just use css to hide it), depending on the cause.

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.