An open source template engine for C#

 

[Click here to return to the main page of this project]

Example 3: Embedding Templates

You can also embed your templates in your assembly instead of leaving them in external files. This is useful if you'd like to "package" templates with your generator code.

See the driver code which uses an embedded template:

File SampleDriver3.cs:
using System;
using TemplateMaschine;

namespace Sample
{
    class SampleDriver3
    {
        static void Main(string[] args)
        {
            // Load a template from the current executing 
            // assembly and compile it
            Template myTemplate = new Template("Sample.template", 
                                   typeof(SampleDriver3).Assembly);

            // Process the template with no arguments
            // and write output to file 'SampleOutput.txt'
            myTemplate.Generate(null, "SampleOutput.txt");
        }
    }
}

Go to the [download section] to download the complete source code.

The template processor looks for the template Sample.template in the current executing assembly (stated by typeof(SampleDriver3).Assembly).

To embed a template in an assembly you have two possibilities: You can use the /resource: command line switch from csc.exe, if you're using a build script for example...

C:\> csc /resource:Sample.template /r:TemplateMaschine.dll SampleDriver3.cs

...or, if you are using Visual Studio .NET, select the template file in solution explorer and set it's "Build Action" to "Embedded Resource" in the Properties Explorer:

 

[Click here to return to the main page of this project]