Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
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

...

:

{color:navy}<%@ CodeTemplate Language="C#" TargetLanguage="HTML" %>{color}
{color:navy}<%@ Property Name="Title" Type="System.String" Optional="False" Category="Options" Description="Page title." %>{color}
{color:navy}<%@ Property Name="CharSet" Type="System.String" Optional="False"  Default="windows-1252" Category="Options" Description="Character set for  the page." %>{color}
{color:navy}<%@ Property Name="IncludeMeta" Type="System.Boolean" Default="True"  Optional="False" Category="Options" Description="Include meta tags."  %>{color}
{color:navy}<html>{color}
{color:navy}<head>{color}
{color:navy}<% if (IncludeMeta){color} { %>
<meta http-equiv="Content-Type" content="text/html; charset=<%= CharSet %>">
<% } {color:navy}%>{color}
{color:navy}<title><%= Title %></title>{color}
{color:navy}</head>

...

{color}

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

...

:

{color:navy}<%@ CodeTemplate Language="C#" TargetLanguage="HTML"

...

 %>{color}
{color:navy}<%@ Property Name="Title" Type="System.String" Optional="False" Category="Options" Description="Page title."

...

 %>{color}
{color:navy}<%@ Property Name="Placeholder" Type="System.String" Optional="True"  Category="Options" Description="Main placeholder text."

...

 %>{color}
{color:navy}<%@ Register Name="Header" Template="Header.cst" MergeProperties="True" ExcludeProperties="IncludeMeta"

...

 %>{color}
{color:navy}<% OutputHeader();

...

 %>{color}
{color:navy}<body>{color}
{color:navy}<h1><%= Title %></h1>{color}
{color:navy}<p><%= Placeholder %></p>{color}
{color:navy}</body>{color}
{color:navy}</html>{color}
{color:navy}<script runat="template">

...

{color}
{color:navy}public void OutputHeader(){color}
{color:navy}{{color}
{color:navy}&nbsp;&nbsp;&nbsp;Header header = this.Create<Header>()

...

;{color}
{color:navy}&nbsp;&nbsp;&nbsp;// include the meta tag{color}
{color:navy}&nbsp;&nbsp;&nbsp;header.IncludeMeta = true;{color}
{color:navy}&nbsp;&nbsp;&nbsp;// copy all properties with matching name and type to the sub-template instance{color}
{color:navy}&nbsp;&nbsp;&nbsp;this.CopyPropertiesTo(header);{color}
{color:navy}&nbsp;&nbsp;&nbsp;// render the sub-template to the current output stream{color}
{color:navy}&nbsp;&nbsp;&nbsp;header.Render(this.Response);

...

{color}
{color:navy}}{color}
{color:navy}</script>

...

{color}

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 GOES HERE

...

:

!SubtemplateProperties.png!

{color:navy}<html>{color}
{color:navy}<head>{color}
{color:navy}<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

...

{color}
{color:navy}<title>My Web Page</title>

...

{color}
{color:navy}</head>

...

{color}
{color:navy}<body>{color}
{color:navy}<h1>My Web Page</h1>{color}
{color:navy}<p>Lorem Ipsit</p>{color}
{color:navy}</body>{color}
{color:navy}</html>{color}