HowTo: Load templates from resources

Topics: faq
Coordinator
Aug 10, 2008 at 11:43 AM
From change set 15004 it's possible to load templates from any source. There are two built in sources: Harddrive and embedded resources.

The source is specified by adding a TemplateLoader to the constructor of TemplateManager:
{{
  TemplateManager mgr = new TemplateManager(ResourceTemplateLoader(2));
}}

The resource template loader scans all loaded assemblies after the specified resource. All resource names are prefixed with a namespace, and to be able to use for instance "user\show.haml" as name in your controllers, you have to specify the number of levels to remove from the namespace prefix.

For instance, I have a project called AdminWeb, in which I have a folder called "views", and a subfolder corresponding to each controller ("user" for instance). This means that that the resource name of the show template is "AdminWeb.views.user.show.haml". That's not ok since one should be able to switch template loaders without having to change the calling code. The solution is to specify the number of namespace levels to remove from the path, in this case 2 (AdminWeb.views). The result is that I now can call Render("show.haml") as usual (the ViewController automatically adds the controller name).