0

is this possible? I need to dynamically assign a variable used in a templates Model class. This variable will be generated via a CMS page. Below is the template and block in question. How can I pass a dynamically assigned variable to it's model?

<reference name="content">
        <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml" ></block>
</reference>

If possible i'd like to assign a variable to this via the block and pass it to the model using session variables (if I cant access this variable from the block outright).

This is the location of the block i'd like to access this variable:

code/core/Mage/Catalog/Block/Layer/View.php

Of course, I will extend this later as it is core atm.

2 Answers 2

4

You can always set a variable on a block like that:

<reference name="content">
    <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml">
        <action method="setMyVar">
            <value>myvalue</value>
        </action>
    </block>
</reference>

This will set the variable my_var with the value myvalue in the specified block. You will be able to get the value of this variable via $this->getMyVar(). Note that this will only work after the loadLayout() method of the respective Mage_Core_Controller_Front_Action has been called.

12
  • How do I call it? Will I be able to get this data from within the block and then pass it to the model for use? Mar 2, 2015 at 19:32
  • You "call it" via layout XML as shown above. You should be able to use it in the block, at least after calling $this->loadLayout().
    – Simon
    Mar 2, 2015 at 19:35
  • okay, how do I manipulate this assigned variable within the block itself? I tried to log this: Mage::log($this->getData('MyVar')); however it doesn't show anything within the log. Mar 2, 2015 at 19:38
  • 1
    @easymoden00b see my edited answer
    – Simon
    Mar 2, 2015 at 20:20
  • 1
    If the model is used before the call of loadLayout(), yes.
    – Simon
    Mar 2, 2015 at 20:48
1

I know this is an old thread but came across it and noticed that the correct way to pass a variable to a method should be with an argument tag:

   <reference name="content">
        <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml">
            <action method="setMyVar">
                <argument name="value" xsi:type="string">myvalue</argument>
            </action>
        </block>
    </reference>
1
  • This worked for Magento 2.3.4. Thanks.
    – Ejaz
    Mar 3, 2022 at 8:30

Your Answer

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

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