Magento 2 – Set Custom Template for Core Block

layoutmagento2moduleoverridestemplate

I'm trying to override the register.phtml in my module, but it seems there are some config problem that make Magento unable to use my custom template to override the one in core module. I've checked the code but can't find the problem. Anyone can help? Thanks in advance.
module.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Namespace_ModuleName" setup_version="0.0.2">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

customer_account_create.xml under Namespace\ModuleName\view\frontend\layout:

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="customer_form_register">
        <arguments>

            <argument name="template" xsi:type="string">Namespace_ModuleName::form/register.phtml</argument>

        </arguments>
    </referenceBlock>
</layout>

composer.json:

{
  "name": "namespace/module-modulename",
  "description": "modulename",
  "type": "magento2-module",
  "version": "0.0.2",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "require": {
    "php": "~5.5.0|~5.6.0",
    "magento/framework": "~0.42",
    "magento/magento-composer-installer": "*",
    "magento/module-customer": "*"
  },
  "extra": {
    "map": [
      [
        "*",
        "Namespace/ModuleName"
      ]
    ]
  }
}

Best Answer

Just reference necessary block and set template as attribute of referenceBlock node:

<?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="customer_form_register" template="VendorName_ModuleName::form/register.phtml"/>
    </body>
</page>

Alternative syntax:

<referenceBlock name="customer_form_register">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">VendorName_ModuleName::form/register.phtml</argument>
    </action>
</referenceBlock>

Also if you are using Enterprise Edition, make sure to put Magento_CustomerCustomAttributes to sequence in your module.xml because it also overrides this template and may override your changes.