Put any text you want in a file.
template.txt:
Hello, World!
By default, define embedded files using lines that start with '###' followed by a file path.
template.txt:
Hello, World!
### C:\file.txt
file.txt (nested file):
This is a nested file.
By default, parameters are defined by surrounding a parameter name in double curly braces: {{parameter}}.
Template File:
Hello, {{name}}!
### C:\file.txt
4. Get the text of the generated templates and write it to a file, or do anything else you want with it.
C# Code:
// Define the template path
string templatePath = "C:\\template.txt";
// Define template parameters
Dictionary<string, string>[] parameters = new Dictionary<string, string>[1];
parameters[0] = new Dictionary<string, string>();
parameters[0].Add("name", "World");
TemplateData templateData = new TemplateData();
templateData.AddTemplateParameterData("template", parameters);
// Get the expanded template text
TemplateGenerator templateGenerator = new TemplateGenerator(templateData);
string templateText = templateGenerator.GenerateTemplateText(templatePath);
Result:
Hello, World!
This is a nested file.
5. Assign multiple parameter sets to a template's TemplateData to repeat that template for each parameter set.
C# Code:
// Define the template path
string templatePath = "C:\\template.txt";
// Define template parameters
Dictionary<string, string>[] parameters = new Dictionary<string, string>[2];
parameters[0] = new Dictionary<string, string>();
parameters[0].Add("name", "World");
parameters[1] = new Dictionary<string, string>();
parameters[2].Add("name", "Other World");
TemplateData templateData = new TemplateData();
templateData.AddTemplateParameterData("template", parameters);
// Get the expanded template text
TemplateGenerator templateGenerator = new TemplateGenerator(templateData);
string templateText = templateGenerator.GenerateTemplateText(templatePath);
Result:
Hello, World!
Hello, Other World!
This is a nested file.
Note: Passing an empty array or null for the parameters will result in a single instance of the template text being generated by default. An option can be set on the TemplateData object to cause nothing to be generated instead.
C# Code:
// Define the template path
string templatePath = "C:\\template.txt";
// Define template parameters
List<ParameterSets> parameters= new ParameterSet[2];
parameters.Add(new ParameterSet() { Name = "World", });
parameters.Add(new ParameterSet() { Name = "Other World" });
TemplateData templateData = new TemplateData();
// Note: The 3rd parameter is optional--whether to convert parameter objects' property names to camelCase
templateData.AddTemplateParameterData("template", parameters, true);
// Get the expanded template text
TemplateGenerator templateGenerator = new TemplateGenerator(templateData);
string templateText = templateGenerator.GenerateTemplateText(templatePath);
Result:
Hello, World!
Hello, Other World!
This is a nested file.