Protected Filters

One of the goals of Templates is that its defaults should be safe enough to be able to execute arbitrary templates by untrusted 3rd parties. Given this constraint, only default filters are pre-registered which contain a comprehensive set of filters we deem safe for use by anyone. Other filters available in Templates which are useful to have in a server-generated website environment but we don't want 3rd Parties to access are filters in TemplateProtectedFilters.cs.

TemplateProtectedFilters are not pre-registered when creating a new TemplateContext but they are pre-registered when registering the TemplatePagesFeature ServiceStack Plugin as that's designed to use Templates as a View Engine where access to its context is limited to the server's Web Application.

To access TemplateProtectedFilters features within your own TemplateContext it can be registered like any other filter:

var context = new TemplateContext
{
    TemplateFilters = { new TemplateProtectedFilters() }
}.Init();

includeFile

fileContents

Use includeFile to embed content directly within templates:

page.html
my-file.txt
Cascading resolution

If an exact match isn't found it will look for the closest file with that name it can find, starting from the directory where the containing page that uses the filter is located and traversing up until it reaches the root folder.

includeFileWithCache

fileContentsWithCache

If your VirtualFiles is configured to use a combination of various sources that includes a remote file service like S3VirtualFiles, you'll likely want to cache the contents in memory to ensure fast subsequent access the next time the file is requested, which you can cache without expiration using includeFileWithCache without arguments:

{{ 'my-file.txt' | includeFileWithCache }}

In which case it will use the 1 minute default overridable in Args[DefaultFileCacheExpiry] or if you want the content to be refreshed after 1hr you can use:

{{ 'my-file.txt' | includeFileWithCache({ expiresInSecs: 3600 }) }}

includeUrl

urlContents

You can also embed the contents of remote URLs in your page using includeUrl:

page.html

includeUrl is actually a very flexible HTTP Client which can leverage the URL Handling filters to easily construct urls and the additional filter arguments to customize the HTTP Request that's sent, here are some examples:

Accept JSON responses:

{{ url | includeUrl({ accept: 'application/json' }) }}
{{ url | includeUrl({ dataType: 'json' }) }}

Send data as form-urlencoded in a HTTP PUT Request with a Templates User-Agent:

{{ url | includeUrl({ method:'PUT', data: { id: 1, name: 'foo' }, userAgent:"Templates" }) }}

Send data as JSON in a HTTP POST request and Accept JSON response:

{{ url | includeUrl({ method: 'POST', data: { id: 1, name: 'foo' }, 
                      accept: 'application/json', contentType: 'application/json' }) }}

Shorter version of above request:

{{ url | includeUrl({ method:'POST', data: { id: 1, name: 'foo' }, dataType: 'json' }) }}

Send data as CSV in a HTTP POST Request and Accept a CSV response:

includeUrl({ method:'POST', data: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }], dataType:'csv' })

includeUrlWithCache

urlContentsWithCache

In the same way includeFileWithCache can cache file contents, includeUrlWithCache can cache URL content, either without an expiration:

{{ url | includeUrlWithCache }}

In which case it will use the 1 minute default overridable in Args[DefaultUrlCacheExpiry] or if you want to ensure that no more than 1 url request is made per hour for this url you can specify a custom expiry with:

{{ url | includeUrlWithCache({ expiresInSecs: 3600 }) }}

Virtual File System APIs

The Virtual File System APIs are mapped to the following filters:

Filter Name Virtual File System API
File APIs
filesFind GetAllMatchingFiles(globPatern)
fileExists FileExists(virtualPath)
file GetFile(virtualPath)
fileWrite WriteFile(virtualPath, contents)
fileAppend AppendFile(virtualPath, contents)
fileDelete DeleteFile(virtualPath)
fileReadAll GetFile(virtualPath).ReadAllText()
fileReadAllBytes GetFile(virtualPath).ReadAllBytes()
fileHash GetFileHash(virtualPath)
Directory APIs
dir GetDirectory(virtualPath)
dirExists DirectoryExists(virtualPath)
dirFile GetDirectory(dirPath).GetFile(fileName)
dirFiles .GetDirectory(dirPath).GetFiles()
dirDirectory GetDirectory(dirPath).GetDirectory(dirName)
dirDirectories GetDirectory(dirPath).GetDirectories()
dirFilesFind GetDirectory(dirPath).GetAllMatchingFiles(globPatern)
Virtual File System APIs
vfsAllFiles GetAllFiles()
vfsAllRootFiles GetRootFiles()
vfsAllRootDirectories GetRootDirectories()
vfsCombinePath CombineVirtualPath(basePath, relativePath)

See the Filters API Reference for the full list of Protected filters available.

made with by ServiceStack