Skip to content

Expression language

Martin Slezák edited this page Jun 10, 2024 · 2 revisions

Table of Contents

Expressions

Expressions are used for parameterization of the templates. Expression can be used in any file, that has its action in makeit.json (template config) set to Make.

Expressions are enclosed in {{ and }} and can contain variables, literals or operators. You can also use parenthesis (( and )) for changing the presedence of the operators.

Variables

Naming convention

Variable names must start with alphabetic character or underscore and can be followed by any alphanumeric character or underscore.

Definition

Variables can be defined in template config (makeit.json in template folder) or they can be supplied by command-line arguments. Variables in the expressions are then expanded to the set values.

Internal variables

There are also some internal variables in the expression language and more might be added in the future:

  • _PNAME: project name based on project directory
  • _PDIR: project directory
  • _OS: operating system

Example

Let's say we have variable with name greet and value of "hello" (more about variable definitions later). Then this code:

{{ greet }}

would expand to:

hello

Literals

Literals are enclosed in double quotes ("). They support escape sequences as well, starting with backslash (\):

  • \n: newline
  • \r: carriage return
  • \t: tabulator
  • \\: backslash
  • \": double quotes
  • Other sequences are expanded to character following backslash

Example

When we have this code:

{{ "Hello makeit!" }}

it would get expanded to:

Hello makeit!

Operators

Operator +

The plus operator (+) is used for literal and variable concatenation. It allows you to combine two literals/variables into a single literal.

Syntax

The syntax is:

{{ EXPR1 + EXPR2 }}

where EXPR1 and EXPR2 are the values to be concatenated.

Example

When we have this code:

{{ "Hello " + _OS }}

it would get expanded to (in case of using linux):

Hello linux

Operator ==

The equality operator (==) is used to compare two values for equality. It evaluates to true if the values on either side of the operator are equal, and false otherwise.

Syntax

The syntax is:

{{ EXPR1 == EXPR2 }}

where EXPR1 and EXPR2 are the values or expressions to be compared.

Example

When we have this code:

{{ "equals" == "equals" }}
{{ "not equals" == "negative" }}

it would get expanded to:

true
false

Operator ?:

The ternary conditional operator (?:) is a shorthand for an if-else statement. It evaluates a condition and returns one of the two values depending on whether the condition is true or false.

Syntax

The syntax is:

{{ EXPR1 ? EXPR2 : EXPR3 }}

where EXPR1 is the condition to evaluate, EXPR2 is the value returned if the condition is true, and EXPR3 is the value returned if the condition is false.

Example

When we have this code:

{{ "cond" == "cond" ? "equals" : "not equals" }}
{{ "cond1" == "cond2" ? "equals" : "not equals" }}

it would be expanded to:

equals
not equals

Operator ??

The null coalescing operator (??) is used to provide a default value when an expression evaluates to null. It returns value of the first operand if it is not null. Otherwise, it returns the value of the second operand.

Syntax

The syntax is:

{{ EXPR1 ?? EXPR2 }}

where EXPR1 is the expression to evaluate and EXPR2 is the default value to return if EXPR1 is null

Example

Let's say we have variable valuable set to "diamond" and this code:

{{ valuable ?? "default valuable" }}
{{ ore ?? "default ore" }}

it would be expanded to:

diamond
default ore