How to pass parameter with block form contents from cms pages in magento
Solution 1
I'm not 100% sure what you are asking, but if you are trying to pass a variable to multibanners.phtml
from the code above then you could create another attribute similar to category_id="9"
and in multibanners.phtml
you could get the value using $this->getData("category_id");
e.g.
{{block ... my_var="value here" ... template="multibanners/multibanners.phtml"}}
In multibanners.phtml
:
$this->getData('my_var');
Solution 2
I think the problem here stems from the block type you are calling. When you define a type, you're telling Magento to load that model and pass it the appropriate data - which then only exposes the functions defined on that specific model.
A better solution may be to reference the core block type "core/template" which exposes the ->getData() method, and then load the "multibanners/multibanners" model to work with and output the data.
{{block type="core/template" category_id="9" name="multibanners" alias="multibanners" template="multibanners/multibanners.phtml"}}
I'm not sure what the proper syntax is to load 'multibanners', but in the multibanners.phtml would be something like this:
<?php
$catId = $this->getData('category_id');
$multibanner = Mage::getModel('multibanners/multibanners')->load($catId);
/**
** Generate some output here.
*/
?>
Solution 3
I found this very useful and I thought I'd explain what I did in case it helps anyone else.
I have a few static blocks that I use to build some static pages with basic non-changing information (about-us type pages) which include some photos. The photos are very large (for a web page) and I wanted to use Magento's resizing facility. The only way I could work out to do this was to use the ideas here. I now have a block I can include on any cms static page/block when I want to have a resized image with several parameters. It's like a subroutine (am I allowed to say that?! ;o). Anyway, here's what I did.
The block:
{{block type="core/template" name="display_resized_img" gimg="IMG_0559.JPG" gsize="300" gpath="/wysiwyg/ShopFront/" gclass="about-us" galt="The shop" template="utilities/display_resized_img.phtml"}}
and the phtml code file:
<?php
/*
* Displays and resizes an image as requested from the block.
* The image is only resized if it hasn't been already.
*/
$img = $this->getData('gimg');
$size = $this->getData('gsize');
$path = $this->getData('gpath');
$class = $this->getData('gclass');
$alt = $this->getData('galt');
$resizePath = Mage::getBaseDir ('media') . $path . "resized/" . $size . $img;
if (!file_exists($resizePath)):
$imagePath = Mage::getBaseDir('media') . $path . $img;
$imageObj = new Varien_Image($imagePath);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->keepFrame(FALSE);
$imageObj->resize($size, null);
$imageObj->save($resizePath);
endif;
$resizeUrl = Mage::getBaseUrl ('media') . $path . "resized/" . $size . $img;
?>
<img class="<?php echo $class; ?>" src="<?php echo $resizeUrl ?>" alt="<?php echo $alt; ?>">
Note I save my re-sized images in a resized folder and add the new size to the image filename so I can easily see what's happening and manage the files.
Thanks for reading!
Mahmood Rehman
I am a developer with a wide range of skills (PHP, HTML, CSS, Javascript (jQuery), MySQL, Magento, WordPress). Always keen to learn new things
Updated on July 09, 2022Comments
-
Mahmood Rehman almost 2 years
I want to pass a variable with the block code like of JSON type in magento,
{{block type="multibanners/multibanners" category_id="9" name="multibanners" alias="multibanners" template="multibanners/multibanners.phtml"}}
from cms pages content area , but I don't receive any thing.
If I use block with action method than I easily get my value. Anyone know how can I pass variable with my custom block? -
Mahmood Rehman over 11 yearsOk let me explain more details . i want to pass variable like category_id from the block but i can't get values in phtml or block using $this->getData('my_var') or $this->getMyVar().I have try your code but nothing happen.Hope you got what i want to do.
-
MagePal Extensions over 11 yearsDo you have cache enable and what version of magento are you using?
-
Mahmood Rehman over 11 yearsI disable all type of cache due to developemnt and i am using Magento ver. 1.7.0.0.
-
Mahmood Rehman over 11 yearsYes if i pass values from block with action method from xml than it works , but for content cms pages when i use the json type block than it dont show anything.
-
MagePal Extensions over 11 yearsAdd this code to your cms page {{block type="core/template" template="test.phtml" page_type="recentposts"}} .. in test.phtml enter ... hello world <?php echo this->getData('page_type'); ?> let me know the result
-
MagePal Extensions over 11 yearsYes or No ... did it display 'hello world'?
-
Mahmood Rehman over 11 yearsHi thanks to R.S.Your answer is right.The reason why its not running on my system is the editor style.when i remove the style and run code {{block type="multibanners/multibanners" name="multibanners" alias="multibanners" template="multibanners/multibanners.phtml" category_id="8"}}.I got my answer. Thanks again so much.
-
PedroKTFC almost 11 yearsJust wondering why somebody voted this down? Maybe I've misunderstood the purpose of these forums but I thought sharing our experiences was part of it?
-
Jongosi about 10 yearsPedroKTFC, I totally agree with you. I guess haters'll hate. Personally, I found your answer helpful; lemme push the vote up +1 ;)