Στις 24/6/2013 1:29 πμ, ο/η ru...@yahoo.com έγραψε:
In this simple example, there is not much advantage of Mako
over your templates.  But with more complicated cases, for
instance, when you have tables or forms that you want to
dynamically construct from external data (info extracted
from a database for example) then a template system like Mako
makes your Python code simpler because all the table and form '
stuff is in the template file, and all the Python code has to
do is get the data and pass it to the template's render method.

For example, for a web page that shows a table of users and
their last login dates, your template might contain something
like

    <table>
       <tr><th>Username</th>  <th>Last Login</th></tr>
       %for user,llog in userrecs:
         <tr><td>${user}</td>  <tr><td>${llog}</td></tr>
       %endfor
    </table>

And your Python cgi code

   userdata = mysqlcursor.execute (
     "SELECT username,MAX(logintime) FROM userlogins GROUP BY username", [])
   html = template.render (userrecs=userdata)
   print (html)

When you call template.render() above, Mako and the template
will create an html table row for each user in userdata.

I see. if for example i take my files.py where it acts like a template and python script all together and its in fact displaying not usernames but constructs an html table form extracted from database info, why you mako's approach and not keep it as i have it now which is:


# =================================================================================================================
# Display ALL files, each with its own download button
# =================================================================================================================
print('''<body background='/data/images/star.jpg'>
                 <center><img src='/data/images/download.gif'><br><br>
                 <table border=5 cellpadding=5 bgcolor=green>
''')

try:
        cur.execute( '''SELECT * FROM files ORDER BY lastvisit DESC''' )
        data = cur.fetchall()
        
        for row in data:
                (filename, hits, host, lastvisit) = row
                lastvisit = lastvisit.strftime('%A %e %b, %H:%M')
                
                print('''
                <form method="get" action="/cgi-bin/files.py">
                        <tr>
                                <td> <center> <input type="submit" name="filename" 
value="%s"> </td>
                                <td> <center> <font color=yellow size=5> %s 
</td>
                                <td> <center> <font color=silver size=4> %s 
</td>
                                <td> <center> <font color=orange size=4> %s 
</td>
                        </tr>
                </form>
                ''' % (filename, hits, host, lastvisit) )
        print( '''</table><br><br>''' )
except pymysql.ProgrammingError as e:
        print( repr(e) )

Why use mako's approach which requires 2 files(an html template and the actual python script rendering the data) when i can have simple print statements inside 1 files(my files.py script) ?
After all its only one html table i wish to display.


All code above is untested and from memory so there is probably
errors in it.  I was just trying to give you an idea of how
template systems can make cgi code simpler without moving
everything to a full web framework.

And if we wanted to to compare an html template method to a web framework solution?

What are the benefits of one over the other?

I know you dont use the latter but this questios is for averybody that does.


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to