Class TemplateResolver
The template resolver is responsible to find what template to use for a certain type.
Inheritance
Inherited Members
Namespace: EPiServer.Web
Assembly: EPiServer.Cms.AspNet.dll
Version: 11.20.7Syntax
public abstract class TemplateResolver : ITemplateResolver, ITemplateResolverEvents
Remarks
The template resolver can be extended using TemplateResolving and TemplateResolved events.
Examples
The following code example demonstrates how to exchange the template for mobile requests.
[InitializableModule]
public class MobileRedirectSample : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved
+= new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}
public void Uninitialize(global::EPiServer.Framework.Initialization.InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved
-= new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}
void MobileRedirectSample_TemplateResolved(object sender, TemplateResolverEventArgs eventArgs)
{
if (eventArgs.ItemToRender != null && eventArgs.ItemToRender is TypedPage &&
HttpContext.Current.Request.Browser.IsMobileDevice)
{
var mobileRender = eventArgs.SupportedTemplates
.SingleOrDefault(r => r.Name.Contains("Mobile") &&
r.TemplateTypeCategory == eventArgs.SelectedTemplate.TemplateTypeCategory);
if (mobileRender != null)
{
eventArgs.SelectedTemplate = mobileRender;
}
}
}
public void Preload(string[] parameters)
{
}
}
Constructors
TemplateResolver()
Declaration
protected TemplateResolver()
Methods
GetContentType(IContentData)
Get content type for given contentData
Declaration
protected abstract ContentType GetContentType(IContentData contentData)
Parameters
Type | Name | Description |
---|---|---|
IContentData | contentData |
Returns
Type | Description |
---|---|
ContentType |
HasPageTemplate(PageData)
Determines whether the provided content data item has a page template.
Declaration
public virtual bool HasPageTemplate(PageData pageData)
Parameters
Type | Name | Description |
---|---|---|
PageData | pageData | The page data that should be checked if it has a template. |
Returns
Type | Description |
---|---|
System.Boolean |
|
HasPageTemplate(PageData, String)
Determines whether the provided content data item has a page template.
Declaration
public virtual bool HasPageTemplate(PageData pageData, string tag)
Parameters
Type | Name | Description |
---|---|---|
PageData | pageData | The page data that should be checked if it has a template. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
System.Boolean |
|
HasTemplate(IContentData, TemplateTypeCategories)
Determines whether the provided content data item has a template matching the provided template type.
Declaration
public virtual bool HasTemplate(IContentData contentData, TemplateTypeCategories templateTypeCategory)
Parameters
Type | Name | Description |
---|---|---|
IContentData | contentData | The content data. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be checked for. |
Returns
Type | Description |
---|---|
System.Boolean |
|
HasTemplate(IContentData, TemplateTypeCategories, ContextMode)
Determines whether the provided content data item has a template matching the provided template type in given ContextMode
Declaration
public virtual bool HasTemplate(IContentData contentData, TemplateTypeCategories templateTypeCategory, ContextMode contextMode)
Parameters
Type | Name | Description |
---|---|---|
IContentData | contentData | The content data. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be checked for. |
ContextMode | contextMode | The context mode. |
Returns
Type | Description |
---|---|
System.Boolean |
|
HasTemplate(IContentData, TemplateTypeCategories, String)
Determines whether the provided content data item has a template matching the provided template type.
Declaration
public virtual bool HasTemplate(IContentData contentData, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type | Name | Description |
---|---|---|
IContentData | contentData | The content data. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be checked for. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
System.Boolean |
|
OnTemplateResolved(TemplateResolverEventArgs)
Called raise the TemplateResolved event when the template has been resolved.
Declaration
protected virtual void OnTemplateResolved(TemplateResolverEventArgs eventArgs)
Parameters
Type | Name | Description |
---|---|---|
TemplateResolverEventArgs | eventArgs | The event arguments for the event. |
OnTemplateResolving(TemplateResolverEventArgs)
Called to raise the TemplateResolving event before attempting to resolve the template.
Declaration
protected virtual void OnTemplateResolving(TemplateResolverEventArgs eventArgs)
Parameters
Type | Name | Description |
---|---|---|
TemplateResolverEventArgs | eventArgs | The event arguments for the event. |
Resolve(HttpContextBase, ContentType, TemplateTypeCategories)
Resolves what rendering template that should be used for the provided content type given that it is of the provided template type.
Declaration
public virtual TemplateModel Resolve(HttpContextBase httpContext, ContentType contentType, TemplateTypeCategories templateTypeCategory)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
ContentType | contentType | The content type that the template should be associated with. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be returned. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
Resolve(HttpContextBase, ContentType, TemplateTypeCategories, String)
Resolves what rendering template that should be used for the provided content type given that it is of the provided template type.
Declaration
public virtual TemplateModel Resolve(HttpContextBase httpContext, ContentType contentType, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
ContentType | contentType | The content type that the template should be associated with. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be returned. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
Resolve(HttpContextBase, Object, TemplateTypeCategories)
Resolves what rendering template that should be used for the provided content item given that it is of the provided template type.
Declaration
public virtual TemplateModel Resolve(HttpContextBase httpContext, object itemToRender, TemplateTypeCategories templateTypeCategory)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
System.Object | itemToRender | The routed item of which type the template should be associated with. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be returned. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
Resolve(HttpContextBase, Object, TemplateTypeCategories, ContextMode)
Resolves what rendering template that should be used for the provided content item
given that it is of the provided template type and that it has a template for the specified contextMode
.
Declaration
public virtual TemplateModel Resolve(HttpContextBase httpContext, object itemToRender, TemplateTypeCategories templateTypeCategory, ContextMode contextMode)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
System.Object | itemToRender | The routed item of which type the template should be associated with. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be returned. |
ContextMode | contextMode | The context mode. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
Remarks
When contextMode
is Edit first a template with tag Edit is searched for
if no such is found a template with no tag is searched for and finally if no is found a template with tag Preview is searched for.
When contextMode
is Preview first a template with tag Preview is searched for
if no such is found a template with no tag is searched for.
When contextMode
is Default or Default first a template with tag Edit is searched for
if no such is found a template with no tag is searched for and finally if no is found a template with tag Preview is searched for.
Resolve(HttpContextBase, Object, TemplateTypeCategories, String)
Resolves what rendering template that should be used for the provided content item given that it is of the provided template type.
Declaration
public virtual TemplateModel Resolve(HttpContextBase httpContext, object itemToRender, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
System.Object | itemToRender | The routed item of which type the template should be associated with. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be returned. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
Resolve(HttpContextBase, Type, Object, TemplateTypeCategories, String)
Resolves what rendering template that should be used for the provided content item given that it is of the provided template type.
Declaration
public virtual TemplateModel Resolve(HttpContextBase httpContext, Type itemType, object itemToRender, TemplateTypeCategories templateTypeCategory, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The context. |
System.Type | itemType | Type of the item. |
System.Object | itemToRender | The routed item of which type the template should be associated with. |
TemplateTypeCategories | templateTypeCategory | The category of template type that should be returned. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolveCore(HttpContextBase, ContentType, Type, Object, TemplateTypeCategories, String)
Template resolver algorithm.
Declaration
protected abstract TemplateModel ResolveCore(HttpContextBase httpContext, ContentType contentType, Type itemType, object itemToRender, TemplateTypeCategories category, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP Context (optional, can be null) |
ContentType | contentType | The content type (optional, can be null) |
System.Type | itemType | The item type (required) |
System.Object | itemToRender | The item to render (optional, can be null) |
TemplateTypeCategories | category | The category (required) |
System.String | tag | The tag for the template (optional, can be null) |
Returns
Type | Description |
---|---|
TemplateModel | A template model if found or |
ResolveMvcTemplate(HttpContextBase, IContentData)
Resolves what MVC template that should be used for the provided content item.
Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContextBase httpContext, IContentData contentItem)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
IContentData | contentItem | The content item of which type the template should be associated with. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolveMvcTemplate(HttpContextBase, IContentData, String)
Resolves what MVC template that should be used for the provided content item.
Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContextBase httpContext, IContentData contentItem, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
IContentData | contentItem | The content item of which type the template should be associated with. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolveMvcTemplate(HttpContextBase, ContentType)
Resolves what MVC template that should be used for the provided content type.
Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContextBase httpContext, ContentType contentType)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
ContentType | contentType | The content type that the template should be associated with. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolveMvcTemplate(HttpContextBase, ContentType, String)
Resolves what MVC template that should be used for the provided content type.
Declaration
public virtual TemplateModel ResolveMvcTemplate(HttpContextBase httpContext, ContentType contentType, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
ContentType | contentType | The content type that the template should be associated with. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolvePageTemplate(HttpContextBase, IContentData)
Resolves what page template that should be used for the provided content item.
Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContextBase httpContext, IContentData contentItem)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
IContentData | contentItem | The content item of which type the template should be associated with. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolvePageTemplate(HttpContextBase, IContentData, String)
Resolves what page template that should be used for the provided content item.
Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContextBase httpContext, IContentData contentItem, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
IContentData | contentItem | The content item of which type the template should be associated with. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolvePageTemplate(HttpContextBase, ContentType)
Resolves what page template that should be used for the provided content type.
Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContextBase httpContext, ContentType contentType)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
ContentType | contentType | The content type that the template should be associated with. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
ResolvePageTemplate(HttpContextBase, ContentType, String)
Resolves what page template that should be used for the provided content type.
Declaration
public virtual TemplateModel ResolvePageTemplate(HttpContextBase httpContext, ContentType contentType, string tag)
Parameters
Type | Name | Description |
---|---|---|
System.Web.HttpContextBase | httpContext | The HTTP context. |
ContentType | contentType | The content type that the template should be associated with. |
System.String | tag | The tag to use when selecting renderer. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
Events
TemplateResolved
Occurs when tries to resolve control. This events raises after finding default or first supported template. One can change the selected template on the TemplateResolverEventArgs
Declaration
public event EventHandler<TemplateResolverEventArgs> TemplateResolved
Event Type
Type | Description |
---|---|
System.EventHandler<TemplateResolverEventArgs> |
Examples
The following code example demonstrates how to exchange the template for mobile requests.
[InitializableModule]
public class MobileRedirectSample : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved
+= new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}
public void Uninitialize(global::EPiServer.Framework.Initialization.InitializationEngine context)
{
context.Locate.TemplateResolver().TemplateResolved
-= new EventHandler<TemplateResolverEventArgs>(MobileRedirectSample_TemplateResolved);
}
void MobileRedirectSample_TemplateResolved(object sender, TemplateResolverEventArgs eventArgs)
{
if (eventArgs.ItemToRender != null && eventArgs.ItemToRender is TypedPage &&
HttpContext.Current.Request.Browser.IsMobileDevice)
{
var mobileRender = eventArgs.SupportedTemplates
.SingleOrDefault(r => r.Name.Contains("Mobile") &&
r.TemplateTypeCategory == eventArgs.SelectedTemplate.TemplateTypeCategory);
if (mobileRender != null)
{
eventArgs.SelectedTemplate = mobileRender;
}
}
}
public void Preload(string[] parameters)
{
}
}
TemplateResolving
Occurs when a template is about to be resolved.
Declaration
public event EventHandler<TemplateResolverEventArgs> TemplateResolving
Event Type
Type | Description |
---|---|
System.EventHandler<TemplateResolverEventArgs> |
Remarks
If an eventHandler sets the TemplateModel on the eventargs to the event then that render is used without calling for registered renderers.
Explicit Interface Implementations
ITemplateResolver.Resolve(Object, Type, TemplateTypeCategories, IEnumerable<String>)
Resolves what rendering template that should be used for the provided item given that it is of the provided template category.
Declaration
TemplateModel ITemplateResolver.Resolve(object item, Type itemType, TemplateTypeCategories category, IEnumerable<string> tags)
Parameters
Type | Name | Description |
---|---|---|
System.Object | item | The item of which type the template should be associated with. |
System.Type | itemType | in case item is null then type is used for resolving |
TemplateTypeCategories | category | The category of template type that should be returned. |
System.Collections.Generic.IEnumerable<System.String> | tags | The tags to use when selecting a template. |
Returns
Type | Description |
---|---|
TemplateModel | A TemplateModel object that contains information on the template. |
Remarks
The template tags are treated as a prioritized list meaning a template with a tag matching a preceding tag will be preferred.