volkovku / RazorTemplates

Open source templating engine based on Microsoft's Razor parsing engine. Thread safe. Allows run Razor templates outside ASP.Net MVC Projects.
Microsoft Public License
99 stars 32 forks source link

Not able to use assembly .AddNamespace #14

Closed JasonFoglia closed 10 years ago

JasonFoglia commented 10 years ago

Html5 is a separate project.

var template = Template.WithBaseType() .AddNamespace("Html5") .Compile("@Html5.Tag.Create(\"div\", \"test\")"); // Fails

error CS0246: The type or namespace name 'Html5' could not be found (are you missing a using directive or an assembly reference?)

There is a reference to the project and there is a using directive at the top of the code.

JasonFoglia commented 10 years ago

I figured this out, but I wondering if it has to work this way.

Basically, I had to create or not a dummy class in Html5 project, then created an instance of this class before running the Compile code on Template.

var dummy = new Html5.Dummy();

var template = Template.WithBaseType() .AddNamespace("Html5") .Compile("@Html5.Tag.Create(\"div\", \"test\")"); // Worked!

volkovku commented 10 years ago

Jason, thank you. I will investigate this strange behavior.

JasonFoglia commented 10 years ago

Volkovku, the behavior seems to be expected or as designed. Your method GetLoadedAssemblies() says it all. I wonder tho, if I am referencing Html5 in my project is there away to reference the assemble in RazorTemplates, even if it's not loading into the current domain? Oh and thank you for your work! Great stuff!

Eibwen commented 9 years ago

Here is how I ended up doing it so far. Yeah its definitely the GetLoadedAssemblies(), I don't think there is a way to improve it, since .Net removes assembly references it thinks are not being used. You'd have to get the referenced assemblies before its gone through the compiler...

var useful1 = typeof(HtmlString);
var outputBody = _templateService.Parse<T>(outputTemplate, model, null, typeof(T).Name);
JasonFoglia commented 9 years ago

@Eibwen I'm glad you got it working

lvermeulen commented 8 years ago

@Jason54 @Eibwen @volkovku I needed this and it was actually pretty easy to fix. If anyone is still interested, I can add a PR although the project no longer seems active.

volkovku commented 8 years ago

@lvermeulen thanks for you interest. Unfortunately in last years I have no time to support this project. But I can review and merge you pull request.

lvermeulen commented 8 years ago

@volkovku That's great! PR submitted.