2.4. Custom Greetings (Hello from the Controller)
Sure, that wasn't much code, but it looks an awful lot like a static HTML page, and you probably want to see something more dynamic; so let's take a look at how this simple application works and add a bit of dynamic content, which is generated in another Python file called the controller, to our page before we display it to the user.
In the hello project is a hello folder that contains the file controllers.py. This file is what gets called when you browse to your web application. When you open that file in a text editor, something like this displays:
import turbogears from turbogears import controllers, expose class Root(controllers.RootController): @expose(template="hello.templates.welcome") def index(self): import time return dict(now=time.ctime())
This file defines what happens when the user connects to a specific URL. Each method in the Root class defines another URL, so if you want to have a http://localhost:8080/hello URL, you can easily do so: Just define a new method called hello to handle the request and send back a response to the browser.
Let's do that now. Just add this to the bottom of the file:
@expose(template="hello.templates.hello") def hello(self): import time return dict(greeting="Greetings from the Controller")
Notice a couple of important things about this code. Don't worry if parts of it do not make sense yet; we cover the details soon. For now, it's enough to know that every time you go to http:/localhost:8080/hello, this method is called; and when it has finished processing, it calls the hello.kid template (which is processed by the Kid templating engine) found in our templates directory. The dict returned by this method is automatically available in our template, so we can use it to create dynamic content in the final web page that will be sent out to the browser.
The next thing we need to do is to make a copy of welcome.kid and call it hello.kid. Then you can open this file in your favorite text editor and add this line directly below
<h1>Hello World<h1>: <span py:replace="greeting">message from the controller goes here</span>
Save this file, and you are ready to go to your new hello method, which will create a dynamic message from the controller at http://localhost:8080/hello. You don't even have to restart your TurboGears application, if you're in development mode. It watches to see whether anything changed and automatically restarts, so you can make a change and then test it right away in your browser.
The displayed page should have both the Hello World heading, which we saw in Section 2.3, and the new message we passed in from the controller. This case isn't all that interesting because we just passed in a simple string, but you can use this technique to create much more complex web pages with simple and easy-to-understand templates.
You can change this message at any time in your controller file, along with the layout in hello.kid. Then when you click reload on the Hello page, the controller and the template automatically reload in your application, enabling you to see the effects of your change right away.
That's it, your first TurboGears application (see Figure 2.3).
Figure 2.3. Hello from the template