dsegw3424 2014-07-24 00:03
浏览 18
已采纳

从html而不是php调用smarty变量

HTML CODE:

<html><body>
{section name=a loop=$items}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>

PHP CODE:

$pid = '12';

$items = $cbvid->get_channel_items($pid);

assign('items',$items);

This is perfectly working fine, with the integer 12 being my php code. However, I wanted to add the integer 12 and call it from the html code, but it didn't work.

I tried:

<html><body>
{section name=a loop=$cbvid->get_channel_items(12)}
{$items[a].title}
{include file="/directory/showstuff.html" video=$items[a]}
{/section}
</body> </html>

But it didn't work. How can I do it?

  • 写回答

1条回答 默认 最新

  • dousi8559 2014-07-24 00:17
    关注

    Just don't do it. That looks like if you would like to move business logic to representation layer - that is not what Smarty is used for. Prepare data beforehand, then give it to template.

    But if you really want it to work, use foreach

    <html><body>
    {foreach  from=$cbvid->get_channel_items(12) item=video}
    {$video.title}
    {include file="/directory/showstuff.html" video=$video}
    {/foreach}
    </body> </html>
    

    The reason why it did not work with sections, was because you have no $items variable defined, still, you are trying to get value of it. This is where you need to use assign.

    <html><body>
    {assign var="items" value=$cbvid->get_channel_items(12)}
    {section name=a loop=$items}
    {$items[a].title}
    {include file="/directory/showstuff.html" video=$items[a]}
    {/section}
    </body> </html>
    

    Still, I prefer foreach.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题