Magento2: XML syntax to change block's template

Created on 10 Feb 2016  路  21Comments  路  Source: magento/magento2

Summary

Documentation claims that it is possible to change a block's template by the following layout update:

<referenceBlock name="navigation.sections">
  <arguments>
    <argument name="template" xsi:type="string">Magento_Theme::html/nav_sections.phtml</argument>
  </arguments>
</referenceBlock>

But It doesn't work.

Using action node, instead of arguments node, works fine:

<referenceBlock name="navigation.sections">
  <action method="setTemplate">
    <argument name="template" xsi:type="string">Magento_Theme::html/nav_sections.phtml</argument>
  </action>
</referenceBlock>

Source:
http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#set_template

Preconditions (*)

  1. Magento 2.3-develop
  2. PHP 7.2

Steps to reproduce (*)

Try to change a block's template by the following layout update
e.g.:
3356

Expected result (*)

Block's template is changed without any errors

Actual result (*)

Got exception
3356ex

Frontend FrameworCode Theme Clear Description Confirmed Format is valid Ready for Work done Reproduced on 2.2.x Reproduced on 2.3.x Dev.Experience bug report

Most helpful comment

@magento-engcom-team the PR you've referencing was also closed, as it required some changes in the pull request code.
So now both this issue and PR are closed but the issue still exists and documentation is still wrong.
In my opinion this issue should stay open until it's fixed.

If you consider this a feature request, please at last fix the documentation.

All 21 comments

Hi @cescopag

You provided correct way with action method
Looks like mistake in public docs

Hi @cescopag!
Thank you for reporting this. Looks like there's a bug in magento code, that is why the code sample provided in docs doesn't work. But you found the correct temporary solution.
It must be temporarily as is deprecated. I'll add a note in docs, and updated them when the bug is fixed.

Is this now fixed?

In 2.1.0 I still have to use 'action' and the online documentation is still wrong.

@digitalpianism No, sorry - I believe that's what we all tried first. At least I did, that's how I ended up here.

@LiamFielding my bad. I guess we gotta wait ^^

The reason why it doesn't work is because the method \Magento\Framework\View\Layout\Generator\Block::generateBlock:

When we want to overwrite an existing block, this one was at first initialized via a layout XML with an attribute template.

Into the generateBlockmethod, arguments are passed to the block, then setTemplate is called and then in the calling method process the action is called.

So the priority is the following:

  • actions
  • setTemplate via attribute template
  • arguments

When a block was initialized with its attribute template, arguments will have no effect. That's why you need to use actions

protected function generateBlock(
        Layout\ScheduledStructure $scheduledStructure,
        Layout\Data\Structure $structure,
        $elementName
    ) {
        list(, $data) = $scheduledStructure->getElement($elementName);
        $attributes = $data['attributes'];

        if (!empty($attributes['group'])) {
            $structure->addToParentGroup($elementName, $attributes['group']);
        }
        if (!empty($attributes['display'])) {
            $structure->setAttribute($elementName, 'display', $attributes['display']);
        }

        // create block
        $className = $attributes['class'];
        $block = $this->createBlock($className, $elementName, [
            'data' => $this->evaluateArguments($data['arguments'])
        ]);
        if (!empty($attributes['template'])) {
            $block->setTemplate($attributes['template']);
        }
        if (!empty($attributes['ttl'])) {
            $ttl = (int)$attributes['ttl'];
            $block->setTtl($ttl);
        }
        return $block;
    }

Issue in progress @nsergo & @sambolek

Internal ticket to track issue progress: MAGETWO-66248

Closing the issue as the feature request. Please, see more details here https://github.com/magento/magento2/pull/8913#discussion_r106549969

@magento-engcom-team the PR you've referencing was also closed, as it required some changes in the pull request code.
So now both this issue and PR are closed but the issue still exists and documentation is still wrong.
In my opinion this issue should stay open until it's fixed.

If you consider this a feature request, please at last fix the documentation.

This issue still exists. We should reopen it.

@cescopag, thank you for your report.
We've acknowledged the issue and added to our backlog.

This is still not solved.

Solution:

if ($block->hasData('template')) {
    // set template agrument
    $block->setTemplate($block->getData('template'));
} else {
    if (!empty($attributes['template'])) {
        $block->setTemplate($attributes['template']);
    }
}
if (!empty($attributes['ttl'])) {
    $ttl = (int)$attributes['ttl'];
    $block->setTtl($ttl);
}
return $block;

On Magento 2.3.2 seems to not be an issue any more

This issue still exist.



Yoma_Dipen::addtocart.phtml

I tried this code in my customer module's catalog_product_view.xml

it's not working.

Doesn't work in 2.3.3-p1

Does not work in 2.3.5-p2

Hi @ihor-sviziev. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:


    1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

    1. Verify that the issue is reproducible on 2.4-develop branch
      Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
      - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
      - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
    1. If the issue is not relevant or is not reproducible any more, feel free to close it.

FYI This issue was resolved by adjusting the docs and highlighting the applied priorities in https://github.com/magento/devdocs/pull/7977

Was this page helpful?
0 / 5 - 0 ratings