Create a Custom Template
ADL comes with a library of built-in templates, but sometimes you need something specific to your project. This guide walks you through creating a custom Handlebars template from scratch.
What you’ll build
Section titled “What you’ll build”A simple template that generates a SQL CREATE TABLE statement for a data object. Along the way, you’ll learn the key Handlebars concepts you need to write your own templates for any output format.
Prerequisites
Section titled “Prerequisites”- An ADL project with at least one data object that has Data Items (columns) defined
- Familiarity with the basics of How ADL Works
Step 1: Create a new template
Section titled “Step 1: Create a new template”- Navigate to the Templates screen.
- Create a new template and give it a descriptive name, like “Simple Table DDL”.
- The built-in code editor will open, ready for you to write your template.
Step 2: Write the basic structure
Section titled “Step 2: Write the basic structure”Start with the static parts — the SQL syntax that will be the same for every table:
CREATE TABLE [{{targetDataObject.name}}] ();The {{targetDataObject.name}} expression is a Handlebars placeholder. When ADL processes this template against a data object, it replaces this with the actual object name.
Step 3: Loop through columns
Section titled “Step 3: Loop through columns”Now add the columns by looping through the Data Items:
CREATE TABLE [{{targetDataObject.name}}] ({{#each targetDataObject.dataItems}} [{{name}}] {{dataType}}{{#unless isNullable}} NOT NULL{{/unless}}{{#unless @last}},{{/unless}}{{/each}});Here’s what each part does:
{{#each targetDataObject.dataItems}}— Starts a loop over all columns in the data object.[{{name}}]— Outputs the column name in square brackets.{{dataType}}— Outputs the column’s data type.{{#unless isNullable}} NOT NULL{{/unless}}— AddsNOT NULLif the column doesn’t allow nulls.{{#unless @last}},{{/unless}}— Adds a comma after each column except the last one.{{/each}}— Ends the loop.
Step 4: Preview the output
Section titled “Step 4: Preview the output”- Save your template.
- Navigate to the Code Preview screen.
- Select a data object and your new template.
- Check the output — you should see a valid
CREATE TABLEstatement with all the columns from your data object.
Step 5: Enhance the template
Section titled “Step 5: Enhance the template”Once the basics work, you can add more features:
Add a primary key
Section titled “Add a primary key”{{#if targetDataObject.businessKeyDefinitions}} CONSTRAINT [PK_{{targetDataObject.name}}] PRIMARY KEY ( {{#each targetDataObject.businessKeyDefinitions}} [{{businessKeyComponentId}}]{{#unless @last}},{{/unless}} {{/each}} ){{/if}}Use helper functions
Section titled “Use helper functions”ADL provides custom Handlebars helpers for common operations. For example:
{{stringReplace name "_" " "}}— Replace underscores with spaces{{now "yyyy-MM-dd"}}— Insert the current date{{lookupExtension targetDataObject.extensions "SchemaName"}}— Look up a custom extension value
Add conditional logic
Section titled “Add conditional logic”{{#if targetDataObject.notes}}-- {{targetDataObject.notes}}{{/if}}Step 6: Map and generate
Section titled “Step 6: Map and generate”Once you’re happy with the template:
- Go to the Data Objects screen.
- Select a data object and add a template mapping pointing to your new template.
- Use the Code Generator to generate the output.
Tips for writing templates
Section titled “Tips for writing templates”- Start simple — Get the basic structure working before adding complexity.
- Preview often — The Code Preview screen is your best friend for testing templates.
- Study the built-in templates — They’re a great reference for common patterns and Handlebars techniques.
- Check the Handlebars Helpers — The custom helpers can save you a lot of effort.
- Keep templates focused — One template, one type of output. It’s easier to maintain several small templates than one giant one.
What’s next?
Section titled “What’s next?”- Handlebars Helpers — Explore all the custom helper functions
- Template Library — Browse the built-in templates for inspiration
- Templates & Code Generation (Concepts) — Deeper dive into how the template system works