This project is read-only.

TemplateManager.Render(...)

Nov 27, 2008 at 10:04 PM
Edited Nov 27, 2008 at 10:04 PM
Hi,

First of all, thanks a lot for that project. I think that can be really usefull and it look good!

I got a question about the Render method of the TemplateManager : it take 2 arguments, filename and arguments. How i can specifie the filename if the resource is embedded resource ?

There is the sample you put in the Tutorial5, it always crash on the second line and raize a FileNotFoundException  :

TemplateArguments arguments = new TemplateArguments(args);
s
tring pageTemplate = m_TemplateMng.Render("Views\\Logon\\Main.x", arguments);
arguments.Clear();
arguments.Add(
"text", pageTemplate);
return m_TemplateMng.Render("Views\\Layouts\\Application.x", arguments);

Thanks a lot !

Simon

 

 

 

 

Nov 28, 2008 at 9:08 AM
Hi Simon,
The TemplateManager doesn't actually read the files from the paths you specify directly but uses the path you insert and retrieves the files from each added ITemplateLoader (of which we've implemented two, the FileTemplateLoader and the ResourceTemplateLoader).
These loaders must be manually added to the TemplateManager upon creation:
new TemplateManager(new FileTemplateLoader(), new ResourceTemplateLoader)

Next up is to bind the resources you want available for rendering to the ResourceTemplateLoader (this have to be done manually since you might have resources you dont want available and moreover resources in strange namespaces supposed to be bound to other addresses). The binding is done to an url (since this is a webserver) and looks like this:
Assembly theAssemblyTheResourcesAreIn = Assembly.GetExecutingAssembly();
myResourceTemplateLoader.LoadTemplates("/toMyUrl/", theAssemblyTheResourcesAreIn, "The.Namespace.The.Resources.Are.in")

This will load all the resources in the specified namespace to the path "toMyUrl\\" and any resource in a child namespace to "toMyUrl\\subNamespace\\".

It might seem odd that we add the resources to url's and then you have to retrieve them using paths but we've made it as such since this will in most cases be used together with the ie ViewController in which case it is a bit more logical in the application to add resources to urls instead of paths (since the controllers work with urls).

Finally your example should then look like such:
ResourceTemplateLoader resources = new ResourceTemplateLoader();
resources.LoadTemplates("/views/logon/", Assembly.GetExecutingAssembly(), "YourProject.Views.Logon");
resources.LoadTemplates("/views/layouts/", Assembly.GetExecutingAssembly(), "YourProject.Views.Layouts");
TemplateManager manager = new TemplateManager(resources);

TemplateArguments arguments = new TemplateArguments(arguments);
string page = manager.Render("Views\\Logon\\Main.x", arguments);
arguments.Clear();
arguments.Add("text", pageTemplate);
return manager.Render("Views\\Layouts\\Application.x", arguments);

If you are using some kind of special format, don't forget to add a generator for it (since our default generator for haml wouldn't render any file ending with an .x).

Hope this cleared things up a bit for you, feel free to come again with any further questions.
Nov 28, 2008 at 8:13 PM
Hi, Thanks a lot, it work !!

I Got a second question, there is a method to put custom error message without change the project?

I want put my own 404/500 error page..

Thanks

Simon
Nov 28, 2008 at 8:24 PM
The easiest way is to inherit HttpServer and override the ErrorPage method.

I'll create an event for this later.