Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How do I pass variables via blocks (such as the list blocks)? 
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

Hi all -
I see that you can pass a category_id to a list view using this in the cms:

{{block type="catalog/product_list" category_id="5" template="catalog/product/list.phtml"}}

inside of List.php, the only code I can see that would retrieve this variable (category_id) is this:

if ($this->getCategoryId()) {
                $category 
Mage::getModel('catalog/category')->load($this->getCategoryId());
                
$layer->setCurrentCategory($category);
            
}

However, I cannot seem to replicate this:

//in layout update XML in admin section when editing Home Page within the CMS
<block type="myCompany_Leftbar/Leftbar" category_id="3" template="leftbar/left.phtml" />

<?php
/******************************
    Leftbar.php
*****************************/
class myCompany_Leftbar_Block_Leftbar extends Mage_Catalog_Block_Product_Abstract
{
    
public $catid;
    
    public function  
__construct()
    
{    
        parent
::__construct();
        
$layer Mage::getSingleton('catalog/layer');
        if(
$this->getCategoryId()) {
            $this
->catid $this->getCategoryId();
        
else {
            $this
->catid 'neg';
        
}
    }
}
?>

/******************************
    left.phtml
*****************************/
<div>
<?php
echo $this->catid;
?>
</div>

I get the output “neg” every time.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

So I figured out that if I use a function other than __constructor() in my Leftbar.php, I can pass parameters via this method:

{{block type="myCompany_Leftbar/Leftbar" name="mycompany.leftbar" category_id="5" template="leftbar/left.phtml"}}

However, I can’t seem to pass a parameter via the Layout Update XML in the CMS using:

<reference name="left">
<
block type="myCompany_Leftbar/Leftbar" name="mycompany.leftbar" category_id="3" template="leftbar/left.phtml" />
</
reference>
 
Magento Community Magento Community
Magento Community
Magento Community
 
smily
Jr. Member
 
Avatar
Total Posts:  15
Joined:  2008-05-05
Sydney, Australia
 

In case anyone else comes across this thread, I find that you have to use the setData method when using the layout updates section, eg:

<reference name="left">
    <
block type="myCompany_Leftbar/Leftbar" name="mycompany.leftbar" template="leftbar/left.phtml">
        <
action method="setData"><name>category_id</name><value>3</value></action>
    </
block>
</
reference>

Then you can access it in the template like so:

echo $this->getCategoryId();
 
Magento Community Magento Community
Magento Community
Magento Community
 
monsbutu2000
Jr. Member
 
Total Posts:  1
Joined:  2008-11-29
 

i trtied to post a variable i need in 2 or three subcategories and tried it like this in layout update xml:

<reference name="root">
   <
block type="catalog/category" name="kate" before="-">
              <
action method="setData"><name>kategorie</name><value>4</value></action>
   </
block>
</
reference>

how can i get this variable in my category page.
this htings i have tested but no one works.

1.
echo $kat = (int)$this->getRequest()->getParam('kategorie');
2.
$catid 
Mage::getModel('catalog/category')->getData('kategorie');

thanks for your help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
smily
Jr. Member
 
Avatar
Total Posts:  15
Joined:  2008-05-05
Sydney, Australia
 

As far as I know the setData method adds the data into the block not the model. Block data is accessible using $this in the phtml file (because the phtml file is using the block class ‘Mage_Catalog_Category’).

Try:

echo $this->getKategorie();

or possibly:

echo $this->getData('kategorie');
 
Magento Community Magento Community
Magento Community
Magento Community
 
jamieconnor
Jr. Member
 
Avatar
Total Posts:  11
Joined:  2008-02-12
Auckland, New Zealand
 

Just so you know I passed a variable from the cms pages to the template by going like

{{block type="catalog/product_list" manufacturer_id=14 template="catalog/product/brand_spotlight.phtml"}}

and then in the template I put

$this->getData(’manufacturer_id’)

Thanks for your help above.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bmotters
Member
 
Total Posts:  46
Joined:  2009-04-16
 

Doing it as a “category_id” attribute in the block only works in the “{{block}}” construct, where the email template filtering processes the attributes of the {{block ...}} So you can do this from email templates and on Cms pages (which use email templates too), but you can’t do anything analogous with straight “<block>” elements in layouts.  For layouts, you have to use an action:

<block type="catalog/product_list" >
  <
action method="setData"><name>category_id</name><value>42</value></action>
</
block>

such as was mentioned in a previous post. 

However, I should add I haven’t had a lot of success getting blocks from the Mage Catalog module to work independently of their usual context.  You can define attributes using actions, but blocks defined in the Catalog module seem to depend on a lot of context that is set by the controllers of the Catalog module, and it is difficult to get them to work in other contexts just by setting some attributes, like category_id.  For one thing, a lot of these blocks also set things with Mage::register. 

So this is a reasonable method to pass parameters to simple blocks and custom blocks where you know exactly what has to be set, but it is a bit unpredictable with Mage Catalog blocks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Solide
Member
 
Avatar
Total Posts:  44
Joined:  2010-03-13
Netherlands
 
bmotters - 09 March 2010 01:48 PM

Doing it as a “category_id” attribute in the block only works in the “{{block}}” construct, where the email template filtering processes the attributes of the {{block ...}} So you can do this from email templates and on Cms pages (which use email templates too), but you can’t do anything analogous with straight “<block>” elements in layouts.  For layouts, you have to use an action:

<block type="catalog/product_list" >
  <
action method="setData"><name>category_id</name><value>42</value></action>
</
block>

such as was mentioned in a previous post. 

However, I should add I haven’t had a lot of success getting blocks from the Mage Catalog module to work independently of their usual context.  You can define attributes using actions, but blocks defined in the Catalog module seem to depend on a lot of context that is set by the controllers of the Catalog module, and it is difficult to get them to work in other contexts just by setting some attributes, like category_id.  For one thing, a lot of these blocks also set things with Mage::register. 

So this is a reasonable method to pass parameters to simple blocks and custom blocks where you know exactly what has to be set, but it is a bit unpredictable with Mage Catalog blocks.

Thanx, this helped me a lot.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top