I am doing some M2 fundamentals online training, the system is telling me that one of these answers is incorrect...

Question 1: Which of these directives will assign a template to a block in a layout xml file

Answer 1 : <block template="test.phtml">

Question 2 : Which block methods are available in a template?

Answer 2 : Only public methods.

I have tested both of these and they both seem to be right. Is it a trick question ?

Can anyone shed some light before I tear my hair out?


Question 1 :

There are Three methods to set template block in layout.xml


<referenceBlock name="block.name" template="Vendor_Module::file-name.phtml" />


<referenceBlock name="block.name">
        <argument name="template" xsi:type="string">Vendor_Module::file-name.phtml</argument>


<referenceBlock name="block.name">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_Module::file-name.phtml</argument>

Question 2 :

Yes, of course in template you have access juste for the public methods commings from block, exemple $block->getAnswer() this method public function getAnswer()is declared and you get it from the block associate to this template and it is well public, if it was private you can't access it because the private function is only visible in it's own class.

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.