Comments (17)
Yep, inheritance is supported.
Create a class:
public class BasePage<T> : TemplatePage<T>
{
public override Task ExecuteAsync()
{
return Task.FromResult(0);
}
}
In your view:
@inherits YourApplicationNamespace.BasePage<dynamic>
Replace dynamic with your model type, or leave it as it is.
About assemblies:
It takes dependencies from entryAssembly, so make sure that you have everything there.
from razorlight.
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 */
from razorlight.
HTML Helpers are not supported by RazorLight at the moment. @using, however, should work for specifying a namespaces
from razorlight.
I have my own type FFHtmlHelper which I want to expose as a property Html
on the views base type. Is there a way to set the base type?
Does it just use whatever assemblies are in the current appdomain, or do I need to specifically inject assemblies?
from razorlight.
Is there a way to set some properties on the MyBasePage
before executing the template?
from razorlight.
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
from razorlight.
Is everything clear @mcintyre321?
from razorlight.
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:
class MyPageTempalteType<T> : : TemplatePage<T>
{
public HtmlHelper Html {get;set;}
public override Task ExecuteAsync() => Task.FromResult(0);
}
...
var engine = EngineFactory.CreateEmbedded(typeof(MyPageTemplateType))
var injector = engine.services.GetRequiredService<PropertyInjector>();
engine.Configuration.PreRenderCallbacks.Add(template => {
((MyPageTemplateType) template).Html = GetHtmlHelper();
});
...
var model = new SomePageModel()
{
Title = "Hello, world",
Description = "Some text here"
};
string result = engine.Parse("SomePage", model);
Is that about right?
from razorlight.
Is it performant to new up a new engine each time, or should it be cached? I may have different values for MyOtherProperty
etc.
from razorlight.
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
from razorlight.
Special for you, I added an additional overload to "Parse" method, which accepts Action<TemplatePage>
, so you can add callbacks for each page individually, and not to rely on global PreRenderCallbacks of EngineConfiguration.
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>
from razorlight.
Let me know if it works for you, so I can finally close this issue :)
from razorlight.
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.
from razorlight.
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...
from razorlight.
Still trying to get a custom base type to work...
I tried setting the using a custom PageFactory (in a custom EngineFactory copied from the Embedded one):
IPageFactoryProvider pageFactory = new DefaultPageFactory((key) =>
{
ITemplateSource source = manager.Resolve(key);
string razorTemplate = core.GenerateRazorTemplate(source, new ModelTypeInfo(typeof(FormFactoryTemplateBase)));
var context = new CompilationContext(razorTemplate, configuration.Namespaces);
CompilationResult compilationResult = configuration.CompilerService.Compile(context);
return compilationResult;
});
but the template always compiles with TemplatePage<TModel>
not FormFactoryTemplateBase*
- is it because [BaseType] is a const, and isn't being set from my ModelTypeInfo?
*which inherits TemplatePage
from razorlight.
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
from razorlight.
I see, I had to use an explicit @inherits
statement (rather than setting the base type in the factory). It seems to be working now! thanks!
from razorlight.
Related Issues (20)
- Failed to find type or namespace name 'Dictionary'
- Re-loading of an updated "Include"/partial template section? HOT 12
- Project can not find template with key HOT 3
- The CompileRenderStringAsync still caching templates HOT 7
- Even if @ using System.Collections.Generic is used; Nor can. any() be used
- The type or namespace name 'Razor' does not exist in the namespace 'RazorLight' HOT 4
- Unable to use Linq extension methods HOT 1
- Inject page to @RenderBody programmatically HOT 3
- [Question] Pros of using Cache System HOT 1
- The library states it is .Net System 2.0 HOT 3
- How to use method 'IncludeAsync()' to import partial view in root view?
- Entity Framework Core and RazorLight : "An error occurred while accessing the Microsoft.Extensions.Hosting service"
- IncludeAsync but truncate if too long.
- AddDynamicTemplates for embedded resources HOT 1
- Exception : One or more section(s) have been ignored. Ignored section(s)
- Unable to use 'media' queries in html templates
- Make sure PreserveCompilationContext is set to true
- Update for .NET 8.0, consider removing deprecated .NET versions HOT 9
- Have more than one operatingAssembly ... or included assemblies HOT 1
- Azure functions V4 issue HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from razorlight.