Interface ITemplateService
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
namestringThe source name, basically the first part of the token eg: [Name:Value]
valuesIDictionary<string, string>
Returns
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
namestringThe source name, basically the first part of the token eg: [Name:Value]
getterFunc<string, string, string>
Returns
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
namestringThe source name, basically the first part of the token eg: [Name:Value]
getterFunc<string, string>The function which uses the key to retrieve a value. It will be case-sensitive/-insensitive based on your code.
Returns
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
namestringThe source name, basically the first part of the token eg: [Name:Value]
itemICanBeEntityAn IEntity, ITypedItem or similar object.
npoNoParamOrderdimensionsstring[]optional array of languages to use when looking for the value - if the data is multi-language. Default to current languages.
Returns
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
namestringThe source name, basically the first part of the token eg: [Name:Value]
originalILookUp
Returns
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
npoNoParamOrdersourcesIEnumerable<ILookUp>optional additional sources
Returns
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
npoNoParamOrdersourcesIEnumerable<ILookUp>optional sources, but without them this engine won't do much
Returns
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
namestring
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
namestringsourcesIEnumerable<ILookUp>
Returns
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
templatestringnpoNoParamOrderallowHtmlboolallow adding html to the string - if false (default) will html encode anything found for safety before replacing something
sourcesIEnumerable<ILookUp>recursionsintRecursion 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
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
originalICanBeEntitynpoNoParamOrderallowHtmlboolallow adding html to the string - if false (default) will html encode anything found for safety before replacing something
parserITemplateEngineA prepared parser - takes preference over
sourcessourcesIEnumerable<ILookUp>A list of sources to create a parser
recursionsintRecursion 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
0for 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
originalICanBeEntitynpoNoParamOrderallowHtmlboolallow adding html to the string - if false (default) will html encode anything found for safety before replacing something
parserITemplateEngineA prepared parser - takes preference over
sourcessourcesIEnumerable<ILookUp>A list of sources to create a parser
recursionsintRecursion 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
0for 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.