MOSS 2007: Creating named templates
Named templates allow for templating the output of a supporting field control from a centralised template. It's not necessarily meant for html reuse on page layouts, but it would do the job. You'd really only want to use it if you had complex html being outputted by an image/text field and it the same html was used across multiple pages.
Like I mentioned in a previous post, field templates (named or inline) will not give you property-level access to content; meaning you can't access the alternate text or the url of a link by itself. With that warning aside, let's create a named template.
To create a named template called 'ImageCustomTemplate':
- Create a file named CustomTemplates.ascx in c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES (you can call the ascx file anything you want, as long as it is .ascx)
- In CustomTemplates.ascx, create a RenderingTemplate with the ID of 'ImageCustomTemplate'. Here is an example:
<%@ Control Language="C#" %> <%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%> <spwc:RenderingTemplate ID="ImageCustomTemplate" runat="server"> <Template> [<spwc:FieldValue runat="server" />] </Template> </spwc:RenderingTemplate> - Add a RichImageField to your page layout, assign the FieldName and give it a DisplayTemplateName of ImageCustomTemplate:
<pwc:RichImageField runat="server" FieldName="Page Image" DisplayTemplateName="ImageCustomTemplate" />
- Restart the website application/application pool.
