Page Formats
#Script 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.
#Script supports rendering multiple different text formats simultaneously within the same ScriptContext, 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:
- That the Page Arguments should be defined within HTML comments at the top of the page
- That files with the .html extension should use this format
- That it should return the text/html Content-Type in HTTP Responses
- That the result of all Template Expressions should be HTML-encoded by default
- That it should not use a default layout for complete HTML files that start with a HTML or HTML5 tag
- How Expression Exceptions should be rendered in HTML if RenderExpressionExceptions is enabled
Results of all #Script Expressions are HTML-encoded by default
The EncodeValue in the HtmlPageFormat automatically encodes the results of all #Script 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 ScriptContext's PageFormat collection:
var context = new ScriptContext {
PageFormats = { new MarkdownPageFormat() }
}.Init();
Which now lets you resolve pages with a .md file extension who will use the behavior defined in MarkdownPageFormat.