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.

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

报告相同问题?

悬赏问题

  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)