Can you pass arguments to functions in Slate?

560 Views Asked by At

In Foundry's Slate application, is there a clean way to write functions that accept arguments as input using the handlebar syntax?

1

There are 1 best solutions below

0
On

Instead of function arguments, inputs to a Slate function are defined by a Handlebar reference inside the Function itself; for example to access data from a query from inside a Function, you might write:

const data = {{q_myQuery}}

Defining dependencies in this way allows Slate to automatically recompute the Function outputs whenever the values of upstream dependencies change. In this way, you never "call" a function, but rather some other element in Slate references the Function output and that output is updated whenever the inputs change.

If you want to do some kind of code reuse you can use functionLibraries to write common code that you can re-use between Functions. These are standard javascript functions that are included in the global javascript scope and can be referenced simply by the function name from any Function and take function parameters using normal javascript syntax. Since these are vanilla javascript you cannot use Handlebars inside a functionLibrary - here any input must be passed in as a parameter from the parent Function.

From the documentation (Slate > Concepts > Functions):

Per-Document level function libraries

Users are able to write reusable javascript functions with parameters. This will assist in the refactoring of code and reducing the copying and pasting of code in functions. You can also re-run and update all the functions dependent on a function library using the Re-run All Function button.

Default JavaScript libraries available

For enhanced use of functions, Slate ships by default (as of Slate 2.15) with the following external JavaScript libraries: Lodash, Math.js, Moment, Numeral and es6-shim. Feel free to use these libraries when writing your functions.Do not use ES6 syntax features unless all users are mandated to use a browser supporting these features.