Template engine based on Microsoft's Razor parsing engine for .NET Core
APACHE-2.0 License
Bot releases are visible (Hide)
Published by toddams over 7 years ago
Published by toddams over 7 years ago
Published by toddams over 7 years ago
Published by toddams over 7 years ago
Published by toddams about 8 years ago
This release comes out with some improvements and refactorings. We are planning to roll up a stable version soon
ParseString()
is extracted to an extension method (under RazorLight.Extensions namespace)IEngineConfiguration
InvalidOperationException
- RazorLightException
is thrownAggregateExceptions
. I'm erasing annoying AggregateExceptions from code. Please, open an issue If you see it again somewhereNew package brings ASP.NET MVC Core integration which allows you to inject services into your templates via built in Dependency Injection container
Add package
Install-Package RazorLight.MVC
Add RazorLight services in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
....
services.AddRazorLight("/Views"); // <- This one
....
}
private readonly IRazorLightEngine engine;
public HomeController(IRazorLightEngine engine)
{
this.engine = engine;
}
@inject MyProject.TestViewModel myModel
@Include now works in Layout and ViewStart pages as well (#12)
This release brings you Include feature
Example
@model MyProject.TestViewModel
<div>
Hello @Model.Title
</div>
@{ await IncludeAsync("SomeView.cshtml", Model); }
First argument takes a key of the template to resolve, second argument is a model of the view (can be null)
Raw()
method to output model value without encoding ( #7 )IMemoryCache
from DefaultPageLookup
CachingPageFactory
IEngineConfiguration config = EngineConfiguration.Default;
config.Namespaces.Add("My.Custom.Namespace");
var engine = EngineFactory.CreatePhysical("root/folder", config);
Note: Viewbag is accessible both from the template page and it's layout.
Code
var model = new MyModel()
{
Title = "Test"
};
//create a dynamic viewbag
dynamic viewBag = new ExpandoObject();
viewBag.DataForLayoutPage = "Hello world";
string result = engine.Parse("key", model, viewBag);
Template
@model MyViewModel
@{
Layout = "layout_key";
}
<div>Hello @ViewBag.Title</div>
This release brings Layout, _Viewstart pages support, sections and custom template namespaces. API is completely reworked.
ITemplateManager(string key)
to resolve a templates. For files, Key - is a relative path to the view. For embeded resources - it's a name of the resourse.@model MyModel
@{
Layout = "_Layout.cshtml"; // <- This is a key of the layout page
}
Layout.cshtml:
@RenderSection("section-name", required : false)
View.cshtml:
@section section-name
{
<div>It is a content inside a section</div>
}
ITemplateConfiguration.Namespaces (ISet<string)
Before:
var config = new ConfigurationOptions() { ViewsFolder = "C:/path/to/folder" }
var engine = new RazorLightEngine(config);
string result = engine.ParseFile("Test.cshtml", model);
After:
var engine = EngineFactory.CreatePhysical("C:/path/to/folder");
string result = engine.Parse("Test.cshtml", model);
Published by toddams about 8 years ago
First release with initial features: