Table of Contents

Interface ITemplateService

Namespace
ToSic.Sxc.Services
Assembly
ToSic.Sxc.Services.dll

Service on Kit.Template to help parse token-based templates.

[PublicApi]
public interface ITemplateService

Remarks

Released in 18.03

Methods

CreateSource(string, IDictionary<string, string>)

Create a source based on a dictionary. Lookup will be case-insensitive.

ILookUp CreateSource(string name, IDictionary<string, string> values)

Parameters

name string

The source name, basically the first part of the token eg: [Name:Value]

values IDictionary<string, string>

Returns

ILookUp

CreateSource(string, Func<string, string, string>)

Create a source using a function, basically a very custom source. This variant has 2 string parameters - the key and the format-string.

ILookUp CreateSource(string name, Func<string, string, string> getter)

Parameters

name string

The source name, basically the first part of the token eg: [Name:Value]

getter Func<string, string, string>

Returns

ILookUp

CreateSource(string, Func<string, string>)

Create a source using a function, basically a very custom source.

ILookUp CreateSource(string name, Func<string, string> getter)

Parameters

name string

The source name, basically the first part of the token eg: [Name:Value]

getter Func<string, string>

The function which uses the key to retrieve a value. It will be case-sensitive/-insensitive based on your code.

Returns

ILookUp

CreateSource(string, ICanBeEntity, NoParamOrder, string[]?)

Create a source using an entity (or entity-like thing such as an ITypedItem) as the source.

ILookUp CreateSource(string name, ICanBeEntity item, NoParamOrder npo = default, string[]? dimensions = null)

Parameters

name string

The source name, basically the first part of the token eg: [Name:Value]

item ICanBeEntity

An IEntity, ITypedItem or similar object.

npo NoParamOrder

see Convention: Named Parameters

dimensions string[]

optional array of languages to use when looking for the value - if the data is multi-language. Default to current languages.

Returns

ILookUp

CreateSource(string, ILookUp)

Create a source based on another source. This is mainly used to give a source another name.

ILookUp CreateSource(string name, ILookUp original)

Parameters

name string

The source name, basically the first part of the token eg: [Name:Value]

original ILookUp

Returns

ILookUp

Default(NoParamOrder, IEnumerable<ILookUp>?)

Start with the default engine, which already has lookups for QueryString and similar sources.

ITemplateEngine Default(NoParamOrder npo = default, IEnumerable<ILookUp>? sources = null)

Parameters

npo NoParamOrder

see Convention: Named Parameters

sources IEnumerable<ILookUp>

optional additional sources

Returns

ITemplateEngine

Empty(NoParamOrder, IEnumerable<ILookUp>?)

Start with an empty engine. This usually only makes sense, if you provide custom sources.

ITemplateEngine Empty(NoParamOrder npo = default, IEnumerable<ILookUp>? sources = null)

Parameters

npo NoParamOrder

see Convention: Named Parameters

sources IEnumerable<ILookUp>

optional sources, but without them this engine won't do much

Returns

ITemplateEngine

GetSource(string)

Get a built-in source by name. This is usually used when you want to create a template-engine with some specific sources, and you explicitly need for example the QueryString source as well.

ILookUp? GetSource(string name)

Parameters

name string

Returns

ILookUp

The source if found, otherwise null

MergeSources(string, IEnumerable<ILookUp>)

Merge multiple sources into one.

ILookUp MergeSources(string name, IEnumerable<ILookUp> sources)

Parameters

name string
sources IEnumerable<ILookUp>

Returns

ILookUp

Remarks

Added v17.09

Parse(string, NoParamOrder, bool, IEnumerable<ILookUp>?, int)

Quick parse a template using the default engine, and optional sources.

string Parse(string template, NoParamOrder npo = default, bool allowHtml = false, IEnumerable<ILookUp>? sources = null, int recursions = 0)

Parameters

template string
npo NoParamOrder

see Convention: Named Parameters

allowHtml bool

allow adding html to the string - if false (default) will html encode anything found for safety before replacing something

sources IEnumerable<ILookUp>
recursions int

Recursion depth for tokens-in-tokens. Could leak unexpected information, if a token contains user provided data or url-parameters so use with caution. Defaults to 0, added v20.09

Returns

string

ParseAsItem(ICanBeEntity, NoParamOrder, bool, ITemplateEngine?, IEnumerable<ILookUp>?, int)

Take an entity, TypedItem or similar object, and return a TypedItem which will run its values through the parser.

ITypedItem ParseAsItem(ICanBeEntity original, NoParamOrder npo = default, bool allowHtml = false, ITemplateEngine? parser = null, IEnumerable<ILookUp>? sources = null, int recursions = 0)

Parameters

original ICanBeEntity
npo NoParamOrder

see Convention: Named Parameters

allowHtml bool

allow adding html to the string - if false (default) will html encode anything found for safety before replacing something

parser ITemplateEngine

A prepared parser - takes preference over sources

sources IEnumerable<ILookUp>

A list of sources to create a parser

recursions int

Recursion depth for tokens-in-tokens. Could leak unexpected information, if a token contains user provided data or url-parameters so use with caution. Defaults to 0 for safety.

Returns

ITypedItem

An ITypedItem to be used with .String("name") etc.

Remarks

WIP v20.09

If neither parser nor sources are provided, an empty parser will be used, resulting in no changes to the original values.

ParseAs<T>(ICanBeEntity, NoParamOrder, bool, ITemplateEngine?, IEnumerable<ILookUp>?, int)

Take an entity, TypedItem or similar object, and return a type T which will run its values through the parser.

T ParseAs<T>(ICanBeEntity original, NoParamOrder npo = default, bool allowHtml = false, ITemplateEngine? parser = null, IEnumerable<ILookUp>? sources = null, int recursions = 0) where T : class, ICanWrapData

Parameters

original ICanBeEntity
npo NoParamOrder

see Convention: Named Parameters

allowHtml bool

allow adding html to the string - if false (default) will html encode anything found for safety before replacing something

parser ITemplateEngine

A prepared parser - takes preference over sources

sources IEnumerable<ILookUp>

A list of sources to create a parser

recursions int

Recursion depth for tokens-in-tokens. Could leak unexpected information, if a token contains user provided data or url-parameters so use with caution. Defaults to 0 for safety.

Returns

T

A TypedItem of specified type T

Type Parameters

T

Remarks

WIP v20.09

If neither parser nor sources are provided, an empty parser will be used, resulting in no changes to the original values.