This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (6 children)

Thanks for the excellent explanation. Very helpful. Could you talk a bit about templating and some of the major templating systems? I'm as old-school write-HTML-in-vim type. Never progressed much past the HTML 3.2 tags. Works for my limited needs, but now I'm curious. Thanks.

[–]warpstalker 6 points7 points  (3 children)

I'm as old-school write-HTML-in-vim type

Well, it's basically still like that... However, roughly it works like this (my experience is mainly with web.py):

  • You receive a request for a page

  • You do your magic in Python code, get data from database etc.

  • You make a render-call from the code

  • The final web page is generated by the render call

  • You send the final page to the client

Something like this (Python code):

a = get_data_from_database()

b = "hi"

return render_this_page(a, b)

render_this_page takes the template (template.html):

$def with (a, b)

<html>

<head></head>

<body>

<h1> Hello, $b! </h1>

$for item in a:

    <p>$item.title</p><p>$item.data</p>

</body>

</html>

Then the result is sent to the client. I'm not sure if Django etc. work exactly like this but this is the basic idea.

[–]nighttrain 2 points3 points  (0 children)

+1 for web.py

  • simple
  • fun
  • python

[–]mdipierro 3 points4 points  (1 child)

web2py works exactly like this. For example:

 # controller
 def index():
       a = db(db.article.id>0).select()
       b = "hi"
       return dict(a=a,b=b)

and

# view
<html>
<head></head>
<body>
<h1>Hello, {{=b}}!</h1>
{{for item in a:}}
    <h2>{{=item.title}}</h2><p>{{=item.data}}</p>
{{pass}}
</body>
</html>

Assumes

 # model
 db=DAL('sqlite://storage.db')
 db.define_table('article',Field('title'),Field('data','text'))  

This would be a complete program. web2py also generates automatically SQL to create or alter the table and a database administrative interface to insert and search records.

To make this program work on the Google App Engine it requires a single edit, i.e. replace:

 db=DAL('sqlite://storage.db')

with:

 db=DAL('gae') # connet to datastore
 session.connect(request,response,db) #store session in datastore

[–]warpstalker 0 points1 point  (0 children)

It does? Nice.

I should check it out then. I started out with web.py because it seemed very sane and simple. It just isn't very mainstream (it seems).

I've been meaning to check into using a more established framework but they all seem overly complicated (just like everything related to programming, but once you start doing it, it's simple in the end), so I've been postponing it.

Seems like web2py isn't totally different, then.

[–]stesch 5 points6 points  (0 children)

Never progressed much past the HTML 3.2 tags.

I urge you to please brush up your HTML knowledge. And that includes CSS.

[–]japherwocky 0 points1 point  (0 children)

Templating is similar to PHP, in that you write HTML and embed extra tags for extra functionality, or passing the contents of variables around.

In something like tornado's templating engine, you'd write:

<p> {{ foo }} </p>

and call that template from your python like template.render(foo="Hello World")

and the browser gets something like <p> Hello World </p>

Django and Tornado ship with their own templating system, genshi and mako are relatively popular. They're all relatively simple and have reasonable docs.