Lyo.Formatter
1.0.0
See the version list below for details.
dotnet add package Lyo.Formatter --version 1.0.0
NuGet\Install-Package Lyo.Formatter -Version 1.0.0
<PackageReference Include="Lyo.Formatter" Version="1.0.0" />
<PackageVersion Include="Lyo.Formatter" Version="1.0.0" />
<PackageReference Include="Lyo.Formatter" />
paket add Lyo.Formatter --version 1.0.0
#r "nuget: Lyo.Formatter, 1.0.0"
#:package Lyo.Formatter@1.0.0
#addin nuget:?package=Lyo.Formatter&version=1.0.0
#tool nuget:?package=Lyo.Formatter&version=1.0.0
Lyo.Formatter
SmartFormat.NET-backed templating for user-defined strings: named placeholders, lists, pluralization, and culture-aware formatting. Designed for validation + formatting pipelines (for example IFormatterService with Lyo.Api computed fields and Lyo.Web.Automation step templates).
Examples
Register services
using Lyo.Formatter;
using Microsoft.Extensions.DependencyInjection;
services.AddFormatterService();
// Or: services.AddFormatterService(sp => /* custom SmartFormatter */);
ITemplate workflow
var t = formatter.CreateTemplate("{Title} — {Count}")
.WithValue("Title", doc.Title)
.WithValue("Count", doc.Count);
if (!t.TryValidate(out var err))
throw new InvalidOperationException(err);
if (!t.TryValidateContext(out var ctxErr))
throw new InvalidOperationException(ctxErr);
var text = t.Format();
When to use this package
- Turn stored templates (
"{User.Name} — {Order.Total:C}") into final text with one or more context objects. - Validate templates before persisting them (
ValidateTemplate,TryValidateTemplate). - Discover placeholders for dependency analysis (
GetPlaceholders,GetUnresolvedPlaceholders,AllPlaceholdersResolved). - Build context fluently with
IContextBuilder(dates, conditional keys, custom formatters).
Registration
Register FormatterService as singleton and expose IFormatterService. Prefer the overload with a factory when you need extra SmartFormat extensions or custom * *SmartSettings**.
Core types
| Type | Role |
|---|---|
IFormatterService |
Format, validate, inspect placeholders, wrap templates as ITemplate. |
FormatterService |
Default implementation: FormatErrorAction.MaintainTokens so missing data leaves {tokens} in output (enables unresolved-placeholder detection). Case-insensitive placeholder matching. |
ITemplate |
Parse-once style workflow: WithContext, AddContext, TryValidateContext, then Format(). |
IContextBuilder |
Fluent dictionary builder passed to Format(template, configure). |
Formatting overloads
Format(template, object? context)— single DTO, anonymous object, or any type SmartFormat can reflect over.Format(template, params object?[] contextItems)— multiple sources; later objects win on duplicate names.Format(template, IReadOnlyDictionary<string, object?>)— explicit name/value map.Format(template, Action<IContextBuilder>)— build the map withAdd,AddIf,AddWhen, typed format strings, or customFunc<,>formatters.
Validation and placeholders
ValidateTemplate/TryValidateTemplate— parser pass; catches syntax errors before you save a template.TryFormat— swallows exceptions from SmartFormat and returns false (use sparingly; prefer validation + known context).GetPlaceholders— lightweight regex-based names (first segment of each{...}); good for UI hints andentityTypes-style dependency lists.AllPlaceholdersResolved/GetUnresolvedPlaceholders— compare template to formatted output; relies onMaintainTokensso missing keys stay visible as{Name}.
ITemplate workflow
Use AddContext on the template to layer IContextBuilder steps without allocating a full dictionary at the call site. Format(additionalContext) merges a one-off
context (dictionary or object) on top of the accumulated state for a single render.
ITemplate.TryValidateContext succeeds when the accumulated context keys cover every placeholder name (or supply a parent path like Order for {Order.Total}). Bare CLR objects
passed via WithContext(object?) do not participate in this check (only the merged dictionary and dictionary-shaped extras do), so call WithValue/AddContext or supply a
dictionary when you want the validator to confirm coverage.
SmartFormat behavior
This library does not fork SmartFormat; it configures a SmartFormatter instance. Refer to the SmartFormat documentation for list formatting, plural rules, and built-in extensions. Automation note: Lyo.Web.Automation documents that step templates use single-brace placeholders ({page.url}); legacy {{page.url}} is normalized there. This service accepts standard SmartFormat templates as-is.
Integration points
Lyo.Api— optionalIFormatterServiceforComputedFieldson projection/query responses (SmartFormat templates over projected rows).Lyo.Web.Automation— optionalIFormatterServiceto validate automation plans before execution.
Thread safety
FormatterService is safe for concurrent reads if you do not mutate SmartFormatter or Culture from multiple threads without synchronization. Typical ASP.NET Core registration as a singleton treats Culture as ambient per request by setting it at the start of a request (or avoid mutating Culture on the shared instance and pass culture-aware data in context instead).
Dependencies
Generated from ProjectReference / PackageReference (same model as docs/Lyo.ProjectGraph.html).
Lyo.Common— (direct, lyo)Lyo.Exceptions— (direct, lyo)Microsoft.Extensions.DependencyInjection.Abstractions10.0.5— (direct, microsoft)SmartFormat.NET3.6.1— (direct, third-party)Microsoft.Extensions.Logging.Abstractions10.0.5— (transitive, microsoft)System.Memory4.6.3— (transitive, microsoft, netstandard2.0)System.Text.Json10.0.5— (transitive, microsoft, netstandard2.0)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. 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. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Lyo.Common (>= 1.0.0)
- Lyo.Exceptions (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- SmartFormat.NET (>= 3.6.1)
-
net10.0
- Lyo.Common (>= 1.0.0)
- Lyo.Exceptions (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- SmartFormat.NET (>= 3.6.1)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Lyo.Formatter:
| Package | Downloads |
|---|---|
|
Lyo.Api
Core API library for building RESTful APIs with Entity Framework Core, caching, and mapping support. |
|
|
Lyo.Web.Automation
Shared browser automation models: element locators, JSON automation plans, session abstraction, and plan runners (engine-neutral). |
|
|
Lyo.Job.Scheduler
Job scheduling and execution service dispatching jobs via RabbitMQ message queue. |
|
|
Lyo.Formatter.Web.Components
WASM-safe Blazor components for live SmartFormat template editing and annotated preview using Lyo.Formatter. |
GitHub repositories
This package is not used by any popular GitHub repositories.