Skip to main content

Template Basics

A template is a folder of files. This page shows what is in it and the order in which to learn to write one: each lesson adds one idea and ends with a complete example that you can download and install.


What is a template?​

A CodeBaker template is a folder of files and subfolders that serves as a blueprint. Think of it as a snapshot of a feature that can be replicated with different values.

.CodeBaker/
└── LaravelCRUD/ # The template
├── codebaker.config.json5 # Configuration and variables
├── codebaker.advanced.json5 # (Optional) the mapping file: where each file goes and how
├── Controller.php # Template files, with their own extensions
├── Model.php
├── views/
│ ├── index.blade.php
│ └── show.blade.php
└── tests/
└── FeatureTest.php

By default every template file is copied, with its relative path, into the folder you right-clicked. The two codebaker.*.json5 files are not copied: they describe the template. Both are JSON5, which is JSON with comments and trailing commas allowed (see Why JSON5?). To place files elsewhere, see Advanced Templates.


Why JSON5?​

Both files are JSON5, which is JSON made friendlier for files people write by hand:

  • Comments. Explain a variable or a mapping entry right next to it. The mapping file relies on this too: loop and condition tags written as // CodeBaker: {% for %} comments keep the file valid JSON5, so your IDE does not underline it.
  • Trailing commas and unquoted keys. Fewer syntax errors when you add or reorder entries, and cleaner Git diffs.
  • Familiar and well supported. JSON5 is a superset of JSON, and JetBrains IDEs highlight and check .json5 files out of the box, in line with the idea that everything in a template stays an ordinary file your IDE understands.

The lessons​

Read them in this order. Each page builds on the one before.

  1. Values and Variables: where the fields of the dialog come from, what JSON is and how to hold a list, how a value is computed from other values, and the placeholder style that suits your language. It is in the Start here section, right after Your First Template.
  2. Conditions and Loops: generate a part of a file only when something is true, or once for each item of a list.
  3. Keeping the Template Valid for Your IDE: hide the conditions and loops in comments, so the IDE still reads the template as code.
  4. Names and Filters: turn one name into every spelling a feature needs: class, table, route, constant.
  5. Replacing Real Values: keep a real value, such as a color, in the file and swap it at generation time.
  6. Escaping Text: keep text that contains {{ }} or {% %} exactly as it is.
  7. Generating JSON: write a template as JSON5 and get plain JSON.

After that, Advanced Templates covers the mapping file: renaming and moving files, repeating a file for every item of a list, writing a file once, and updating one block of a file that already exists. The Pebble Syntax page is a cheat sheet of the template language, and the Configuration Reference lists every option of the config file.

Not sure where to start? Your First Template builds a small one from scratch in ten minutes, and Turn Your Own Code into a Template starts from code you already have.