Frameworks
Emmanuel Surleau
emmanuel.surleau at gmail.com
Wed Oct 21 02:06:35 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.
Ingenious. Web2py looks more consistent (everything relies on {{ }} while Mako
uses %, $ and <% >). On the other hand, the Mako syntax is a bit lighter to my
eyes. Tradeoffs, tradeoffs...
> 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.
Bah, as if anyone needed anything else than vim :) Just joking, I see the
advantage if you are working with external designers.
Cheers,
Emm
More information about the Python-list
mailing list