Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Template descriptor attribute

The optional TemplateDescriptor attribute can be used on templates to add meta data to the template. The attribute can also be used to set the template as the default template. The attribute, which exists in the EPiServer.Framework.DataAnnotations namespace, contains the following properties:

Property nameDescriptionDefault value
Path The path to the template to be rendered. Needs to be set if folder structure does not follow namespace structure. There is a namespace convention where the file will be searched for in the path according to the namespace. For example if there is a template with type CodeSamples.Templates.Pages.MyTemplate, then the Path will be resolved if it is located in a folder structure that follows the namespace. So if there is a folder Templates in the application root and it has a sub folder Pages where the file MyTemplate.aspx is located then the Path will be found and hence Path does not need to be set.
The system tries to find the template file using the same conventions in the corresponding Shell module folder if the Path property is not set and the template type is associated with a module.
The system also tries to resolve the full virtual path to the template file in a module folder when the template type is associated with a Shell module and the Path property value contains a path which is not application relative and not an absolute virtual path.
null
ModelType The model for the content type. This can be set on an untyped template which derives from EPiServer.TemplatePage to set the model type, which will be the type of the CurrentPage property. null
Default Default defines the template as the default template for the model type. false
Description Description contains a description of the template. null
Inherited Inherited means that when this property is set to true, all model types which inherits from the ModelType will get the template as a supported template. false

Example

Some sample of a MVC Controller that handles all page types that don't have their own specific controllers by specifying Inherited=true and dynamically selecting template. See Alloy templates for a fully working sample.

    [TemplateDescriptor(Inherited = true)]
    public class DefaultPageController : PageControllerBase<SitePageData>
    {
        public ViewResult Index(SitePageData currentPage)
        {
            var model = CreateModel(currentPage);
            return View(string.Format("~/Views/{0}/Index.cshtml", currentPage.GetOriginalType().Name), model);
        }

        /// <summary>
        /// Creates a PageViewModel where the type parameter is the type of the page.
        /// </summary>
        /// <remarks>
        /// Used to create models of a specific type without the calling method having to know that type.
        /// </remarks>
        private static IPageViewModel<SitePageData> CreateModel(SitePageData page)
        {
            var type = typeof(PageViewModel<>).MakeGenericType(page.GetOriginalType());
            return Activator.CreateInstance(type, page) as IPageViewModel<SitePageData>;
        }
    }
Do you find this information helpful? Please log in to provide feedback.

Last updated: Nov 19, 2014

Recommended reading