Page Formats

Templates is a general purpose text templating language which doesn't have any notion of HTML or any other format embedded in the language itself. It simply emits text outside of mustaches verbatim and inside mustaches evaluates the expression and emits the result. All custom behavior pertinent to specific text formats are instead extrapolated in its Page Format.

Templates supports rendering multiple different text formats simultaneously within the same TemplateContext, but the only Page Format pre-registered by default is the HtmlPageFormat which is defined below:

HTML Page Format

public class HtmlPageFormat : PageFormat
    public HtmlPageFormat()
        ArgsPrefix = "<!--";
        ArgsSuffix = "-->";
        Extension = "html";
        ContentType = MimeTypes.Html;
        EncodeValue = HtmlEncodeValue;
        ResolveLayout = HtmlResolveLayout;
        OnExpressionException = HtmlExpressionException;
    public static string HtmlEncodeValue(object value) { /*impl*/ }
    public TemplatePage HtmlResolveLayout(TemplatePage page) { /*impl*/ }
    public virtual object HtmlExpressionException(PageResult result, Exception ex) { /*impl*/ }
    public static async Task HtmlEncodeTransformer(Stream stream) { /*impl*/ }

The HtmlPageFormat is used to specify:

Results of all Template Expressions are HTML-encoded by default

The EncodeValue in the HtmlPageFormat automatically encodes the results of all template expressions so they're safe to embed in HTML, e.g:

You can use the raw default filter to skip HTML-encoding which will let you emit raw HTML as-is:

Inside filters you can return strings wrapped in a new RawString(string) or use the ToRawString() extension method to skip HTML-encoding.

Markdown Page Format

By contrast here is what the MarkdownPageFormat looks like which is able to use most of the default implementations:

public class MarkdownPageFormat : PageFormat
    public MarkdownPageFormat()
        Extension = "md";
        ContentType = MimeTypes.MarkdownText;

Registering a new PageFormat

To register a new Page Format you just need to add it to the TemplateContext's PageFormat collection:

var context = new TemplateContext { 
    PageFormats = { new MarkdownPageFormat() }

Which now lets you resolve pages with a .md file extension who will use the behavior defined in MarkdownPageFormat.

made with by ServiceStack