Try our conversational search powered by Generative AI!

Class TemplateResolver

The template resolver is responsible to find what template to use for a certain type.

Inheritance
System.Object
TemplateResolver
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: EPiServer.Web
Assembly: EPiServer.dll
Version: 7.19.2
Syntax
public class TemplateResolver
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.

Constructors

TemplateResolver(IContentTypeRepository, TemplateModelRepository, DisplayChannelService, TemplateModelSelector)

Initializes a new instance of the TemplateResolver class.

Declaration
public TemplateResolver(IContentTypeRepository contentTypeRepository, TemplateModelRepository templateModelRepository, DisplayChannelService displayChannelService, TemplateModelSelector modelSelector)
Parameters
Type Name Description
IContentTypeRepository contentTypeRepository

The content type model repository that will be used by this instance.

TemplateModelRepository templateModelRepository

The template model repository that will be used by this instance.

DisplayChannelService displayChannelService

The display channel service.

TemplateModelSelector modelSelector

The model selector.

Methods

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

true if the specified render context has template; otherwise, false.

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

true if the specified render context has template; otherwise, false.

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

true if the specified render context has template; otherwise, false.

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

true if the specified render context has template; otherwise, false.

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

true if the specified render context has template; otherwise, false.

OnTemplateResolved(HttpContextBase, ContentType, Type, Object, TemplateModel, String)

Called raise the TemplateResolved event when the template has been resolved.

Declaration
protected virtual TemplateModel OnTemplateResolved(HttpContextBase httpContext, ContentType contentType, Type itemType, object itemToRender, TemplateModel selectedTemplate, string tag)
Parameters
Type Name Description
System.Web.HttpContextBase httpContext

The HTTP context.

ContentType contentType

The content type.

System.Type itemType

Type of the item.

System.Object itemToRender

The content data.

TemplateModel selectedTemplate

The selected template.

System.String tag

The tag.

Returns
Type Description
TemplateModel

The selected template that could have been changed by the event.

OnTemplateResolving(HttpContextBase, ContentType, Type, Object, String)

Called to raise the TemplateResolving event before attempting to resolve the template.

Declaration
protected virtual TemplateModel OnTemplateResolving(HttpContextBase httpContext, ContentType contentType, Type itemType, object itemToRender, string tag)
Parameters
Type Name Description
System.Web.HttpContextBase httpContext

The HTTP context.

ContentType contentType

Type of the content.

System.Type itemType

Type of the item.

System.Object itemToRender

The content data.

System.String tag

The tag.

Returns
Type Description
TemplateModel

The selected template that could have been changed by 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 virtual 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 null if no model was found

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.

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.

Extension Methods