template – How to Provide Properties to Views in Magento

layoutprogrammingtemplatezend-framework

In Zend Framework we provide properties to layouts which are then consumed in the view. An example of this would be:

$this->layout = new Zend_Layout();
$this->layout->username = 'John';
$this->layout->email = 'john@foofoobunnies.edu';

Which I would later echo in my view.phtml:

<?= $this->username; ?>
<?= $this->email; ?>

However I find myself in Magento writing block methods that return entire objects to be consumed by the view – for instance:

Block/User.php

public function getUser(){
    return Mage::getModel('mymodule/user')->load($this->getUserId());
}

And in my user.phtml:

$user = $this->getUser();
echo $user->getUsername();
echo $this->getEmail();

Is there any way to cut out this step of having to write block methods to return relevant data? I've been using this method for 2-3 years so I'm no stranger to it, but it just seems like the Zend Two-Step/Composite view layout paradigm is much faster to hack on.

Edit

It just occurred to me that it may be possible to utilize block properties in the same manner that I utilize the block methods, e.g. $this->username where $this may be an object of type Mymodule_User_Block_User; though I don't know if this is an understood or accepted practice in Magento, as I haven't seen this too often.

Best Answer

There's always assign() and the aforementioned getters and setters via Varien_Object::_call()

Related Topic