How to use the action methods in Magento xml layout files to use

<strong> Examples

mylayoutfile.xml

<layout> <default> <reference name="header"> <block type="mynamespace_mymodule/view" name="mynamespace_mymodule" output="toHtml" template="mymodule/html/view.phtml"> <action method="setTest"><param1>myparam1</param1><param2>myparam2</param2></action> </block> </reference> </default> </layout> 

Application / code / local / Mynamespace / MyModule / block / view.php

  class Mynamespace_Mymodule_Block_View extends Mage_Core_Block_Template{ public $test = "before any params"; public function setTest($passedparam1,$passedparam2){ $this->test = $passedparam1 ." ". $passedparam2; } } 

application / design /.../.../ MyModule / html / view.phtml

 <?php echo "<pre>"; print_r($this->test); //myparam1 myparam2 echo"</pre>"; die(); 

Explanation

mylayoutfile compiled in upgrade through your config.xml modules

the block class prefix mynamespace_module is also defined inside your config.xml modules

mynamespace_module / view is set as the block type and an instance is created, and the output file view.phtml is set

an action is performed that calls the parent method node block setTest, passing two parameters with the value myparam1 and myparam2.

inside the setTest function, the class parameter "test" is set to "myparam1 myparam2"

the file /app/design/.../.../mymodule/html/view.phtml of the template file is loaded, and we repeat the value $ this-> test ($ this refers to the previously created block class Mynamespace_mymodule_Block_View)

QUESTIONS

  • What are some use cases where this can be used?
  • Can you pass anything but a string? (Object, Array)?
  • Do automatic setters and getter methods execute inside the layout file?
  • Is it possible to use logic (if then, foreach, else, etc.)?
  • Are there any scenarios in which this method should not be used?
  • Is there anything else I can lose associated with creating a block from a layout file?
  • Is there anything else I can skip related to the actions from the layout file?
+7
source share
1 answer
  • Can be used for multipurpose template. Take a look at using setTitle in catalog.xml.
  • Everything can be transferred. Arrays can be defined in the XML layout:

     <action method="setFoo"> <arbitrary> <key1>val1</key1> <key2>val1</key2> </arbitrary> </action> 

    In addition, the argument nodes can execute a helper method whose return value will be passed as a value:

     <action method="setFoo"> <arbitrary helper="foo/bar" /> </action> 

    This will create an instance of the helper and run the method: Mage::helper('foo')->bar() . So the return value can be whatever you want. Additionally, args can be passed to helper in child nodes!

  • Varien_Object extension Varien_Object , so yes, although setters are the only reasonable ones.
  • There is no direct logic for this. The closest is the action ifconfig attribute, which will call Mage::getStoreConfigFlag() with the argument provided and will process the action if the configuration value is true.
  • "[c] will not be used" - no. "It does not matter" - yes.
  • There are many nuances (too many to enter here), but you already have a lot of things. Alan Storm gets all the nuances in his book, No Frills Magento Layout ; nothing beats the limitations themselves.
+14
source

All Articles