Skip to content

Template language

create()’s path and template are both template strings, rendered against the same single options object. This page is the reference for the language you write inside them. For where rendering happens, templating the output path, and what to do when a render fails, see the templates guide.

Templates use the delimiters {= and =}, not the more familiar {{ }}. Inside the delimiters you reference your options through a leading dot:

You write It means
{= .name =} the name option
{= .user.address.city =} walk nested objects: user → address → city
{= . =} the current context itself (useful inside range/with)

Array- and object-valued options pass as plain native values; you never hand-encode them.

Two small details:

  • Emit a literal {=. There is no backslash escape. To print the delimiter itself, wrap it in a quoted string action: {= "{=" =} renders the literal text {=.
  • Comments. {= /* a note to yourself */ =} never produces output.

Pipes transform a string value. You apply one with |:

Pipe "userProfile" becomes Rule
upper USERPROFILE uppercase every letter
lower userprofile lowercase every letter
capitalize UserProfile uppercase the first letter only
dasherize user-profile split into words, lowercase, join with -
underscore user_profile split into words, lowercase, join with _
camelize userProfile PascalCase, first word lowercased
classify UserProfile PascalCase including the first word

Chain pipes left to right. Order is observable:

{= .name | underscore | upper =}

"MyComponent" → my_component → MY_COMPONENT.

Three rules to remember:

  • Pipes are a closed set. There are these 7 and no others; an unknown pipe name is an error that lists the valid names.
  • Pipes take strings only. Applying a pipe to a number (or any non-string) is an error naming the pipe and the offending kind; the engine never silently stringifies a value.
  • classify does not singularize. Unlike the classify in Rails or Angular, a plural stays plural: users becomes Users, not User. If you need the singular, pass the singular in your options. There is no pluralize pipe either.

Blocks are opened by a keyword and closed by {= end =}; there are no braces {}. There is no for, no .map(), no .forEach(), no arrow function:

// JavaScript
methods.map(m => ` ${m.name}() {}\n`).join("")
{= range .methods =} {= .name =}() {}
{= end =}

with methods: [{ "name": "load" }, { "name": "save" }] this emits one line per element. Array order is preserved exactly; the engine never reorders your list.

Inside the block, the dot . becomes the current element. There is no parameter name unless you ask for one: . is the item, and .name reads the name field of the current item:

{= range .items =}[{= . =}]{= end =}

over ["x", "y"] → [x][y].

To get the index and the value, declare them with $ variables and :=:

{= range $i, $v := .items =}{= $i =}:{= $v =} {= end =}

over ["a", "b"] → 0:a 1:b .

JavaScript Template
for (const m of methods) { … } {= range .methods =} … {= end =} (item is .)
items.map(x => …) {= range .items =} … {= end =}
items.forEach((v, i) => …) {= range $i, $v := .items =} … {= end =}
Object.entries(obj).map(([k, v]) => …) {= range $k, $v := .obj =} … {= end =}

Ranging over an object iterates its keys in sorted order (deterministic); ranging over an array keeps the array’s own order.

Putting it together, one options object drives both the path and a loop in the content:

create("src/{= .name | dasherize =}/{= .name | dasherize =}.component.ts", {
template:
"export class {= .name | classify =}Component {\n" +
"{= range .methods =} {= .name =}() {}\n{= end =}}\n",
options: {
name: "userProfile",
methods: [{ name: "load" }, { name: "save" }],
},
});

Path: src/user-profile/user-profile.component.ts

export class UserProfileComponent {
load() {}
save() {}
}

Conditionals: if, but the operators come first

Section titled “Conditionals: if, but the operators come first”

A comparison is a function call with the operator name first, then its operands, the opposite of JavaScript’s infix style:

JavaScript Template
a === b eq a b
a !== b ne a b
a < b lt a b
a > b gt a b
a && b and a b
a || b or a b
!a not a

So an equality check is:

if (kind === "primary") { … } // JavaScript
{= if eq .kind "primary" =}…{= end =} // template — "eq" first, then the two operands

Combine conditions by nesting the calls in parentheses. There is no && / ||:

if (a > 1 && b < 5) { … } // JavaScript
{= if and (gt .a 1.0) (lt .b 5.0) =}…{= end =} // template

else and else if:

{= if eq .kind "primary" =}main
{= else if eq .kind "secondary" =}alt
{= else =}other
{= end =}

Two data helpers round out the operator set. Both are written operator-first as function calls, never piped:

Operator Meaning JS equivalent
len length of a string, array, or object .length / Object.keys().length
index element or key lookup arr[0] / obj["k"]
{= if gt (len .items) 0 =}has items{= end =}
{= index .items 0 =}

The guaranteed core is eq, ne, lt, gt, and, or, not, len, index. Build every condition from these and it will keep working across engine versions. (le and ge work today but are outside the guaranteed core; for a stable guarantee, invert with not (gt …) / not (lt …).)

Truthiness — almost JS-falsy, with one trap

Section titled “Truthiness — almost JS-falsy, with one trap”

{= if .x =} with no operator tests whether .x is “empty”. false, 0, "", and null are falsy, as in JS, but an empty array or object is falsy here too, where JS treats both as truthy. That’s convenient: to check “does this list have items” you write {= if .items =} directly, with no .length.

with rebinds the dot . to a nested object for the block, so inside you write .name and .email instead of .user.name / .user.email:

{= with .user =}{= .name =} ({= .email =}){= end =}

$name declares a variable with := (same as a range index/value). Assign a piped value once and reuse it instead of repeating the pipe:

{= $base := .name | dasherize =}
{= $base =}.component.ts
{= $base =}.component.spec.ts

A line that contains only a control directive (range, if, else, end, with, an assignment, or a comment) is removed whole (its indentation and trailing newline) before rendering. So this template:

{= range .methods =}
{= .name =}
{= end =}

produces one clean line per method, with no blank lines from the range/end lines. A genuinely empty line, with no directive on it, is always preserved, and a line holding a field or pipe expression ({= .name =}) is never trimmed.