4

In the following file, I found this code:

<?php echo $countries->toHtmlOptions($data->getCountryId()) ?>

app/design/frontend/base/default/template/customer/form/address.phtml

$countries has not been instantiated in this phtml file and it kinds of looks like a global object. Typically, we just use $this-> to access methods and some other info in phtml files. I was also trying to find the related block for this global object($countries) and could not find it so easily. They also have few more variables in this template file.

How did they do it? May be, I can do something similar when it is necessary.

1 Answer 1

2

In Magento 1.9 I don't find any usage of this template anymore, but it's possible to create variables for templates with assign():

$block->assign('countries', $countries);

or in layout XML

<action method="assign"><var>countries</var><value>A,B,C</value></action>

However this method is seldomly used and I would not consider it good practice. The preferred method is to add and retrieve data to the block with magic setters and getters:

$block->setCountries($countries);

and in the template:

$this->getCountries()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.