简体   繁体   中英

Magento: How to call dynamic widget block ID in a .phtml file

Ok here is what I am trying to do, I have read a ton of links and posts but it seems what I am aiming to do is a bit different.

I have a few static blocks on my home page, 1 ad rotator, 1 slider that displays products defined in a featured products category and lastly another category which is currently a pointing to a static category but it does the same as the featured.

My task is to randomize that last category, I was able to achieve this by writing a custom .phtml file and have it cycle through using mt_rand a few already created static blocks.

This however goes one step deeper, what I am looking to do is have a single static block, and have the the category ID random, here is an example:

{{block type="catalog/product_list" category_id="392" template="catalog/product/home-list.phtml"}}

Now using this concept, what I was attempting to do was to try and create another .phtml file, and have the static block call my custom .html which is something of this sort:

<?php
$input = array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150);
$rand_keys = array_rand($input, 2);
echo "{{block type=\"catalog/product_list\" category_id=\"{$input[$rand_keys[0]]}\" template=\"catalog/product/home-list.phtml\"}}";
?>

Now, this example actual does the job, I can load an array of what ever I want category wise and just have it cycle through randomly or the most part.

Where my real issue comes into play is that printing this information to screen is not having the effect I had been hoping for, what it is doing is simply printing the text and while every refresh of the screen does give me a new random element of the array ID wise, its just text to the screen.

If any one could help me address the issue and be able to get the text to translate into the working widget tag that would be fantastic as I have spent a good while researching and have found dead ends each time.

Thanks in advance,

Cheers!

You need a slightly different syntax to use in views. Some pseudo examples

echo $this->getLayout()->createBlock('catalog/product_list')->setTemplate('catalog/product/home-list.phtml')->setCategoryId('392')->toHtml();

or if the block is already existing in the layout you can get it by name:

echo $this->getLayout()->getBlock('blockname')->setTemplate('catalog/product/home-list.phtml')->setCategoryId('392')->toHtml();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM