API Reference

TemplateContext

The TemplateContext is the sandbox where all templates are executed within that can be customized with the available APIs below:

Preconfigured defaults

Some default filters when called without arguments will use the default configuration shown below that can be overridden by replacing their default value in the TemplateContext's Args collection:

var context = new TemplateContext { 
    Args = {
        [TemplateConstants.MaxQuota] = 10000,
        [TemplateConstants.DefaultCulture] = CultureInfo.CurrentCulture,
        [TemplateConstants.DefaultDateFormat] = "yyyy-MM-dd",
        [TemplateConstants.DefaultDateTimeFormat] = "u",
        [TemplateConstants.DefaultTimeFormat] = "h\\:mm\\:ss",
        [TemplateConstants.DefaultFileCacheExpiry] = TimeSpan.FromMinutes(1),
        [TemplateConstants.DefaultUrlCacheExpiry] = TimeSpan.FromMinutes(1),
        [TemplateConstants.DefaultIndent] = "\t",
        [TemplateConstants.DefaultNewLine] = Environment.NewLine,
        [TemplateConstants.DefaultJsConfig] = "excludetypeinfo",
        [TemplateConstants.DefaultStringComparison] = StringComparison.Ordinal,
        [TemplateConstants.DefaultTableClassName] = "table",
        [TemplateConstants.DefaultErrorClassName] = "alert alert-danger",
    }
}.Init();

Args

TemplateContext Arguments can be used to define global variables available to every template, partial, filter, etc:

Virtual Files

Templates only have access to Pages available from its configured VirtualFiles which uses an empty MemoryVirtualFiles. To make pages available to your TemplateContext instance you can choose to either programatically populate the VirtualFiles collection from an external source, e.g:

var fs = new FileSystemVirtualFiles("~/template-files".MapProjectPath());
foreach (var file in fs.GetAllMatchingFiles("*.html"))
{
    if (!MyAllowFile(file)) continue;
    using (var stream = file.OpenRead())
    {
        context.VirtualFiles.WriteFile(file.VirtualPath, stream);
    }
}

Alternatively if you want to enable access to an entire sub directory you can replace the Virtual Files with a FileSystem VFS at the directory you want to make the root directory:

context.VirtualFiles = new FileSystemVirtualFiles("~/template-files".MapProjectPath());

DebugMode

DebugMode is used to control whether full Exception details like StackTrace is displayed. In TemplatePageFeature it defaults to the AppHost DebugMode, otherwise it's true by default.

ScanTypes

Specify a TemplateFilter or TemplateCodePage to auto register.

ScanAssemblies

Specify assemblies that should be scanned to find TemplateFilter's and TemplateCodePage's to auto register. In TemplatePageFeature the AppHost's Service Assemblies are included by default.

TemplateFilters

Register additional instances of filters you want templates to have access to.

CodePages

Register instances of code pages you want templates to have access to.

Container

The IOC Container used by the TemplateContext to register and resolve dependencies, filters and Code Pages. Uses SimpleContainer by default.

AppSettings

Specify an optional App Settings provider that templates can access with the {{ key | appSetting }} default filter.

CheckForModifiedPages

Whether to check for modified pages by default when not in DebugMode, defaults to true. Note: if DebugMode is true it will always check for changes.

CheckForModifiedPagesAfter

If provided will specify how long to wait before checking if backing files of pages have changed and to reload them if they have. Note: if DebugMode is true it will always check for changes.

RenderExpressionExceptions

Whether to Render Expression Exceptions in-line (default = false).

PageResult

The PageResult is the rendering context used to render templates whose output can be customized with the APIs below:

Layout

Override the layout used for the page by specifying a layout name:

new PageResult(page) { Layout = "custom-layout" }

LayoutPage

Override the layout used for the page by specifying a Layout page:

new PageResult(page) { LayoutPage = Request.GetPage("custom-layout") }

Args

Override existing or specify additional arguments in the Template's scope:

new PageResult(page) { 
    Args = { 
        ["myArg"] = "argValue",
    }
}

TemplateFilters

Make additional filters available to the Template:

new PageResult(page) { 
    TemplateFilters = { new MyFilters() }
}

OutputTransformers

Transform the entire Template's Output before rendering to the OutputStream:

new PageResult(page) {
    ContentType = MimeTypes.Html,
    OutputTransformers = { MarkdownPageFormat.TransformToHtml },
}

PageTransformers

Transform just the Page's Output before rendering to the OutputStream:

new PageResult(page) {
    ContentType = MimeTypes.Html,
    PageTransformers = { MarkdownPageFormat.TransformToHtml },
}

FilterTransformers

Specify additional Filter Transformers available to the Template:

new PageResult(page) {
    FilterTransformers = {
        ["markdown"] = MarkdownPageFormat.TransformToHtml
    }
}

ExcludeFiltersNamed

Disable access to the specified registered filters:

new PageResult(page) {
    ExcludeFiltersNamed = { "partial", "selectPartial" }
}

Options

Return additional HTTP Response Headers when rendering to a HTTP Response:

new PageResult(page) {
    Options = { 
        "X-Powered-By" = "ServiceStack Templates" 
    }
}

ContentType

Specify the HTTP Content-Type when rendering to a HTTP Response:

new PageResult(page) {
    ContentType = "text/plain"
}

made with by ServiceStack