We have created our models, created our controller actions and forms. Now we are going to set up our templates. Let’s take a look at the controller action “new_project”:
@expose('ideas.templates.new_project')
def new_project(self):
""" Displays the project form. """
return dict(project_form=project_form)
In this action, we tell TurboGears that when new_project action is called, we are going to render it with “new_project.mak” file in the “templates” folder. We also pass “project_form” variable to the template.
Let’s create our templates. This is the content of new_project.mak:
<!-- ideas/templates/new_project.mak -->
<%inherit file="local:templates.master"/>
<%def name="title()">Project Form</%def>
${project_form()|n}
This is the content of new_task.mak:
<!-- ideas/templates/new_task.mak -->
<%inherit file="local:templates.master"/>
<%def name="title()">Task Form</%def>
${task_form()|n}
And this is the content of show_project.mak:
<!-- ideas/templates/show_project.mak -->
<%inherit file="local:templates.master"/>
<%def name="title()">${project.project_name}</%def>
<h1>${project.project_name}</h1>
<div>${project.project_description}</div>
<h2>Tasks</h2>
<ul>
% for task in tasks:
<li>${task.task_name}</li>
% endfor
</ul>
Now start your web server and visit “http://127.0.0.1:8080/new_project” and see how the project creation form looks like:
And let’s head to task creation page at “http://127.0.0.1:8080/new_task“:
Go ahead and play with it. Do something yourself. For more on the templates you can visit mako documentation.
