Including System Namespace by Default

Dec 28, 2010 at 6:26 PM
Edited Dec 28, 2010 at 6:27 PM


you are doing really great job with this project, thanks a lot!

I am experimenting little bit with it and using it for Mail Merging purposes and so far i have only 2 rather small issues.

1) Would it be possible to include System Namespace by Default into the Engine or somehow add it programmatically before calling Razor.Parse()?

For Example using template like this "@String.Format("{0:dd.MM.yyyy}", DateTime.Now)" would produce erros like "The name 'String' does not exist in the current context" or "The name 'DateTime' does not exist in the current context".

Just right now i had an idea and got around it by rolling out my own Template Class and marking it with "RequireNamespacesAttribute", would that be preferable way?

2) Is Razor Template parsing an "All or Nothing" operation and if yes is there a way around it?

string template = "Hello @Model.Name1 @Model.Name2 Welcome to Razor!";
string result = Razor.Parse(template, new { Name1 = "John" , Name3 = "John"});

This would fail completely because Property "Name2" doesn't exist in model. Would it be possible to force parsing anyways so it resolves all other known properties? The output would be

"Hello John @Model.Name2 Welcome to Razor!"

Thanks and keep up the great work, can't wait for 1.2 ;)


Dec 28, 2010 at 11:18 PM


We should be able to include other namespaces by default, will look to include System, System.Collections.Generic and System.Linq as the standard namespace imports for all templates.  The next check-in will include that.

As for your second question, its a little more difficult to do this, as the Razor parser generates a class which is then compiled.  The compiler will fail to compile because of missing properties on your strongly typed models.  I'm not even sure dynamic would help in this instance as you'd need to re-think how the Model is being provided I think. Will have a think :)

Dec 29, 2010 at 3:09 AM

Antaris, Do you think we should go the route of MVC razor and just add some app/web.config section that could handle stuff like additional namespaces - in addition to a possible straight code method?

Dec 29, 2010 at 9:15 AM

That's not a bad idea at all! In fact expanding from that, you could introduce a Configuration mechanism to pre-build and configure instances of the TemplateService for later consumption.