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.

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 '15 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 '15 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 '15 at 19:38
  • 1
    @easymoden00b see my edited answer
    – Simon
    Mar 2 '15 at 20:20
  • 1
    If the model is used before the call of loadLayout(), yes.
    – Simon
    Mar 2 '15 at 20:48
0

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>

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.