Modules are packages that can be plugged into an application. They allow sharing of controllers, views, assets, and other code between multiple Revel applications or from third-party sources.
A module should have the same layout as a Revel application’s layout. The “hosting” application will merge it in as follows:
- Any templates in
module/app/views
will be added to the Template Loader search path - Any controllers in
module/app/controllers
will be treated as if they were in your application - The assets are made available, via a route action of the form
Static.ServeModule("modulename","public")
- Routes can be included in your application with the route line of
module:modulename
- see routing
Revel comes with some built in modules such as testing and jobs.
Enabling a module
In order to add a module to your app, add a line to conf/app.conf
:
You can add an ordering method by insert a sortable string in between the word module and the name of the module like the following. This allows you to specify which order the views should be loaded, so your application will always be the first, then the order of the modules and finally the default views in Revel.
An empty import path disables the module:
For example, to enable the test runner module:
Routing a module
- See the modules config in
app/routes
Building a module
Best Practices
It’s a good idea to make sure all your views are in their own namespace
so that they will not conflict with other applications that your module is
imported into. If you have a module called Reservation, it would be a good
idea to have your view path look like app/views/Reservation/Controller/Action.html
. This means that your controller needs to call RenderTemplate
instead of Render
so that the correct view path is chosen.
Module Initialization
The revel.RegisterModuleInit(func(*revel.Module))
can be for a module to be called immediately after all the modules
are loaded. The revel.Module
contained in the callback is the one assigned for that module. This can
be used for a variety of purposes.
An example of a purpose would be to retrieve the logger initialized for the module like the following
var moduleLogger logger.MultiLogger
func init() {
revel.RegisterModuleInit(func(module *revel.Module){
moduleLogger = module.Log
moduleLogger.Debug("Assigned Logger")
})
}
The callback
registered in the revel.RegisterModuleInit
is called before the revel.OnAppStart
functions are.