Rocketmakers.Templates.Http 0.2.2

dotnet add package Rocketmakers.Templates.Http --version 0.2.2                
NuGet\Install-Package Rocketmakers.Templates.Http -Version 0.2.2                
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="Rocketmakers.Templates.Http" Version="0.2.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rocketmakers.Templates.Http --version 0.2.2                
#r "nuget: Rocketmakers.Templates.Http, 0.2.2"                
#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 Rocketmakers.Templates.Http as a Cake Addin
#addin nuget:?package=Rocketmakers.Templates.Http&version=0.2.2

// Install Rocketmakers.Templates.Http as a Cake Tool
#tool nuget:?package=Rocketmakers.Templates.Http&version=0.2.2                

Rocketmakers.Templates

A library for rendering content using templates from an external source and including dynamic data.

Packages

Rocketmakers.Templates

Nuget

This is the core library used by implementations that includes core types and interfaces.

Rocketmakers.Templates.Http

Nuget

This library contains an implementation for ITemplateService, whose purpose is to obtain the templates from a Http source. An in memory cache is also provided to support caching template content to avoid over-fetching.

Rocketmakers.Templates.Handlebars

Nuget

This library contains an implemention for ITemplateRenderer, providing the ability to render templates using Handlerbars.JS.

Usage

Prerequisites

This assumes you have setup and configured your template repo.

Setup

The first thing you'll want to do is configure your TemplateService. For templates stored at a remote source, we'll be using Rocketmakers.Templates.Http.

This can be setup using the following snippet.

// Initialise the template service
var templateService = new HttpTemplateService(
    repository: new AnonTemplateRepository(
        new HttpClient { BaseAddress = "https://gitlab.com/my-org/my-templates/-/raw/develop/" }
    ),
    cache: new InMemoryTemplateContentCache(
        new InMemoryTemplateContentCacheConfig(
            UseCache: true,
            CacheExpirySeconds: 3600
        )
    )
);

If you are utilising the default .NET dependency injection, then you can set it up as follows

// Inject service into Microsoft.Extensions.DependencyInjection.IServiceCollection
services.AddHttpTemplateService(
    repositoryUrl: new Uri("https://gitlab.com/my-org/my-templates/-/raw/develop/"),
    cacheConfig: new InMemoryTemplateContentCacheConfig(
        UseCache: true,
        CacheExpirySeconds: 3600
    )
);

You'll also want to register your ITemplateRenderer implementation. In our example, we'll be using Rocketmakers.Templates.Handlebars for processing handlerbars based templates.

services.AddSingleton<ITemplateRenderer, HandleBarsTemplateRenderer>();

Retrieving templates

Retrieving templates is done in the following way. You will repeat the following process for each service whose templates you want to setup.

// Maps to 'myService.json' in the source containing your templates. This is what contains the
// metadata describing the location of the layouts and partials comprising your templates
var serviceName = "myService";

// Get the latest templates for your service
TemplateGroup templateGroup = await templateService.GetTemplatesAsync(serviceName);

// Update the renderer with the latest templates
await renderer.UpdateTemplateGroupAsync(templateGroup);

There are two ways you can setup the above, and which one you use will depend on where you want to compromise.

  1. You can update the templates on bootstrap of your application. This means that rendering templates will be quick as they won't need to be downloaded and compiled. However startup time of your application will be slower. This will be benefical for services that are active for a long time, but not very handy for services that scale to zero or you need to start up quickly.

  2. You can update the templates everytime you render them. This means that startup time of your application wont be compromised, but requests that require content to be rendered will be slower.

Rendering

You can then render your template(s) with your dynamic data, as follows

// Render the template with the supplied payload of parameters
IReadOnlyCollection<RenderedTemplate> rendered = await renderer.RenderAsync(
    layoutId: "resetPassword",
    payload: new { username = "Alan", returnUrl = "https://rocketmakers.com" });

Your layoutId must match a layout defined in your specified service.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.2.2 2,877 11/4/2024
0.2.1 14,614 5/5/2023
0.2.0 5,301 1/5/2023
0.1.0 314 11/23/2022