A Sub-Template Example

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:

<%@ 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." %>
<html>
<head>
<% if (IncludeMeta) { %>
<meta http-equiv="Content-Type" content="text/html; charset=<%= CharSet %>">
<% } %>
<title><%= Title %></title>
</head>

Next, the main template generates the body of the HTML file. Note that it uses the sub-template to generate the header:

<%@ 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" %>
<% OutputHeader(); %>
<body>
<h1><%= Title %></h1>
<p><%= Placeholder %></p>
</body>
</html>
<script runat="template">
public void OutputHeader()
{
   Header header = this.Create<Header>();
   // include the meta 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);
}
</script>

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:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>My Web Page</title>
</head>
<body>
<h1>My Web Page</h1>
<p>Lorem Ipsit</p>
</body>
</html>