How can I nest a foreach or section loop in JS in Smarty template?

Source: Internet
Author: User
Tags smarty template
This is a static page.
          


Use Smarty loops to achieve this
{literal}                                 


Reply to discussion (solution)

As on the Web page, it's better to change the delimiter "{<"

PHP can be converted into JSON with Json_encode and then passed to Smarty, JS parsing json output is much simpler.

{foreach Item=lop from= $tuan}
Box.add ({"url": "./upload/admin/{$lop. Image}", "title": "{$lop. Title}"});
{/foreach}

This loop can be encapsulated by PHP, and the value will be passed to the template.

{foreach Item=lop from= $tuan}
Box.add ({"url": "./upload/admin/{$lop. Image}", "title": "{$lop. Title}"});
{/foreach}

This loop can be encapsulated by PHP, and the value will be passed to the template.


This is justified. Either encapsulated by PHP, the value is passed to the template, or it is encapsulated by the JS function, and the value is passed to the template to determine if the function is executed.

PHP can be converted into JSON with Json_encode and then passed to Smarty, JS parsing json output is much simpler.


Elder brother, have not used Json_encode,json, task a little rush. Help people in the end, write some specific code, thank you!

example, you can change it by yourself.

var json= ' [{' id ': "name": "AA"},{"id": "name": "BB"}] '; JSON object from PHP json_encode var obj=eval (JSON) for (var i=0; i
  
  

The third floor method, I feel it.

example, you can change it by yourself.

var json= ' [{' id ': "name": "AA"},{"id": "name": "BB"}] '; JSON object from PHP json_encode var obj=eval (JSON) for (var i=0; i
     
    



I tried to change it in your case, trying to output the format I wanted correctly, but it was outputting the data directly on the page. JS can not read, how should I write it?

Remove the document.write

Remove the document.write



for (var i=0; i
}

So, but still no effect ah!

What exactly do you want to achieve?

for (var i=0; I 
       
       

I met the same problem as the landlord ... I used a stupid method. is to use {literal}{/literal} to separate the variable and JS code. The
complete code is as follows

 "!--{if $recommend _product}-->"!--{/if}--> 
" Li>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.