Cutlass 0.0.3

dotnet add package Cutlass --version 0.0.3
NuGet\Install-Package Cutlass -Version 0.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cutlass" Version="0.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cutlass --version 0.0.3
#r "nuget: Cutlass, 0.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cutlass as a Cake Addin
#addin nuget:?package=Cutlass&version=0.0.3

// Install Cutlass as a Cake Tool
#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

License

MIT

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.3 339 9/17/2021
0.0.2 427 9/15/2021