Yellicode
The extensible code generator
- Getting started
- Tutorial - Model driven basics
- Tutorial - Full stack Angular application
- Fundamentals
- How-to
- Modeler
- Reference
Passing Arguments to a Template
You might want to reuse the same template in a code generation project: you configure multiple instance of the same template file in your codegenconfig.json
file and, for example, configure a different model file for each instance. But: how would you provide instance-specific data to each template? For example, you might want to use a different output file name for each configured instance. This is where you can use template arguments.
Template arguments are custom JSON objects that you configure in your codegenconfig.json
file using the templateArgs
key. For example:
{ "templates": [ { "modelFile":"./data-model", "templateFile": "./classes.template.ts", "templateArgs": { "outputFile": "./data/data-classes.cs" } }, { "modelFile":"./api-model", "templateFile": "./classes.template.ts", "templateArgs": { "outputFile": "./api/api-classes.cs" } }, ] }
The templateArgs
key can have any arbitrary JSON object as value. Any value that you provide here is made available in your template using the Generator.templateArgs
field:
import { Generator } from '@yellicode/templating'; const outputFile = Generator.templateArgs.outputFile; Generator.generateFromModel({ outputFile: outputFile }, (writer: TextWriter, model: elements.Model) => { // Template code goes here... });