In the previous page we created a new Revel application
called myapp. On this page we look at how Revel handles the HTTP request
http://localhost:9000/ resulting in the welcome message.
The first thing that Revel does is check the
conf/routes file (see routing):
GET / App.Index
This tells Revel to invoke the
Index method of the
controller when it receives a http
GET request to
Let’s follow this call to the code, in app/controllers/app.go:
Templates are in the app/views directory. When an explicit template name is not specified, Revel looks for a template matching the action. In this case, Revel finds the app/views/App/Index.html file, and renders it as a Go template.
Beyond the functions provided by the Go templates package, Revel adds a few helpful ones also.
The template above : -
- Adds a new title variable to the render context with set.
- Includes the header.html template, which uses the title variable.
- Displays a welcome message.
- Includes the flash.html template, which shows any flashed messages.
- Includes the footer.html.
If you look at header.html, you can see some more template tags in action:
You can see the set
.title being used, and also see that it accepts JS and CSS
files included from calling templates in the moreStyles and moreScripts
watchers that check for changes to files and recompiles as part of the development cycle.
To demonstrate this, change the welcome message. In Index.html, change
Refresh the browser, and you should see the change immediately! Revel noticed that your template changed and reloaded it.
Revel watches - see config):
- All go code under app/
- All templates under app/views/
- The routes file: conf/routes
Changes to any of those will cause Revel to update and compile the running app with the latest change in code. Try it right now: open app/controllers/app.go and introduce an error.
Refresh the page and Revel will display a helpful error message:
Lastly, let’s pass some data into the template.
In app/controllers/app.go, change:
And in the app/views/App/Index.html template, change:
Refresh the browser and to see a Hawaiian greeting.