-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting base type and setting namespaces #46
Comments
HTML Helpers are not supported by RazorLight at the moment. @using, however, should work for specifying a namespaces |
I have my own type FFHtmlHelper which I want to expose as a property Does it just use whatever assemblies are in the current appdomain, or do I need to specifically inject assemblies? |
Yep, inheritance is supported. Create a class: public class BasePage<T> : TemplatePage<T>
{
public override Task ExecuteAsync()
{
return Task.FromResult(0);
}
} In your view:
Replace dynamic with your model type, or leave it as it is. About assemblies: |
Is there a way to set some properties on the |
Yep, here is an example of code I use at RazorLight.MVC to inject properties values before rendering a page: private static void AddEngineRenderCallbacks(IRazorLightEngine engine, IServiceProvider services)
{
var injector = services.GetRequiredService<PropertyInjector>();
engine.Configuration.PreRenderCallbacks.Add(template => injector.Inject(template));
} Add your custom PreRenderCallback to EngineConfiguration |
Is everything clear @mcintyre321? |
Not really, but I haven't had a chance to try it out yet! I'm imagining I'm going to have to do something along the lines of:
Is that about right? |
Part with injector is not correct. Is was just an example of how I use PreRenderCallback to fill in dependencies in templates. Here is more complete example: var configuration = EngineConfiguration.Default;
configuration.PreRenderCallbacks.Add(templatePage =>
{
var page = (YourBaseType)templatePage;
page.Html = /* Assign your property here */,
page.MyOtherProperty = "Same here";
});
var engine = EngineFactory.CreatePhysical("C:\\", configuration);
/* Use engine then */ |
Is it performant to new up a new engine each time, or should it be cached? I may have different values for |
Yea, that might be a problem. First solution that comes to my mind - is to add "Remove" method to PreRenderActionList, so you can replace a callback before each render |
Special for you, I added an additional overload to "Parse" method, which accepts Here is a full example: Code: string result = engine.Parse("file.cshtml", model: 1337, viewBag: null, prerenderCallback: new Action<TemplatePage>((t) =>
{
var page = (BasePage<int>)t;
page.Property = 1337;
})); View: @inherits RazorLight.Sandbox.BasePage<int>
<html>
@Property
</html> BasePage.cs public class BasePage<T> : TemplatePage<T>
{
public int Property { get; set; }
public override Task ExecuteAsync()
{
return Task.FromResult(1);
}
} Rendered result: <html>
1337
</html> |
Let me know if it works for you, so I can finally close this issue :) |
Regrading namespaces, I see there is a collection for namespaces. Can I just add the namespaces there? If so, how? I added them to that collection and it still didnt work. The razor I have uses the namespaces in the web.config, so they are not listed in the actual html. |
Thanks for the new feature! I'm still trying to get this to work, have discovered NUnit doesn't work in netcore1.1 which is slowing me down a little... |
Still trying to get a custom base type to work...
but the template always compiles with *which inherits TemplatePage |
Example that I provided works, I tested it. You cast TemplatePage to your template base type and it works. You don't need custom PageFactory |
I see, I had to use an explicit |
Hi again. I'm trying to replace RazorEngine with RazorLight in my FormFactory project, but am getting some errors when I run my templates.
My templates are very similar to MVC views (in fact they are MVC views when I run them inside MVC, but I read them and change them when running as standalone templates), e.g. one might look like this:
So I have
using
statements, and am using@model
andModel
and alsoHtml
.I got this to work with RazorEngine by making the template inherit from a base type with
Html
andModel
on it. How can I set the base type with RazorEngine? It looks likeModel
is set from the.Parse(..., model)
but that won't work forHtml
-Also I'm getting errors like
- The type or namespace name 'DataAnnotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?)
. Is there a way to tell RazorEngine what assemblies to use?- caused by DataAnnotations having a different namespace in 451/standardthanks!
The text was updated successfully, but these errors were encountered: