Block Helpers In Template Engine
Beeceptor's template engine supports a range of block operators for flow control and customizing mock responses. Explore examples of if/else, switch, and repeat/for-loop constructs to see how you can tailor responses to your needs.
Note: Within a mock rule, the template engine is off by default. You need to explicitly mark a rule to enable usage Handlebars templates before start using block constructs.
Switch
The switch block allows you to create conditional responses by matching on specific input values, like query parameters, or an attribute from request body.
Syntax
- Standard Syntax
{{#switch expression}}
{{#case 'value1'}}content-1{{/case}}
{{#case 'value2'}}content-2{{/case}}
{{#default}}default-content{{/default}}
{{/switch}}
Let’s break down each component:
#switch
: This starts the switch block. The expression can be a static value or an expression to be evaluated. Its value determines which#case
block will be executed.#case 'valueX'
: Each#case
block checks if the expression matchesvalueX
. If it does, the content within this block (e.g.,content-1
orcontent-2
) is rendered in the response.#default
: This is an optional block that provides a fallback option. If the expression does not match any of the specified cases, the content inside the#default
block is rendered.
Video Demo
Examples
- Switch with two cases and default content
- Response based on user's role in a POST request
The following template returns different name based on given id
in the query parameter. The switch
statement handles two cases and a default value.
Template used:
{
"name":
{{#switch (queryParam 'id')}}
{{#case '1'}}"Alex"{{/case}}
{{#case '2'}}"Jenny"{{/case}}
{{#default}}"Some-Name"{{/default}}
{{/switch}},
"status": "The switch/case are amazing!"
}
Request triggered:
GET /find-all-users?id=1
Response generated:
The name as Alex
is picked because from the block {{#case '1'}}"Alex"{{/case}}
.
{
"name": "Alex",
"status": "The switch/case are amazing!"
}
The following example returns a response object for user registration API. It compares a value from request payload's role
variable and returns a list of permissions.
Template used:
{
"username": "{{body 'username'}}",
"role": "{{body 'role'}}",
"welcomeMessage":
{{#switch (body 'role')}}
{{#case 'admin'}}
{
"message": "Welcome, Admin {{body 'username'}}! You have full access to the system.",
"permissions": ["read", "write", "delete"]
}
{{/case}}
{{#case 'editor'}}
{
"message": "Welcome, Editor {{body 'username'}}! You can edit content and manage submissions.",
"permissions": ["read", "write"]
}
{{/case}}
{{#case 'viewer'}}
{
"message": "Welcome, Viewer {{body 'username'}}! You can browse and read content.",
"permissions": ["read"]
}
{{/case}}
{{#default}}
{
"message": "Welcome, {{body 'username'}}! Your role is not recognized, please contact support.",
"permissions": []
}
{{/default}}
{{/switch}}
}
Request triggered:
POST /users
{
"username": "johndoe",
"role": "admin"
}
Response generated:
The username as johndoe
is picked from request payload.
{
"username": "johndoe",
"role": "admin",
"welcomeMessage": {
"message": "Welcome, Admin johndoe! You have full access to the system.",
"permissions": ["read", "write", "delete"]
}
}
If & Else
The if
helper in Beeceptor allows you to conditionally emit content in HTTP responses. When you use this helper, Beeceptor evaluates its argument. If the result is false
, undefined
, null
, an empty string (""
), zero (0
), or an empty array ([]
), the block of content associated with this if helper will not be displayed.
Syntax
- IF
- IF and ELSE
{{#if expression}}
content
{{/if}}
Here, the content
is the text block will be returned in the HTTP response only when the expression is satisfied. The result of the expression should be non-empty and non-zero to be satisfied. Refer to the examples on how to build expression logic.
{{#if expression}}
content-when-expression-is-satisfied
{{else}}
content-when-expression-is-not-satisfied
{{/if}}
Here, the content-when-expression-is-satisfied
is the text block will be returned in the HTTP response when the expression is satisfied. Else content-when-expression-is-not-satisfied
is placed.
You can build complex checks for expression. For instance,
- to check the presence of a field in a POST request's body, use an expression like
(body 'status')
. This parses the request body as JSON and evaluates the status field's value. - to check the value of a field in the POST request's body, use a sub-expression
(eq 'paid' (body 'status'))
.
Refer to a full list of supported operators, syntax and example here.
Examples
- Compare a JSON field's value
- Check presence of a JSON field
In the following example, the template checks the order status, and returns additional fields in JSON. You can see a conditional field amountToCollect
. When the order status is paid, amountToCollect
is set to 0, else it is equal to the order amount.
Template used:
{
"customerId": "{{body 'customerId'}}",
"productId": "{{body 'productId'}}",
"quantity": {{body 'quantity' '1'}},
"status": "{{body 'status'}}",
"amount": {{body 'amount'}},
{{#if (eq 'paid' (body 'status'))}}
"amountToCollect": 0,
{{else}}
"amountToCollect": {{body 'amount'}},
{{/if}}
}
Payload for the POST request:
{
"customerId": "1234",
"productId": "PRODUCTID",
"quantity": 3,
"status": "paid",
"amount": 30
}
Response generated:
{
"customerId": "1234",
"productId": "PRODUCTID",
"quantity": 3,
"status": "pending",
"amount": 30,
"amountToCollect": 0,
}
When the status
field in the request body has value paid
, the template engine emits "amountToCollect" : 0
. Else, the value specified in the amount
field is replicated for amountToCollect
.
In the following example, the if block only checks for non-empty or non-null value. If the request payload has a value for the status
field, the contents of the block is emitted in the HTTP response.
Template used:
{
"customerId": "{{body 'customerId'}}",
{{#if (body 'status')}}
"orderConfirmed": "true",
{{else}}
"orderConfirmed": "false",
{{/if}}
}
Payload for the POST request:
{
"customerId": "1234",
"status": "paid"
}
Response generated:
{
"customerId": "1234",
"orderConfirmed": "true"
}
Technical Details
Here's a detailed technical breakdown of how the If block functions:
- Checkout the list of supported operators on this page. Please reach out to Support Team for additional operators support.
- The If clause lets you extract values from query parameters, path, headers, or the request body. Use the expression clause as shown in the above examples.
Repeat
Beeceptor adds an advanced feature known as the Repeat block in the template engine. This allows you to simulate loop constructs while generating mock data. This feature is particularly beneficial when creating a response with JSON arrays, GET API call to retrieve objects or search results in an API response.
Syntax
- Fixed iterations
- Random iterations
- Skipping comma
{{#repeat count}}
content
{{/repeat}}
Here, the content
is the text block to be repeated for the given count
times. The argument count
has to be a number. A comma will be added between consecutive content blocks.
{{#repeat min-count max-count}}
content
{{/repeat}}
Here, the content
is the text block to be repeated for random times. The random count is picked between the given min and max counts. Each request will have variable iterations. A comma will be added between consecutive content blocks.
{{#repeat count comma=false}}
content
{{/repeat}}
When you explicitly set comma=false
, Beeceptor will not add a comma between consecutive sections. Refer the examples below.
Examples
- Simple Loop
- Random search results
- Fake Users (JSON)
- Fake Users (CSV)
Generating a JSON array of three users objects.
Template used:
[
{{#repeat 3}}
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{{/repeat}}
]
Response generated:
[
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
}
]
Creating a random array of JSON objects is straightforward. Notice how the @index variable is used to automatically generate an ID field. Don't worry about the commas; even if you don't include them in the template, the template engine will intelligently add them where needed.
Template used:
[
{{#repeat 0 10}}
{
"id": {{@index}},
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
}
{{/repeat}}
]
Response generated:
[
{
"id": 0,
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{
"id": 1,
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{
"id": 2,
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
},
{
"id": 3,
"name": "Dave",
"address": "45 Brookfield",
"country": "US"
}
]
This example demonstrates how to merge the Fake Data generation syntax with the repeat
block. With this approach, you can easily produce a large volume of fake data efficiently.
Template used:
[
{{#repeat 5}}
{
"id" : {{@index}},
"name": "{{faker 'name.firstName'}} {{faker 'name.lastName'}}" ,
"address": "{{faker 'address.streetAddress'}}",
"country": "{{faker 'address.country'}}",
"phone": "{{faker 'phone.phoneNumber'}}"
}
{{/repeat}}
]
Response generated:
[
{
"id" : 0,
"name": "Nelda Dickens" ,
"address": "1209 Jeromy Lodge",
"country": "Tajikistan",
"phone": "585.434.1987 x3461"
},
{
"id" : 1,
"name": "Ralph Leannon" ,
"address": "48798 Balistreri Green",
"country": "Kiribati",
"phone": "(516) 232-1392"
},
{
"id" : 2,
"name": "Jairo Mraz" ,
"address": "162 Maynard Stream",
"country": "Zimbabwe",
"phone": "1-656-704-5585 x96268"
},
{
"id" : 3,
"name": "Austin Bode" ,
"address": "238 Stanford Lake",
"country": "Belgium",
"phone": "1-224-741-6125 x79764"
},
{
"id" : 4,
"name": "Janelle McGlynn" ,
"address": "372 Nader Villages",
"country": "Chad",
"phone": "565.869.1523"
}
]
In this example, the syntax for generating Fake Data is integrated with the repeat block to create a CSV file. When you set the content type to text/csv, it generates a downloadable CSV filled with random user data.
Template used:
id,first_name,last_name,country
{{#repeat 10 comma=false}}
{{@index}},{{faker 'name.firstName'}},{{faker 'name.lastName'}},{{faker 'address.country'}}
{{/repeat}}
Response generated:
id,first_name,last_name,country
0,Lonnie,Lemke,Eritrea
1,Alyson,Corkery,Indonesia
2,Trinity,Treutel,Canada
3,Braulio,Rau,Tonga
4,Rubye,Wolf,Panama
5,Fanny,Larkin,Argentina
6,River,Ryan,Guinea-Bissau
7,Andre,Flatley,Vietnam
8,Beryl,Schoen,Niue
9,Velva,Ullrich,Iran
Technical Details
Here's a detailed technical breakdown of how the Repeat Block functions:
- Fixed Iteration Count: By giving a single numerical value as an argument, the
repeat
block will execute a predetermined number of iterations. This results in consistent output for each request, with the contents of the repeat block duplicated exactly per the specified iteration count. - Random Iteration Range: If you give two numerical values as arguments, it activates a random iteration mode. The system will randomly select a number within this range, leading to a variable number of iterations. Thus, each request generates a unique output as the repeat block's content is replicated a random number of times.
- Intelligent Comma Insertion: The
repeat
block is designed to automatically insert commas (,) between consecutive blocks to ensure the generation of a syntactically correct JSON array or a item list of items. In addtion, it smartly omits the comma after the last block to maintain JSON validity. You can override this behavior by addingcomma=false
in therepeat
argument, which disables the automatic comma insertion. Refer to an example in the table above. - Iteration Cap: The
repeat
block enforces a maximum limit of 1,000 iterations. If you specify a value exceeding this limit, it is automatically adjusted down to 1,000. This cap ensures efficient performance and avoids excessively large data outputs. - Contextual Keywords: The block supports dynamic keywords such as
@index
and@total
, providing you with contextual information about the current iteration's index and the total iteration count, respectively. This feature is especially useful for labeling or indexing elements within the JSON array output, like assigning unique id fields.