In this post, I will show you how to customize message template in Magento 2.
FYI, this is the default Magento message block:

This template is defined under vendor/magento/module-theme/view/frontend/templates/messages.phtml
, and its name is message
.
To customize this message template, you can use referenceBlock
and set a new value for the template
property, like below:
<?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>
<referenceBlock name="messages" template="Vendor_Module::messages.phtml" />
</body>
</page>
You can also override the block class by overriding the class
property:
<?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>
<referenceBlock name="messages"
class="Vendor\Module\Block\CustomMessageBlock"
template="Vendor_Module::messages.phtml" />
</body>
</page>
That’s how you customize message template in Magento 2.
Have fun!