Magento: When to pass variables to a block and when not to?

L3monade

New Member
I have recently found the power of setting variables to a block in the _toHtml() method, using the assign method. My question is, when is it good to do this and when is it not? I am creating a new module and it seems to me it is much nice to just assign all the variables to the block and just reference those variables in the view file rather then setting up something like this\[code\]<?php $var1 = $this->getVar1(); $var2 = $this->getVar2();?><div id="<?php echo $var1 ?>"><?php echo $var2 ?></div>\[/code\]Isn't better to just set those in the block class and just call the variables in the phtml file? Then your template file would just look like\[code\]<div id="<?php echo $var1 ?>"><?php echo $var2 ?></div>\[/code\]This would remove more php code being in the template, which I think is good?The only thing I can think of is, it will be harder to know what variables are set when other developers are working on the template file. If I put a comment at the top that all variables are mostly set in x block class that would help or if they are debugging they will see all the variables set but I guess it could still be confusing, and I am assuming this is why Magento has only did it sparingly.But I am looking for anyone else's opinion on this for best practices.
 
Back
Top