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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?