Skip to content

Generating Text Files

Simon Mourier edited this page Feb 19, 2020 · 1 revision

SoftFluent CodeModeler provides a built-in template engine which you can use to generate text files. You can leverage this engine at different stages of your application:

  • At production time to generate extra text files such as configuration files, reports, or whatever else you may want;

  • At run time, in your application, to generate documents as reports, invoices, contact cards, documents, HTML pages, etc. based on templates.

The Syntax

The default Template syntax is an “ASP.NET-like” syntax, using "[%" and "%]" to surround C# statements.

Here’s a sample text template illustrating the syntax:

[% foreach (string value in values) { %]
 - The value is [%=value%]
[% } %]

The example above will iterate through all values contained in "values" and write the " - The value is xxx" for each value.

Moreover, as illustrated in this example, using "[%=" indicates the template engine to replace a variable by its value.

Using the Template Engine in your Code

Here’s a code snippet which takes as a parameter a template file, passes a Name value as a parameter to the template, and runs it:

using System;
using System.Collections;
using System.IO;
using CodeModeler.Runtime.Windows.Templating;
 
namespace TemplateEngineSample
{
    class Program
    {
        static void Main(string[] args)
        {
             if (args.Length != 1)
            {
                Console.WriteLine("Template file must be specified!");
                return;
            }
            var templateFilePath = args[0];
            var template = new Template();
            template.Load(templateFilePath, TemplateOptions.Default);
            template.AddParameterDirective("Name", typeof(string));
            template.Build();
            using (var fs = new FileStream("Result.txt", FileMode.Create))
            {
                using (var writer = new StreamWriter(fs))
                {
                    var parameters = new Hashtable(); // or dictionary
                    parameters.Add("Name", "From SoftFluent CodeModeler");
                    template.Run(writer, parameters);
                }
            }
        }
    }
}

The text template engine requires a reference to the CodeModeler.Runtime.Windows.dll assembly. This doesn't matter at production time, since CodeModeler references those assembly for you, however, this does matter if you want to use the template engine in your own application to generate text files as we're doing in the example above.

Here's the template we're passing as an argument to the program above:

This is a template sample:
Hello [%=Name%]!!

Running this code by specifying the template above as parameter will create a Result.txt file with the following content:

This is a template sample:
Hello From SoftFluent CodeModeler!!

Since those templates are external to your assembly, if you don’t need extra parameters in them, you can change them as much as you’d like without having to compile and deploy your application over. Therefore, if your accounting department wants a new layout for invoices automatically generated by your applications; all you must do is update the invoice template used by your application.

References and Assemblies

There are two ways to specify extra references and namespaces to your assemblies:

Option 1: Adding the reference or namespace from your template

Use the reference and namespace tags in your templates:

[%@ reference name="System.Configuration.dll" %]: adds a reference to the System.Configuration.dll assembly,

[%@ namespace name="System.Configuration" %]: adds the corresponding "using" statement,

Option 2: Adding the reference or namespace from calling code

Add the needed references and namespaces through the template class:

var template = new Template();
template.AddReferenceDirective(typeof(string));
template.AddReferenceDirective(typeof(System.Configuration.ConfigurationManager));
template.AddNamespaceDirective(typeof(string).Namespace);
template.AddNamespaceDirective(typeof(ConfigurationManager).Namespace);
...

In the end, both ways are equivalent, use the one which suits your needs best.

Clone this wiki locally