CrestApps.Core Templates
You are a CrestApps.Core expert. Generate code for CrestApps.Core.Templates discovery, Liquid rendering and composition.
Guidelines
CrestApps.Core.Templatesis standalone.services.AddTemplating(...)registersITemplateEngine(FluidTemplateEngine, singleton),ITemplateService(DefaultTemplateService, scoped), the MarkdownITemplateParser, and the code, file-system and prompt file-system providers. It also adds theinclude_promptLiquid filter.CrestApps.Core.AIalready callsAddTemplating()throughAddCoreAITemplating(), which adds AI template sources and the system-prompt profile template provider. Usecrestapps-core-ai-templatesfor that layer.ITemplateService.RenderAsync(id, arguments)renders a registered template;ITemplateEngine.RenderAsync(text, arguments)renders an in-memory Liquid string.FluidTemplateEngineruns on Fluid 3 and takesIOptions<Fluid.TemplateOptionsBuilder>. Templates can read every public member of the objects passed to them, so no type registration is needed; pass only data a template may read, especially when users write templates.- Add custom filters or member accessors on the builder:
builder.Services.Configure<Fluid.TemplateOptionsBuilder>(options => options
.ConfigureFilters(filters => filters.AddFilter("shout", (input, arguments, context) =>
Fluid.Values.StringValue.Create(input.ToStringValue(context).ToUpperInvariant()))));
Use ConfigureMemberAccess(strategy => ...) only for custom member accessors.
- Rendering trims each line and collapses runs of blank lines. A template that fails to parse is logged as a warning and returned unrendered; check templates with
TryValidatebefore saving them. - Template identifiers are case-insensitive. When two providers return the same
Id, the first one read wins. ITemplateService,ITemplateEngineandITemplateProvidermembers take an optionalCancellationToken; pass it through custom providers.
Registration
using CrestApps.Core.Templates.Extensions;
builder.Services
.AddTemplating(options => options
.AddDiscoveryPath(builder.Environment.ContentRootPath)
.AddTemplate(
"welcome",
"Hello {{ customer }}!",
metadata => metadata.Title = "Welcome"))
.AddTemplatesFromAssembly(typeof(Program).Assembly, source: "MyApp");
| Source | Template id | Kind |
|---|---|---|
| AddTemplate(id, content, ...) | the given id | set Template.Kind yourself |
| {discovery path}/Templates/*.md (not recursive) | file name without extension | from Kind front matter |
| {discovery path}/Templates/Prompts/*.md and one level of subfolders | file name; a subfolder name becomes FeatureId | SystemPrompt unless front matter sets it |
| Embedded resources under Templates/ via AddTemplatesFromAssembly(...) | path after Templates.; a Prompts. prefix is dropped | SystemPrompt for Prompts/, otherwise front matter |
Mark the Markdown files as EmbeddedResource in the .csproj for AddTemplatesFromAssembly(...).
Template File Format
---
Title: Order Follow-up
Description: Asks a customer to confirm a delivery window.
Category: Support
IsListable: true
Kind: SystemPrompt
Parameters:
- customer_name: The customer's display name
- order_number: The order being discussed
---
You are writing to {{ customer_name | default: "the customer" }} about order {{ order_number }}.
{% if delivery_window %}
Offer the delivery window {{ delivery_window }}.
{% endif %}
Title, Description, Category, IsListable (default true) and Parameters map to TemplateMetadata; Kind maps to Template.Kind; any other key lands in TemplateMetadata.AdditionalProperties. A missing Title is derived from the id.
Render and Compose
using CrestApps.Core.Templates;
using CrestApps.Core.Templates.Services;
namespace MyApp.Messaging;
public sealed class FollowUpMessageService
{
private readonly ITemplateService _templates;
public FollowUpMessageService(ITemplateService templates)
{
_templates = templates;
}
public Task<string> RenderAsync(string customer, string order, CancellationToken cancellationToken = default)
{
return new TemplateBuilder()
.WithSeparator("\n\n")
.Append("Keep the reply under 120 words.")
.AppendTemplate("order-follow-up", new Dictionary<string, object>
{
["customer_name"] = customer,
["order_number"] = order,
})
.BuildAsync(_templates, cancellationToken);
}
}
MergeAsync(ids, arguments, separator)renders several templates with the same arguments and skips any that render tonull.GetByKindAsync(kind)(inTemplateServiceExtensions) lists templates of one kind, for exampleSystemPrompt.RenderAsyncthrowsKeyNotFoundExceptionfor an unknown id.- Inside a template,
{{ "safety-rules" | include_prompt }}renders another template without arguments, and{% render_ai_template "safety-rules" %}renders it in a child scope that sees the caller's variables. Both guard against recursion:include_promptreturns nothing for a template that is already rendering, andrender_ai_templatestops at a depth of ten.
Store Templates in a Database
A provider whose templates change at runtime implements IDynamicTemplateProvider (a marker over ITemplateProvider). DefaultTemplateService reads static providers first, so a stored template never replaces a shipped template with the same id. Use Aliases to keep old identifiers resolvable; GetAsync matches an id first and an alias second.
using CrestApps.Core.Services;
using CrestApps.Core.Templates.Models;
using CrestApps.Core.Templates.Parsing;
using CrestApps.Core.Templates.Providers;
namespace MyApp.Templates;
public sealed class StoredTemplateProvider : IDynamicTemplateProvider
{
private readonly ICatalog<StoredTemplate> _catalog;
private readonly ITemplateParser _parser;
public StoredTemplateProvider(ICatalog<StoredTemplate> catalog, IEnumerable<ITemplateParser> parsers)
{
_catalog = catalog;
_parser = parsers.First(parser => parser.SupportedExtensions.Contains(".md"));
}
public async Task<IReadOnlyList<Template>> GetTemplatesAsync(CancellationToken cancellationToken = default)
{
var records = await _catalog.GetAllAsync(cancellationToken);
return records
.Select(record =>
{
var parsed = _parser.Parse(record.Markdown);
return new Template
{
Id = record.Name,
Aliases = [$"stored:{record.ItemId}"],
Metadata = parsed.Metadata,
Content = parsed.Body,
Source = "Database",
};
})
.ToArray();
}
}
builder.Services.AddScoped<ITemplateProvider, StoredTemplateProvider>();
StoredTemplate is any CatalogItem with Name and Markdown properties registered through a store package. Register dynamic providers as scoped, since ITemplateService is scoped and reads them in every scope. A custom template service that caches for longer than a scope should override GetStaticTemplatesAsync and cache only what it returns.
Upgrading Within 2.0 Previews
- Fluid 3.
Fluid.TemplateOptionsis immutable with no public constructor, soservices.Configure<Fluid.TemplateOptions>(...)no longer has any effect. Configure filters and accessors onFluid.TemplateOptionsBuilderwithConfigureFiltersandConfigureMemberAccess. - Remove
MemberAccessStrategy.Register<MyType>()calls: the one-argument overload is gone and registration is no longer needed. - Custom filters and tags written against Fluid 2 need Fluid 3 calls such as
FluidValue.ToStringValue(context)andStringValue.Create(...).
Upgrading from 1.x
- New:
IDynamicTemplateProvider,Template.Aliases, andDefaultTemplateService.GetStaticTemplatesAsync. ImplementIDynamicTemplateProvideron providers backed by editable storage so edits apply on the next request. - The system-prompt AI profile template provider moved from the sample hosts into
CrestApps.Core.AIand is registered byAddCoreAITemplating(); template ids are now the template name, with the oldai-profile:ids kept as aliases.