I'm studying Magento and trying to wrap my head around the way this particular function is set up. In the Mage_Rating_Block_Entity_Detailed block, this line creates the $collection variable that can be used in the phtml template:

$this->assign('collection', $ratingCollection);

The assign function, found in the Mage_Core_Block_Template block, goes like this:

public function assign($key, $value=null)
        if (is_array($key)) {
            foreach ($key as $k=>$v) {
                $this->assign($k, $v);
        else {
            $this->_viewVars[$key] = $value;
        return $this;

This uses the _viewVars function, which I haven't found much on--but from what I can tell, it just takes the $key variable ('collection' in this case) and makes it into a proper variable ($collection) with the assigned value.

So why bother with all of this? Why not just replace the assign function with something like:

$collection = $ratingCollection;

Or, for that matter, why bother renaming $ratingCollection at all? There obviously must be some need for the fancy footwork here, but I don't see it.


So as you said the assign method deals with the $_viewVars variable of Mage_Core_Block_Template.

You missed the interested part though.

In the fetchView method you can find the following line:

extract ($this->_viewVars, EXTR_SKIP);

That means that using the following code:

$this->assign('collection', $ratingCollection);

Your .phtml template view file will have access to a local variable called $collection.

As you can see in the app/design/base/default/template/rating/detailed.phtml file, the $collection is not declared in the template directly as it comes from this assign method:

<?php if(!empty($collection) && $collection->getSize()): ?>
    <table class="ratings-table">
        <col width="1" />
        <col />
            <?php foreach ($collection as $_rating): ?>
                <?php if($_rating->getSummary()): ?>
                        <th><?php echo $this->__($this->escapeHtml($_rating->getRatingCode())) ?></th>
                            <div class="rating-box">
                                <div class="rating" style="width:<?php echo ceil($_rating->getSummary()) ?>%;"></div>
                <?php endif; ?>
            <?php endforeach; ?>
<?php endif; ?>

You can find more information here if you'd like to: http://magento-quickies.alanstorm.com/post/19574557297/using-assign-with-magento-blocks

| improve this answer | |
  • That makes sense, but why is that preferable over just declaring the $collection variable in the block php file? Obviously, you would have to declare it outside of a function, so would that make its scope too large and potentially conflict with other parts of the app? – sadq3377 May 2 '16 at 14:19
  • @sadq3377 I think it's a matter of less code to write. I'm not sure but as this block is only used once in the getReviewsSummaryHtml method, declaring a $collection variable in the block file will imply to create a function to retrieve that collection in the template file as well as adding an extra line in the template to call that method. To me that's what it looks like. – Raphael at Digital Pianism May 2 '16 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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