Question

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))  
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top