views:

111

answers:

4

In app engine there a way to use templates a bit more like php/javascript(document.write)?

for instance i would rather do:

<html>
<python>
print "Hello world"
</python>
</html>

rather than all the {IF } {ELSE } django stuff.

+3  A: 

You want embedded python in html page for that look into mako (http://www.makotemplates.org/), you don't even need print e.g.

<%inherit file="base.html"/>
<%
    rows = [[v for v in range(0,10)] for row in range(0,10)]
%>
<table>
    % for row in rows:
        ${makerow(row)}
    % endfor
</table>

<%def name="makerow(row)">
    <tr>
    % for name in row:
        <td>${name}</td>\
    % endfor
    </tr>
</%def>

it comes with inheritance, Callable blocks, is faster and IMO better than django and any day better than php style stuff.

for mako on GAE see https://code.launchpad.net/~pylons-gae/mako/mako-gae

Anurag Uniyal
A: 

The simplest way is to use string templates from the standard library.

Koen Bok
A: 

One of the web programming best practices it to not mix business or page logic with HTML. That's why the templates were created after all, so the code can process the request, call the appropriate logic and prepare the objects used to display the response before any output is made. Why do you want to go the other way around?

jbochi
if i wanna make a tiny change i have to mess with what code is being sent to the template etc. Also i dont think all the symbols are very readable
tm1rbrt
In my opinion, templates make the code more readable, reusable and maintainable, but I respect your opinion. If you are not used to the syntax, it can be confusing.
jbochi
PS: You need to send objects (like lists or strings) to the template, and not code.
jbochi
+2  A: 

The Tornado project's template module allows the insertion of python code, and it's very fast as well. It works well within App Engine, despite being designed to work with the rest of the Tornado framework and the Tornado HTTP server.

Wooble
I like this one. Gonna have a bash
tm1rbrt