Permalink
Browse files

add docs to templating

  • Loading branch information...
1 parent ddf033c commit cbd51cbac107758eb8fa43126e462575a1fed8f6 @asldevi asldevi committed Nov 6, 2013
Showing with 71 additions and 21 deletions.
  1. +5 −20 docs/db.rst
  2. +2 −1 docs/index.rst
  3. +64 −0 docs/templating.rst
View
@@ -7,31 +7,16 @@ Web.py provides a simple and uniform interface to the database that you want to
Create database object
------------------------
-You could create a database object with `web.database()`. Make sure that you have appropriate database library installed.
+The first thing to work with databases from web.py is to create a
+create a database object with `web.database()`. It returns database object, which has convenient methods for you to use.
-PostgreSQL
-``````````
-Database Library: `psycopg2`
-::
-
- db = web.database(dbn='postgres', db='dbname', user='username', pw='password')
+Make sure that you have appropriate database library installed (`psycopg2` for PostgreSQL, `MySQLdb` for MySQL, `sqlite3` for SQLite).
-MySQL
-`````
-Database Library: `MySQLdb`
::
- db = web.database(dbn='mysql', db='dbname', user='username', pw='password')
-
-
-SQLite
-``````
-
-Database Library: `sqlite3`
-::
-
- db = web.database(dbn='sqlite', db='dbname')
+ db = web.database(dbn='postgres', db='dbname', user='username', pw='password')
+`dbn` for MySQL is `mysql` and `sqlite` for SQLite. SQLite doesn't take `user` `pw` parameters.
Multiple databases
``````````````````
View
@@ -9,11 +9,12 @@ Welcome to web.py's documentation!
Contents:
.. toctree::
- :maxdepth: 2
+ :maxdepth: 3
urlmapping
input
db
+ templating
Getting Started
===============
View
@@ -0,0 +1,64 @@
+Templates
+=========
+
+Writing HTML from inside Python can get cumbersome; it's much more fun to write Python from inside HTML. Luckily, web.py makes that pretty easy.
+
+Let's make a new directory for our templates (we'll call it templates). Inside, make a new file whose name ends with HTML (we'll call it index.html). Now, inside, you can just write normal HTML:
+
+<em>Hello</em>, world!
+Or you can use web.py's templating language to add code to your HTML:
+
+::
+
+ $def with (name)
+
+ $if name:
+ I just wanted to say <em>hello</em> to $name.
+ $else:
+ <em>Hello</em>, world!
+
+As you can see, the templates look a lot like Python files except for the def with statement at the top (saying what the template gets called with) and the $s placed in front of any code. Currently, the templetor requires the $def statement to be the first line of the file.
+
+
+Now go back to code.py. Under the first line, add:
+
+::
+
+ render = web.template.render('templates/')
+
+This tells web.py to look for templates in your templates directory. Then change index.GET to:
+
+::
+
+ name = 'Bob'
+ return render.index(name)
+
+('index' is the name of the template and 'name' is the argument passed to it)
+
+Visit your site and it should say hello to Bob.
+
+But let's say we want to let people enter their own name in. Replace the two lines we added above with:
+
+::
+
+ i = web.input(name=None)
+ return render.index(i.name)
+
+Visit / and it should say hello to the world. Visit /?name=Joe and it should say hello to Joe.
+
+Of course, having that ? in the URL is kind of ugly. Instead, change your URL line at the top to:
+
+'/(.*)', 'index'
+and change the definition of index.GET to:
+
+::
+
+ def GET(self, name):
+ return render.index(name)
+
+and delete the line setting name. Now visit /Joe and it should say hello to Joe.
+
+Escaping
+````````
+web.py automatically escapes any variables used in templates, so that if for some reason name is set to a value containing some HTML, it will get properly escaped and appear as plain text. If you want to turn this off, write $:name instead of $name.
+

0 comments on commit cbd51cb

Please sign in to comment.