Executing an ad-hoc template

Generating code using the LLBLGen Pro designer primarily focuses on presets and template bindings. While this system is fine for large sets of template files and can handle a large variety of specifics per template, it's a bit complex when you just want to run one or more additional templates besides the ones in a template group. To do just that, you can use the ad-hoc templates feature of the designer.

.adhoctemplates files

To run an ad-hoc template, you have to specify to the designer where the template is, what output file it will generate and which elements it should be used with. This is done in .adhoctemplates files. These have the following format:

<adhocTemplates usedByModelType="">
    <template filename="" outputFilename="" emitType=""/>
  • usedByModelType This is an optional filter directive to which model type the template applies. It's not run when the current model being code generated for (e.g. the Entity Model) doesn't match with the value specified here. usedByModelType can be set to the following values. Not specifying usedByModelType will use 7 as the value and the adhoc templates are applied to all models when generating code:
    • 1. The Entity model (code for the ORM target framework)
    • 2. The Relational model data (exported DDL SQL scripts)
    • 4. Any derived model (code for any derived model target framework)
    • 7. All (default).
  • filename is relative to the .adhoctemplates file location, which is the same as with .templatebindings files. You can specify an absolute path if you want.
  • outputFilename can be relative to the Destination root folder specified in the code generation task, and it then has to start with .\ or ..\. All other paths are considered absolute paths. The outputFilename can contain macros like with presets: [elementName], [time], [containerName] and [groupName]. It has to contain the right file extension, as there's no output language directive.
  • emitType is optional, if omited the default is generic, which means the template is run once. Possible values are:
    • allEntities. The engine will walk all entities defined in the project and will for each entity execute the template specified.
    • allTypedLists. The engine will walk all typedlists defined in the project and will for each typed list execute the template specified.
    • allTypedViews. The engine will walk all typed views defined in the project and will for each typed view execute the template specified.
    • actionSPCalls. The engine will execute the specified template for all action stored procedure calls together.
    • allTvfCalls. The engine will walk all TvfCalls defined in the project and will for each TvfCall execute the template specified.
    • allValueTypes. The engine will walk all value types defined in the project and will for each value type execute the template specified.
    • retrievalSPCalls. The engine will execute the specified template for all retrieval stored procedure calls together.
    • allModelViews. The engine will walk all modelviews defined in the project and will for each modelview execute the template specified.
    • allRootDerivedElements. The engine will walk all root derived elements defined in the active Derived Model and will for each root derived element execute the template specified.
    • generic The code emitter will simply use the specified template and the current project and generate code. The statements in the template are executed for the project, no elements inside the project are used as a set to execute the template multiple times for multiple files. A single file is the result.


    <template filename="C#\typeInfoListPage.lpt" outputFilename="ProjectInfo.htm" emitType="generic"/>
    <template filename="C#\specialEntity.template" outputFilename="MyCustomCode\[elementName]Special.cs" emitType="allEntities"/>

The above .adhoctemplates file defines a .lpt template which generates a html file and it's run only once. It also defines a TDL template,specialEntity.template, which will generate for each entity available to the code generation task a file MyCustomCode\entityNameSpecial.cs.

Self contained templates

As there is no way to specify a templateID and all templates have an output filename, there’s no way through ad-hoc templates to specify a template which contains code for other templates nor templates which should be included in other templates: all templates should be self-contained, they can’t rely on another template in the ad-hoc templates file, as the templateID they need to refer to the other template isn’t known.

If you need this functionality, it's better to create a .templatebindings file and a preset and run the templates using the normal template system. For this, please see the SDK documentation for details.

Location of .adhoctemplates files

The .adhoctemplates files are placed in any additional templates folder, like the default additional templates folder My Documents\LLBLGen Pro\vX.Y\Templates where X.Y is the LLBLGen Pro designer version, e.g. 5.6. Alternatively, you can specify an additional templates folder in the project settings under 'General'. The .adhoctemplates files are read at project load and when code generation meta-data is refreshed using the Tools -> Refresh Code Generation Meta-data menu item.