Plinth.Templating 1.5.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
.NET 6.0
dotnet add package Plinth.Templating --version 1.5.0
NuGet\Install-Package Plinth.Templating -Version 1.5.0
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="Plinth.Templating" Version="1.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Plinth.Templating --version 1.5.0
#r "nuget: Plinth.Templating, 1.5.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Plinth.Templating as a Cake Addin
#addin nuget:?package=Plinth.Templating&version=1.5.0

// Install Plinth.Templating as a Cake Tool
#tool nuget:?package=Plinth.Templating&version=1.5.0

README

Plinth.Templating

HTML Templating library for generating emails and documents

1. Create an assembly with your cshtml templates in it, and some models

Directory structure example for a template assembly:

Templates/
	Email1.cshtml
	Email2.cshtml
	Email3.cshtml
Layouts/
	DefaultEmail.cshtml
Shared/
	ButtonLink.cshtml
Models/
	EmailModel.cs

2. Initialize the engine

    var engine = new TemplateEngineBuilder()
        .AddTemplateAssembly(typeof(EmailModel).Assembly, new[] { "Layouts", "Shared", "Templates" })
        .PrecompileInBackground() // optional
        .Build();

👉 the paths given are optional. If none are given then you must specify the full path from the assembly root         Example, with no paths, specify "Templates/Email1"         with "Templates" specified, "Email1" will work as well 👉 This goes for references in your code to templates, as well as Layouts, Partials, and IncludeAsync's 👉 The root of the assembly is always included

3. Set up templates

⚠️ NOTE! NOTE! NOTE! 👉 Make sure your cshtml files are set to "Embedded Resource" in your .csproj

@using MyAssembly.Models							// using directives work just like c#

@inherits Plinth.Templating.Template<EmailModel>    // do this to enable Include functionality
@inherits Plinth.Templating.Template		        // if not using a model

@model EmailModel									// set up your model

@{ Layout = "DefaultEmail"; }						// optionally, set up a Layout

@{ await IncludeAsync("ButtonLink", Model.Link); }	// example of including another template

<p>@Model.Title</p>									// example of using the model
<p>@ViewBag.Subtitle</p>							// example of using the view bag

@functions {										// static functions
    public static int Double(int x) => x * 2;
}

@{													// local functions (can access model and viewbag)
    int Double() => Model.Number * 2;
}

4. Render your templates

// no model
var result = await engine.RenderAsync("Email1");

// with model
var emailModel = new EmailModel { ... }
var result = await engine.RenderAsync("Email1", emailModel);

// with viewbag
dynamic viewbag = new ExpandoObject();
viewbag.Field1 = 7;
var result = await engine.RenderWithViewBagAsync("Email1", viewbag);

// with model and viewbag
var emailModel = new EmailModel { ... }
dynamic viewbag = new ExpandoObject();
viewbag.Field1 = 7;
var result = await engine.RenderWithViewBagAsync("Email1", emailModel, viewbag);

5. Rendering dynamic templates from strings

You can render a string template and cache it for later use, or for one time use

👉 These following are cached by "TemplateKey1", and re-rendering with that same key will ignore the string template subsequent renders

// no model
var result = await engine.RenderRawAsync("TemplateKey1", "<div>Hello</div>");

// with model
var emailModel = new EmailModel { ... }
var result = await engine.RenderRawAsync("TemplateKey1", 
	"<div>@Model.Name</div>", emailModel);

// with viewbag
dynamic viewbag = new ExpandoObject();
viewbag.Field1 = 7;
var result = await engine.RenderRawWithViewBagAsync("TemplateKey1",
	"<div>@ViewBag.Field1</div>", viewbag);

// with model and viewbag
var emailModel = new EmailModel { ... }
dynamic viewbag = new ExpandoObject();
viewbag.Field1 = 7;
var result = await engine.RenderRawWithViewBagAsync("TemplateKey1", 
	"<div>@Model.Name, @ViewBag.Field1</div>", emailModel, viewbag);

👉 The following are one-offs and will re-compile every time

// no model
var result = await engine.RenderRawOnceAsync("<div>Hello</div>");

// with model
var emailModel = new EmailModel { ... }
var result = await engine.RenderRawOnceAsync("<div>@Model.Name</div>", emailModel);

// with viewbag
dynamic viewbag = new ExpandoObject();
viewbag.Field1 = 7;
var result = await engine.RenderRawOnceWithViewBagAsync(
	"<div>@ViewBag.Field1</div>", viewbag);

// with model and viewbag
var emailModel = new EmailModel { ... }
dynamic viewbag = new ExpandoObject();
viewbag.Field1 = 7;
var result = await engine.RenderRawOnceWithViewBagAsync( 
	"<div>@Model.Name, @ViewBag.Field1</div>", emailModel, viewbag);
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
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
1.5.0 72 11/9/2022
1.5.0-b88.7a7c20cd 33 11/9/2022
1.4.7 138 10/20/2022
1.4.6 215 10/17/2022
1.4.5 226 10/1/2022
1.4.4 165 8/16/2022
1.4.3 146 8/2/2022
1.4.2 173 7/19/2022
1.4.2-b80.7fdbfd04 78 7/19/2022
1.4.2-b74.acaf86f5 57 6/15/2022
1.4.1 185 6/13/2022
1.4.0 161 6/6/2022
1.3.8 563 4/12/2022
1.3.7 181 3/21/2022
1.3.6 178 3/17/2022
1.3.6-b67.ca5053f3 67 3/16/2022
1.3.6-b66.4a9683e6 65 3/16/2022
1.3.5 185 2/23/2022
1.3.4 493 1/20/2022
1.3.3 177 12/29/2021
1.3.2 316 12/11/2021
1.3.1 197 11/12/2021
1.3.0 184 11/8/2021
1.2.3 1,336 9/22/2021
1.2.2 564 8/20/2021
1.2.1 576 8/5/2021
1.2.0 343 8/1/2021
1.2.0-b37.a54030b9 106 6/24/2021
1.1.6 1,496 3/22/2021
1.1.5 229 3/9/2021
1.1.4 328 2/27/2021
1.1.3 251 2/17/2021
1.1.2 221 2/12/2021
1.1.1 563 2/1/2021
1.1.0 365 12/16/2020
1.1.0-b27.b66c309b 234 11/15/2020
1.0.12 667 10/18/2020
1.0.11 326 10/6/2020
1.0.10 304 9/30/2020
1.0.9 296 9/29/2020
1.0.8 486 9/26/2020
1.0.7 461 9/19/2020
1.0.6 357 9/3/2020
1.0.5 596 9/2/2020
1.0.4 537 9/1/2020
1.0.3 385 9/1/2020
1.0.2 377 8/29/2020
1.0.1 375 8/29/2020
1.0.0 375 8/29/2020
1.0.0-b1.c22f563d 200 8/28/2020

net7.0 support and removed netcoreapp3.1