Rendering a Sub-Template
After you've registered a sub-template and set its properties, you can render the sub-template. There are several ways to do this. The first is to render the sub-template directly to the output of the main template:
// instantiate the sub-template. Header header = this.Create<Header>(); // render the sub-template to the current output stream. header.Render(this.Response);
Alternatively, you can render the sub-template to a separate file. This is useful when you want to create multiple output files as part of a single code-generation process.
// instantiate the sub-template. Header header = this.Create<Header>(); // render the sub-template to a separate file. header.RenderToFile("Somefile.txt");
The RenderToFile method has several overloads that allow for greater control when rending content. The overload shown below will prevent the a generated file from overwriting an already existing file called Somefile.txt.
// instantiate the sub-template. Header header = this.Create<Header>(); // render the sub-template to a separate file. // NOTE: If the file exists then an exception will be thrown. header.RenderToFile("Somefile.txt", false);
The other overloads allow you to use a Merge Strategies to control how the content should be merged with existing content. Also you can one of the overloads that takes a string file path or OutputFile to specify the the file that the output is DependentUpon.Â