Frameworks

Massimo Di Pierro mdipierro at cs.depaul.edu
Tue Oct 20 19:02:36 EDT 2009


On Oct 20, 2009, at 4:59 PM, Emmanuel Surleau wrote:

> Compared to custom tags in, say, Mako? Having to implement a mini- 
> parser for
> each single tag when you can write a stupid Python function is  
> needless
> complication.

I like Mako a lot and in fact web2py template took some inspiration  
from it. Here is a mako example:

% for a in ['one', 'two', 'three', 'four', 'five']:
     % if a[0] == 't':
      its two or three
     % elif a[0] == 'f':
     four/five
     % else:
     ${a}
     %endif
% endfor


Here is the same in web2py-ese, translated line by line

  {{for a in ['one', 'two', 'three', 'four', 'five']:}}
     {{if a[0] == 't':}}
      its two or three
     {{elif a[0] == 'f':}}
     four/five
     {{else:}}
     {{=a}}
     {{pass}}
{{pass}}

Legend Mako -> web2py
% -> {{ .... }}
endif -> pass
endfor -> pass
${...} -> {{=...}}

Mako introduces terms like "endif", "endfor" which are not Python  
keywords. Web2py only uses valid python keywords.

Here is another example, in Mako:

<%def name="myfunc(x)">
     this is myfunc, x is ${x}
</%def>
${myfunc(7)}

and the same in web2py

{{def myfunc(x):}}
    this is myfunc, x is {{=x}}
{{return}}
{{myfunc(7)}}

Again web2py does not introduce any new syntax. only valid Python in  
{{...}} tags.
(Notice {{myfunc(7)}} not {{=myfunc(7)}} becase the function is  
printing, we are not printing the return value of the function).

Mako needs to parse % ..., <%>...</%>, ... and ${...}. web2py needs to  
parse only {{...}}.

The use of {{...}} in web2py is inspired by Django. This has a big  
advantage over <%...%>, it is transparent to html editors and so you  
use any html editor with the templates.

Massimo







More information about the Python-list mailing list