Skip to content

Template Producer

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

The template producer is based on CodeModeler’ template engine to allow developers to generate text files at production time.

Note: All text files are supported. Moreover, the template producer also fully supports specific file formats such as RTF and HTML files.

Since it uses the template engine, the following basic rules are observed:

  • Template file names must be decorated by the [Template] prefix,

  • Files or folders decorated by the [Skip] prefix will be skipped,

  • All files contained in a template directory are copied in the output directory unless they're explicitly marked to be skipped.

 This topic describes how to configure this producer to generate custom text files based on the design model and on template files. 

The Template producer is one of the template producers shipped out-of-the-box with SoftFluent CodeModeler, you can configure this producer to generate any text base files such as:

  • custom documentation files,

  • custom configuration files,

  • etc.

The following example demonstrates how to use the Template producer to generate HTML documentation for the project. Here is a model with 3 entities are associated resources in a “myDoc” custom class name:

Template Producer - Picture 296

First, we need to create a template file named that contains this plain text:

<html>
    <body>
        <h1>Project : [%=Producer.Project.DefaultNamespace %]</h1>
        <table border=1>
                    <tr>
                           <th>Name</th>
                           <th>English</th>
                           <th>French</th>
                    </tr>
        [% foreach(Entity e in Producer.Project.Entities) { %]
            <tr>
                <th>[%=e.Name%]</th>
                <td>[%=e.GetMessageValue("myDoc", new System.Globalization.CultureInfo("en-US"))%]</td>
                <td>[%=e.GetMessageValue("myDoc", new System.Globalization.CultureInfo("fr-FR"))%]</td>
            </tr> 
        [% } %]
        </table>
    </body>
</html>

Note: Using the template producer you can access the in-memory representation that CodeModeler inferred of your model. Then using the CodeModeler API, you can access all concepts of your model.

The template above iterates throughout all entities of the project to print each entity name and associated resources/messages.

To use this template, you can save its content in a specific folder in the Files folder node (how to use the Files folder node is explained in the Project Hierarchy chapter) and name the file [Template]Doc.html(the [Template] prefix is important, it instructs the producer to consider the file as a compilable template). Once the file has been added to the project (1) you can add a producer (2) using the “Add New Producer” context menu on the Producers folder node. Choose the template producer (3). Choose the Source Directory (4) to point to the Templates folder (5):

Template Producer - Picture 297

Now, you can choose a “Target Directory” and/or a target project, if you let it blank, the result files will go to the solution folder.

After the build, the Template producer has created a “Doc.html” file (the file name is the same as the source without the [Template] prefix) in the target directory/project:

<html>
    <body>
        <h1>Project : Commerce</h1>
        <table border="1">
                    <tr>
                           <th>Name</th>
                           <th>English</th>
                           <th>French</th>
                    </tr>
        
            <tr>
                <th>Customer</th>
                <td>Customer</td>
                <td>Client</td>
            </tr> 
        
            <tr>
                <th>Order</th>
                <td>Order</td>
                <td>Commande</td>
            </tr> 
        
            <tr>
                <th>Product</th>
                <td>Product</td>
                <td>Produit</td>
            </tr> 
        
        </table>
    </body>
</html>
Clone this wiki locally