Ken Block
Ken Block

Reputation: 3503

Could not pass list object into template bottle

I have some problem with bottle template. I can not pass python list to template. My code is below:

@route('/'):
def home():
    return template('home', var=['item1', 'item2'])

And this is home.tpl:

<html>
   <ul>
       #for item in var:
         <li>{{item}}</li>
       #end
   </ul>
</html>

I think it work but exception not defined variable 'item' throwed. Could please tell me where my error?

Upvotes: 1

Views: 881

Answers (2)

Jerry Liu
Jerry Liu

Reputation: 11

If you want to iterate through the list in the reverse order, the following template will work.

<html>
     <ul>
         %for i in range(len(var)-1,-1,-1):
             <li>{{var[i]}}</li>
         %end
     </ul>
</html>

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121744

The bottle template syntax uses %, not # to mark python(ish) blocks:

<html>
   <ul>
       %for item in var:
         <li>{{item}}</li>
       %end
   </ul>
</html>

Upvotes: 3

Related Questions