# Template Expressions

Many of command/assertion parameters accept templates. So you can use template variables and expressions. For example, `page.goto("https://github.com/{{ USER }}/puppetry/")` or `TARGET.type("name{{ counter() }}")`

## Template variables

By using **Settings/Template Variables** panel we can define a set of variables per environment. For an instance, we can declare a separate target app URL for every environment (test.acme.com, stage.acme.com, [www.acme.com](http://www.acme.com)). Before running/exporting test project we specify the desired environment and the corresponding template tags will be replaced with the value (URL) given for that environment.

### Managing Variables

To edit template variables click on **Project/Template variables** in the main menu:

![Template variables in the Project menu](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LuIbs7-qh-OBvg6QpLN%2F-LuIcC3Jv-ERr6Qi8nGZ%2Ftemplate-variables.png?alt=media\&token=41cb881a-0d9a-468d-9e1b-31c1def111d1)

Under the table Template variables you will find a manageable table with variables (by default it's empty):

![Template variables panel](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LuIbs7-qh-OBvg6QpLN%2F-LuIdHUkW1TsYFmUsRaH%2Ftemplate-vars2.png?alt=media\&token=c9c670a4-62ac-44e8-973d-eeb01e2ae970)

You can use this UI the same way to do with targets, suits, and test cases. The difference is that the displayed variables belong to the selected environment:

![Selecting destination environment](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhKRkJBUuFiu8R0CcwS%2F-LhKRoSTVQCnEoU-Pv-y%2Fp2-variables-env-select.png?alt=media\&token=6dcd95a2-23ed-4e29-a987-bd91c9d24ef3)

As you add a new variable let's say to **test** environment it appears in all available environments. But when you update it's value, the value stays unique per environment.

Also note the **edit** action link next to the environment selector. When you click it you get a modal window where you can manage the list of environments:

![Editing environments](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LuIbs7-qh-OBvg6QpLN%2F-LuIdWJ0i9h6hzKkP17K%2Ftemplate-vars4.png?alt=media\&token=d4ffc650-b0d9-4cf6-b470-db57fdbe56ec)

After we assigned the variables and running the tests (or exporting them for CI) in the modal window we can choose what environment we target:

![Destinating test run for a given environment](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LuIdb0BIentuQ_Ka4Zl%2F-LuIe8OA-vp_8rMzfdJT%2Ftemplate-vars5.png?alt=media\&token=2555f1b0-d0af-437c-bb17-13d55a685ac2)

### Template Syntax

#### Syntax

```
{{ VARIABLE_NAME }}
```

The tag makes Puppetry injecting the variable value assigned for the selected environment into the container string

#### Examples

```
{{ BASE_URL }}/api/{{ API_VERSION }}/method
```

{% hint style="info" %}
Template variables can be also assigned dynamically. E.g. you can use `page.assignVar` to define a variable by using expressions or `page.assignVarRemotely` to define a variable by using REST API ([learn more](https://docs.puppetry.app/testing-emails)).
{% endhint %}

## Template Expressions

Template expressions are basically functions that we may want to use for building template string.

Test step parameters supporting templates provided with a helper widget, which you can you to autofill the field:

![Template helper widget](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhKSWMFwMhSNQszyGo2%2F-LhKU2k4JJRqBMrO4ovS%2FP2-template-helper.png?alt=media\&token=4f290e9c-453c-405c-ba98-438ff1ad74e5)

### Environment Variables

#### Syntax

```
{{ env( NAME ) }}
```

Function returns system [environment variable](https://en.wikipedia.org/wiki/Environment_variable) by a given name

#### Examples

```
{{ env( "SECRET" ) }}
```

### Counter

#### Syntax

```
{{ counter() }}
```

Function increments every time it called, but is unique per command/assertion&#x20;

#### Examples

```
joe.{{ counter() }}@acme.com
```

This template resolves in email addresses <joe.1@acme.com>, <joe.2@acme.com> and so on

### Uniqid

#### Syntax

```
{{ uniqid() }}
```

Function generates a unique id like `8fzk2g5fpkb`

#### Examples

```
{{ uniqid() }}
```

### Iterate

#### Syntax

```
{{ iterate( LIST ) }}
```

Function iterates through a given list. It starts from the beginning as it reaches the end

#### Examples

```
{{ iterate(["sansas@got.com", "yarag@got.com", "gendryb@got.com"]) }}
```

This template resolves in <sansas@got.com>, <yarag@got.com>, <gendryb@got.com>, <sansas@got.com>, ...

### Random

#### Syntax

```
{{ random( LIST ) }}
```

Function picks a random value out of a given list.

#### Examples

```
{{ random(["sansas@got.com", "yarag@got.com", "gendryb@got.com"]) }}
```

### HtmlOf

#### Syntax

```
{{ htmlOf( TARGET ) }}
```

Function extracts content (innerHTML) of a given TARGET.

#### Examples

```
{{ htmlOf("FOO") }}
```

### AttributeOf

#### Syntax

```
{{ attributeOf( TARGET, ATTRIBUTE_NAME ) }}
```

Function retrieve attribute value of a given TARGET.

#### Examples

```
{{ attributeOf("FOO", "href") }}
```

### PropertyOf

#### Syntax

```
{{ propertyOf( TARGET, PROPERTY_NAME ) }}
```

Function retrieve property value of a given TARGET.

#### Examples

```
{{ propertyOf("FOO", "checked") }}
```

### Faker

#### Syntax

```
{{ fake( FAKER_METHOD, LOCALE ) }}
```

Function generates dummy data, by using [Faker.js methods](https://github.com/marak/Faker.js)

#### Examples

```
 {{ faker("address.streetSuffix", "en_GB") }}
```

### Eval

#### Syntax

```
{{ eval( "JavaScript code" ) }}
```

Function injects the given JavaScript into the suite code forming the string.

#### Examples

```
{{ eval("process.cwd()") }}
```

resolves into

```
`${ process.cwd() }`
```

NPM modules "fs", "path" and "os" as well as Command API (<https://docs.puppetry.app/command-api>) are available for evaluation.&#x20;

Quotation marks in the JavaScript code have to be escaped.
