Skip to main content

Introduction to Handlebars

The basic concepts of Handlebars

Ossi Galkin avatar
Written by Ossi Galkin
Updated over a year ago

Introduction to Handlebars

As it is essential to provide parameters for different Elements in Frends Processes, we must understand how to easily inject references for different variables or C# expressions into the text field that acts as parameters or input. Frends provides this mechanism with the standardized Handlebar notation where you can pass References or C# expressions that result as a string inside double braces {{ }} within parameters of type Text, JSON, XML, or SQL. Under the hood in Frends, the resulting object in the Handlebar is being called with .ToString() method. This also means that the C# expressions executed in Handlebars must return an object and not void.

For instance, we could have the following Handlebars as Task parameters:

  • Referencing a directory from Environment Variables

    {{#env.Share.DirectoryOfCats}}\foo.txt
  • Injecting the current date to text with C# expression

    Today is {{DateTime.Now.ToString("dd.MM.yyyy")}}
  • Creating a file name with variable reference and C# expression

    export_{{#var.objectName }}_{{DateTime.Now.ToString("ddMMyyyyHHmmssfff")}}.xml
  • Forming a simple JSON with variable references

    { "temperature": "{{#var.temperature}}", "unit": "{{#var.unit}}" }
  • C# ternary operation to truncate variable with three dots for a Text input if it is longer than 10 characters

    {{#var.variable.Length > 10 ? #var.variable.Substring(0,7) + "..." : #var.variable}}

As we can see, there are multiple different use cases for using the Handlebars but at the same time, it is essential to understand the different types of variables and returned objects from C# expressions. As said, the Handlebar's resulting object's .ToString() method is being called when the element is executed in the Process. This on the other hand can result in unwanted results if an object is used in Handlebars which does not return an expected String representation.

The default object representations in the UI results can be also misleading as Frends UI serializes C# objects to JSON format.

For instance, if we would assign a variable with Expression new string[]{"first", "second", "third"}, we would see the following serialized Result representation in the UI:

Now, if we would pass that variable array in the Handlebars to another variable, we would get the type of the array as it is the result for the .ToString() method call of the array and not the serialized representation.

If we would like to use that array as a parameter for instance an HTTP request body, we should serialize that object or utilize JArray because these would return JSON strings when called with .ToString() method as the result of the Handlebar.

When building more complex C# expressions or references Handlebars, remember to be sure of the return types in the C# expressions or variable references.

Next, we will have a look at some example use cases with Handlebars.

Did this answer your question?