Functions

Introduction

A function is a construct for providing advanced functionality inside of a clause. As in mathematics, a function typically accepts some sort of input, performs an operation, and returns a result.

For example, a hypothetical function could accept two numbers and return their sum.

For a list of available functions, visit the function reference.

Anatomy of a function

In ESCL, all functions are comprised of two parts: a name in uppercase and an arguments clause wrapped in parenthesis.

A function's arguments clause is processed just like any other clause, so it may contain variables, string identifiers, terms, and even other functions.

In this example, the function's name is LINK and its arguments clause is :myText, :myUrl.

LINK(:myText, :myUrl)

Working with functions

A function may be included in a clause, just like a variable or string identifier. In fact, you can use them together:

welcome_message: `Hello, { LINK(:linkText, :linkUrl) }, and welcome!`

Then, when we translate the string, we pass in some variable values:

$translate('welcome_message', {
    linkText: 'John Doe',
    linkUrl: 'profile.php'
});

And that's it! We've created a hyperlink directly in our string:

Hello, <a href="profile.php">John Doe</a>, and welcome!

Nesting functions

Sometimes, you may want to call multiple functions on the same piece of text. For this, you can nest multiple function calls, and they will be executed in order.

In this example, we want to create a link that is also bold:

welcome_message: `Hello, { BOLD(LINK(:linkText, :linkUrl)) }, and welcome!`

Then, when we translate the string, we pass in some variable values:

$translate('welcome_message', {
    linkText: 'John Doe',
    linkUrl: 'profile.php'
});

Voila! We've used both functions together to provide a result that is bold and linked:

Hello, <strong><a href="profile.php">John Doe</a></strong>, and welcome!

Last updated