Cutlass 0.0.3
dotnet add package Cutlass --version 0.0.3
NuGet\Install-Package Cutlass -Version 0.0.3
<PackageReference Include="Cutlass" Version="0.0.3" />
<PackageVersion Include="Cutlass" Version="0.0.3" />
<PackageReference Include="Cutlass" />
paket add Cutlass --version 0.0.3
#r "nuget: Cutlass, 0.0.3"
#addin nuget:?package=Cutlass&version=0.0.3
#tool nuget:?package=Cutlass&version=0.0.3
Cutlass (a different kind of "Razor")
A small library built to parse Razor code, with a view of implementing templates using Laravel Blade-like directives. As such, directives used in ASP.net Razor, such as @inject, @html, @raw, @inherits, etc are not implemented, neither will they be.<br><br> This library as with most IpelaTech libraries is meant to serve as a starting point for more custom use-cases.
Features
- Cross platform (dotnet 5)
- Pass in template string for conversion
- Ability to create Custom Directives
Installation
- Package Manager
Install-Package Cutlass
- CLI
dotnet add package Cutlass
- Package Reference
#in package file
<PackageReference Include="Cutlass" Version="0.0.1" />
#in console
dotnet restore
Usage/Examples
- Simplest example
string res = Cutlass.Core.Razor.Converters.StringTemplateToHtmlConverter.convert("Hello World");
Output:<br> Hello World
- Use with model
public class TestModel : Cutlass.Core.Razor.IBaseModel
{
public string Name { get; set; } = "World";
}
--------
string res = Cutlass.Core.Razor.Converters.StringTemplateToHtmlConverter.convert(
"Hello @Model.Name",
new TestModel()
{
Name = "World"
}
);
- Use with anonymous model
public class TestModel : Cutlass.Core.Razor.IBaseModel
{
public string Name { get; set; } = "World";
}
--------
string res = Cutlass.Core.Razor.Converters.StringTemplateToHtmlConverter.convert(
"Hello @Model.Name",
new
{
Name = "World"
}
);
Output:<br> "Hello World"
- Use with custom View Component<br> A View Component is the code-behind the template. This is where you can implement your own custom directives. The library provides a base view compiler, IBaseViewComponent (namespace Cutlass.Core.Razor). To create a custom directive, subclass IBaseViewComponent and implement the directive as a function that returns string. It can also take arguments.
public class CustomViewComponent : Cutlass.Core.Razor.IBaseViewComponent {
public string directive() {
return "my directive";
}
public string directive_with_argument(string argument) {
return $"argument says: {argument}";
}
}
----
System.Text.StringBuilder sb = new System.Text.Stringbuilder();
sb.AppendLine("Hello @Model.Name");
sb.AppendLine(@"@directive()");
sb.AppendLine(@"@directive_with_argument(""hello"")");
string res = Cutlass.Core.Razor.Converters.StringTemplateToHtmlConverter.convert<CustomViewComponent>(
sb.ToString(),
new TestModel()
{
Name = "World"
}
);
Output:<br> Hello World<br> my directive<br> argument says: hello
Please see the Tests for other usages and an example of how you might implement layouts.
Acknowledgements
- Awesome Readme Templates
- This SO answer
- RazorEngineCore (Cutlass can be thought of as a stripped down version of RazorEngineCore that takes a slightly different direction)
License
Changelog
0.0.2
- Initial version
0.0.3
- Removed @extends as a standard directive
- Can use anonymous models instead of having to subclass IBaseModel each time
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net5.0
- Microsoft.AspNetCore.Razor (>= 2.2.0)
- Microsoft.AspNetCore.Razor.Language (>= 5.0.9)
- Microsoft.AspNetCore.Razor.Runtime (>= 2.2.0)
- Microsoft.CodeAnalysis (>= 3.11.0)
- Microsoft.CodeAnalysis.CSharp (>= 3.11.0)
- ObjectDumper.NET (>= 3.3.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.