How One File Is Processed
When you generate, CodeBaker reads every file of your template and works through it in the same order. This page is the short version: what happens, in which order, and what that means when you write a template.
The six steps
1. It sets aside what must stay exactly as written.
Anything inside {% raw %} … {% endraw %} (or {% verbatim %} … {% endverbatim %}, which is the same thing), and any one-line string such as {{ 'text' }}. From here on, nothing touches it.
2. It applies your replace entries from the config.
Plain text for plain text, for example OldNamespace becomes App.
3. It applies the replace and regex rules written in the file.
A rule works from the line where you declare it, downwards. The lines above it never change.
4. It turns your placeholders into Pebble expressions.
For example ___name___ becomes {{ name }}. Nothing is filled in yet.
5. It reads your CodeBaker: comments.
// CodeBaker: {% if x %} becomes a real {% if x %}. Slot markers stay as you wrote them, and a line that holds only tags leaves no empty line behind.
6. It puts the set-aside text back, and Pebble does the rest. Pebble fills in the values and evaluates conditions and loops, and the file is ready for the preview.
The same thing in one example
The config has replace: { "OldNs": "App" } and placeholders: ["___VAR___"], and the values are x = VALUE and ok = true.
The template file:
// CodeBaker: replace red => blue
use OldNs\Models;
color: red;
<!-- CodeBaker: {% raw %} -->
<p>{{ message }} OldNs red</p>
<!-- CodeBaker: {% endraw %} -->
label = {{ 'OldNs ___x___' }};
// CodeBaker: {% if ok %}
name = ___x___;
// CodeBaker: {% endif %}
What each step does to it:
| Step | Result |
|---|---|
| 1. Set aside | The raw block and the string {{ 'OldNs ___x___' }} are put aside |
2. Config replace | use OldNs\Models; becomes use App\Models; |
| 3. Rules | The replace red => blue line is gone, and color: red; becomes color: blue; |
| 4. Placeholders | name = ___x___; becomes name = {{ x }}; |
| 5. Comments | // CodeBaker: {% if ok %} becomes {% if ok %} |
| 6. Pebble | {{ x }} becomes VALUE, and the condition is true |
The generated file:
use App\Models;
color: blue;
<p>{{ message }} OldNs red</p>
label = OldNs ___x___;
name = VALUE;
Look at the two lines that were set aside: OldNs and red were not replaced inside them, and ___x___ was not filled in.
Three things to remember
- Raw means raw. Nothing inside a raw block, and nothing inside a
{{ 'text' }}string, is read or changed: not your placeholders, notreplace, not rules, notCodeBaker:comments. - A rule only looks down. It never changes the lines above its own line, and it needs no closing line.
- File and folder names are filled in separately, with the same variables and the same
replaceentries.
A few more details that follow from the order:
- A
replacevalue or a rule may contain a placeholder or an expression, such as"OldNs": "___ns___". It is filled in later, in steps 4 and 6. - If your template chose another
syntax, the tags and expressions in all of this are written with its delimiters, for example[% raw %]and[[ name ]].
Files that are not text
An image, a font or an archive in the template comes out byte for byte, and nothing in it is read or replaced. CodeBaker takes a file for one of these when it has a NUL byte near its start, or is not valid text in its encoding. The preview shows it as [binary file, N bytes], and the IDE's Undo cannot revert it (use Git). Text is read and written in the encoding the IDE finds for the file (UTF-8 unless it, or the project, says otherwise), including a byte order mark, never in the encoding of the operating system.
Where each step is explained
| Step | Read more |
|---|---|
| 1. Set aside | Preserving code with raw blocks |
2. Config replace | replace |
| 3. Rules | Inline & Block Replace |
| 4. Placeholders | placeholders |
| 5. Comments | How CodeBaker comments work |
| 6. Pebble | Template Basics and Inflection & Case Filters |
For the whole picture, from the menu click to the written files, see How It Works.