Introduction
This document describes how to control which template that is used to render a referenced content using tags.
Example
Given that there is a block registered
as follows:
C#
[ContentType]
public class Teaser : BlockData
{
public virtual string Heading { get; set; }
public virtual XhtmlString Content { get; set; }
}
It is possible to have several templates defined for the same model. The example below
registers two different templates for the same model. One is DefaultTeaserTemplate that has property Default set to true in TemplateDescriptor attribute which
states that this is the default template to use when rendering instances of the model. The other template is another template that
is more suitable when rendering in a Sidebar area and is therefore marked with Tag Sidebar.
C#
[TemplateDescriptor(Default = true)]
public partial class DefaultTeaserTemplate : BlockControlBase<Teaser>
{ }
[TemplateDescriptor(Tags = new string[] { RenderingTags.Sidebar })]
public partial class SidebarTeaserTemplate : BlockControlBase<Teaser>
{ }
If you use Property web control to render a ContentArea you
can specify the tag by using RenderSettings attribute as follows:
C#
<EPiServer:Property PropertyName="MyContentArea" runat="server">
<RenderSettings Tag="Sidebar" />
</EPiServer:Property>
If you use ContentControl web control to render a ContentData instance you
can specify the tag by using Tag attribute as follows:
C#
<EPiServer:ContentRenderer Tag="<%#EPiServer.Framework.Web.RenderingTags.Sidebar%>" CurrentData=<%#ContentData%> runat="server" />
If you use MVC to render a ContentArea property you
can specify the tag by using Tag attribute as follows:
C#
<%: Html.PropertyFor(m => m.MyContentArea, new { Tag = EPiServer.Framework.Web.RenderingTags.Sidebar })%>
By default when a template renderer is associated with a Tag then that renderer will only be available when the calling context
(given by for example Property, ContentControl, PropertyFor) has a matching tag. By setting the attribute AvailableWithoutTag
to true on your template the template will be available also when calling context has no tag specified. Se example below:
C#
[TemplateDescriptor(AvailableWithoutTag = true, Tags = new string[] { RenderingTags.Sidebar })]
public partial class SidebarTeaserTemplate : BlockControlBase<Teaser>
{ }
Do you find this information helpful? Please log in to provide feedback.