[web.py] Rendering html template.

Moving to this part http://webpy.org/docs/0.3/tutorial#templating

The basic idea of the template things here is just like what I used to work with ASP.NET years ago. 
It’s a mix of python syntax with html tags to insert properly data into html templates.

For example, the given example from tutorial.

$def with (name)

$if name:
    I just wanted to say <em>hello</em> to $name.
$else:
    <em>Hello</em>, world!

    While the $def with(name) is kinda function declaration for html template where you can define the expected parameters that you want to pass into in the future use.
    The rest of this is in python-like syntax, with the $ sign to denote that this is python line of code mixed into html syntax.

They also noticed that we have 2 ways of using variable in this mode, if you use variable like $name , when you pass into some sort of escape html syntax ( like <em>MyName</em>, it will escape your text properly to render in browser. If you want to display exactly what you’re passing into, you may want to try $:name . That’s it. 

Next , we will find out how to deploy this template into our web app.

Let’s move back to our code.py  

We will declare a global variable name renderer

web.template.render('template/')

We got/template’ is the directory ( in a relative direction with our current main function code i.e, code.py)

Next, in our GET method, we will use this renderer to represent data onto the html template.

name = 'Toan'
return render.index(name)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: