简体   繁体   English

Magento:如何在.phtml文件中调用动态窗口小部件块ID

[英]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. 我的主页上有一些静态块,1个广告旋转器,1个滑块,显示在特色产品类别中定义的产品,最后是另一个类别,当前指向静态类别,但它与特色相同。

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. 我的任务是随机化最后一个类别,我能够通过编写一个自定义.phtml文件来实现这一点,并让它循环使用mt_rand一些已经创建的静态块。

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: 然而,这更进了一步,我要做的是有一个静态块,并且类别ID是随机的,这是一个例子:

{{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: 现在使用这个概念,我试图做的是尝试创建另一个.phtml文件,并让静态块调用我的自定义.html这是这样的东西:

<?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. 我的真正问题在于,将此信息打印到屏幕上并没有产生我一直希望的效果,它正在做的只是打印文本,而每次刷新屏幕确实给我一个新的随机元素数组ID明智,它只是文本到屏幕。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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