<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?
ajameswolf
source share