tags:

views:

28

answers:

1

In this article Django templates like this

{% for i in mylist %}
  <tr>
    <td>{{i.replist|join:"</td><td>" }}</td>
  </tr>
{% endfor %}

prints the list mylist which is an object. Can this be done in Mako? Thanks.

EDIT

class Rep(db.Model):
    author = db.UserProperty()
    replist = db.ListProperty(str)
    unique = db.ListProperty(str)
    date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
    def get(self):      
        user = users.get_current_user()
        greeting = None

        if user:
            greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
                         (user.nickname(), users.create_logout_url("/")))
        else:
            greeting = ("<a href=\"%s\">Sign in or register</a>." %
                        users.create_login_url("/"))

        L = []                                   
        s = self.request.get('sentence')           
        L.append(s)                              

        L = L[0].split('\r\n')     

        def f2(L):
            checked = []
            for e in L:
                if e not in checked:
                    checked.append(e)
            return checked

        Rep().replist = L                                   
        Rep().put()                              
        mylist = Rep().all().fetch(10)

        leng = len(mylist)
        T = type(mylist)
        self.response.out.write("Ttttt")
        print [i for i in mylist]       
        L2 = f2(L)
        x = len(L)
        y = len(L2)
        delta = x - y
        for i in range(delta):
             L2.append('')

         q = Rep().all()
         results = q.fetch(10)
        db.delete(results)



         template_values = {"s": s,
                           "L": L,
                           "L2": L2,
                           "x": x,
                           "y": y,
                           "greeting": greeting,
                           "mylist": mylist,
                           "leng": leng,
                           "T": T,
                           }

        path = os.path.join(os.path.dirname(__file__), 'main.mako')
        templ = Template(filename=path)
        self.response.out.write(templ.render(**template_values))  
+1  A: 

http://www.makotemplates.org/docs/syntax.html#syntax_control

EDIT: I would suggest using an ordered list or named tuple instead of a dictionary so you can focus on your output instead of your data.

% for entry in mylist:
    <tr>
        % for key, value in entry:
        <td>${value}</td>
        % endfor
    </tr>
% endfor

You can nest these if you need to output many <td>s. To do the join part, just use a nested loop.

Scott
I think he's looking for a replacement for the `join` filter rather than the `for` tag.
Ignacio Vazquez-Abrams
Yes, I am trying to render mylist which is list object: `mylist = Rep().all().fetch(10)` for loop just renders the object not the elements of the object.
Zeynel
Can you give us an example of one of your list items? If it's an object returned by a model, you should be able to do ${i.member} in place of ${someVar}
Scott
Ok, thanks. I added the script as an edit above. There is a lot of junk in it because I've been testing the answers here.
Zeynel
Try using a named tuple instead of a dictionary, then you can call your values in order they were set; dictionaries are not ordered and can cause some headaches sometimes. Also, try using better named variables, its a pet peeve, but helps readability of code.
Scott
Thanks. At this point I am confused. I thought I was using a list not a dictionary. I will continue tomorrow. Thanls again.
Zeynel
I may have been wrong, try running in a debugger and insert a breakpoint at `self.response.out.write(templ.render(**template_values))` so you can inspect what is what.
Scott