Here's a simple example so you can see how the various sub-template pieces fit together. This example generates an HTML file from two templates. First, there's a sub-template that generates an HTML header:
Code Block |
---|
|
<%@ CodeTemplate Language="C#" TargetLanguage="HTML" %> |
...
<%@ Property Name="Title" Type="System.String" Optional="False" Category="Options" Description="Page title." %> |
...
<%@ Property Name="CharSet" Type="System.String" Optional="False" Default="windows-1252" Category="Options" Description="Character set for the page." %> |
...
<%@ Property Name="IncludeMeta" Type="System.Boolean" Default="True" Optional="False" Category="Options" Description="Include meta tags." %> |
...
...
...
...
<meta http-equiv="Content-Type" content="text/html; charset=<%= CharSet %>"> |
...
...
<title><%= Title %></title> |
...
Next, the main template generates the body of the HTML file. Note that it uses the sub-template to generate the header:
Code Block |
---|
|
<%@ CodeTemplate Language="C#" TargetLanguage="HTML" %> |
...
<%@ Property Name="Title" Type="System.String" Optional="False" Category="Options" Description="Page title." %> |
...
<%@ Property Name="Placeholder" Type="System.String" Optional="True" Category="Options" Description="Main placeholder text." %> |
...
<%@ Register Name="Header" Template="Header.cst" MergeProperties="True" ExcludeProperties="IncludeMeta" %> |
...
...
...
...
<p><%= Placeholder %></p> |
...
...
...
<script runat="template"> |
...
public void OutputHeader() |
...
{
Header header = this.Create<Header>(); |
...
...
tag
header.IncludeMeta = true; |
...
// copy all properties with matching name and type to the sub-template |
...
instance
this.CopyPropertiesTo(header); |
...
// render the sub-template to the current output |
...
stream
header.Render(this.Response); |
...
When you open the master template, the property sheet shows the Title and Placeholder properties defined in the master template, as well as the CharSet property defined in the sub-template (because of the MergeProperties attribute), but not the IncludeMeta property (because of the ExcludeProperties attribute):
The template's output seamlessly merges the output of the sub-template and the output of the main template:Image Removed
Image Added
...
...
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> |
...
<title>My Web Page</title> |
...
...
...
...
...
...