Home  >  Article  >  Backend Development  >  smarty模板中,js中怎么嵌套foreach或section循环?

smarty模板中,js中怎么嵌套foreach或section循环?

WBOY
WBOYOriginal
2016-06-23 13:50:481155browse

这是静态页。

          <script>                                     var box =new PPTBox();                                     box.width = 1250; //宽度                                     box.height = 490;//高度                                     box.autoplayer = 3;//自动播放间隔时间                                     //box.add({"url":"图片地址","title":"悬浮标题","href":"链接地址"})                                     box.add({"url":"images/tu1.jpg","title":"悬浮提示标题1"});                                     box.add({"url":"images/tu2.jpg","title":"悬浮提示标题2"});                                     box.add({"url":"images/tu3.jpg","title":"悬浮提示标题3"});                                     box.add({"url":"images/tu4.jpg","title":"悬浮提示标题4"});                                     box.show();                                </script>


用smarty循环,实现这样
{literal}                                 <script>                                     var box =new PPTBox();                                     box.width = 1250; //宽度                                     box.height = 490;//高度                                     box.autoplayer = 3;//自动播放间隔时间                                     //box.add({"url":"图片地址","title":"悬浮标题","href":"链接地址"})	{foreach item=lop from=$tuan }        box.add({"url":"./upload/admin/{$lop.image}","title":"{$lop.title}"});	{/foreach}        box.show();                                </script>


回复讨论(解决方案)

就和在网页上的一样,最好改一下定界符“{

可以在php里面用json_encode 转化为json后再传给smarty , js 解析json输出就简单多了。

 {foreach item=lop from=$tuan }
        box.add({"url":"./upload/admin/{$lop.image}","title":"{$lop.title}"});
    {/foreach}

这个循环可以通过php来封装,将值传到模板即可。

 {foreach item=lop from=$tuan }
        box.add({"url":"./upload/admin/{$lop.image}","title":"{$lop.title}"});
    {/foreach}

这个循环可以通过php来封装,将值传到模板即可。


此言有理。要么通过php来封装,将值传到模板即可;要么通过js函数来封装,将值传到模板,确定是否执行此函数。

可以在php里面用json_encode 转化为json后再传给smarty , js 解析json输出就简单多了。


大哥,没用过json_encode,json,任务有点赶。帮人帮到底,写点具体的代码,谢谢!

例子,自行改下就行。

var json='[{"id":12,"name":"aa"},{"id":13,"name":"bb"}]';  // json对象来自php的json_encode var obj=eval(json) for(var i=0; i<obj.length; i++)  {      document.write(obj[i].id+" " + obj[i].name);  }

三楼的方法我感觉可行

例子,自行改下就行。

var json='[{"id":12,"name":"aa"},{"id":13,"name":"bb"}]';  // json对象来自php的json_encode var obj=eval(json) for(var i=0; i<obj.length; i++)  {      document.write(obj[i].id+" " + obj[i].name);  }


<script>                                     var box =new PPTBox();									 var json='[{"url":"images/tu1.jpg","title":"悬浮提示标题1"},{"url":"images/tu2.jpg","title":"悬浮提示标题1"},{"url":"images/tu3.jpg","title":"悬浮提示标题1"},{"url":"images/tu4.jpg","title":"悬浮提示标题1"}]';									 var obj=eval(json)                                     box.width = 1250; //宽度                                     box.height = 490;//高度                                     box.autoplayer = 3;//自动播放间隔时间                                     //box.add({"url":"图片地址","title":"悬浮标题","href":"链接地址"})									 for(var i=0; i<obj.length; i++){										document.write('box.add({"url":'+'"'+obj[i].url+'"'+","+'"title":'+'"'+obj[i].title+'"'+"});");									 }                                     box.show();                                </script>

我照着你的例子试着改了改,想在能正确输出我要想的格式了,但它是直接在网页上输出了数据。js不能读取,请问应该怎么写呢?

将document.write 去掉

将document.write 去掉



for(var i=0; i }

这么写,但是还还是没有效果啊!

你究竟要达到什么目的?

for(var i=0; i<obj.length; i++)  {      box.add({"url":'"'+obj[i].url+'"',"title":'"'+obj[i].title+'"'});  }

我遇到了跟楼主一样的问题。。。我使用了一个笨办法。就是用{literal}{/literal}挨个把变量和js代码分隔开。
完整代码如下

<!-- {if $recommend_product} -->		<script type="text/javascript" src="../images/pptBox.js"></script>		<script>			{literal}			var box =new PPTBox();			box.width = 223; //宽度			box.height = 123;//高度			box.autoplayer = 3;//自动播放间隔时间			{/literal}			<!-- {foreach from=$recommend_product name=recommend_product item=product} -->			{literal}			box.add({"url":"{/literal}{$product.thumb}{literal}","href":"{/literal}{$product.url}{literal}","title":"{/literal}{$product.name}{literal}"});{/literal}			<!-- {/foreach} -->			{literal}			box.show();			{/literal}		</script>		<!-- {/if} -->

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php如何接收jsonNext article:php mysql 数据操作问题