import
Learn how to load and evaluate a JSON file relative to the template.
Loads a JSON or JSON5 file relative to the template location, evaluates any expressions in it, and returns the result.
Parameters passed via the second argument are merged into the this context of the imported file, so the imported file can reference them using ${this.paramName}.
Imported files have full access to all parent variables like options, project, and packageManager. The params parameter only adds or overrides keys within this — it does not restrict the scope.
Example
Given a file that defines a print action using both a parameter and a parent variable:
{ "name": "print", "semantics": "success", // `this.greeting` comes from the parent scope, `this.name` from params "message": "${this.greeting}, ${this.name}!"}A template can define a variable and then import the file, passing additional parameters:
{ "$schema": "https://schema.croct.com/json/v1/template.json", "title": "Greeter", "description": "Prints a welcome message.", "actions": [ { "name": "define", "variables": { "greeting": "Welcome" } }, "${import('./actions/welcome.json5', {name: 'Alice'})}" ]}Running this template prints Welcome, Alice!. The imported file sees this.greeting from the parent scope and this.name from the params.
Parameters
- pathstring
The path to the JSON file, relative to the template.
- params(optional)object
Key-value pairs merged into the this context when evaluating the imported file. This is how you pass data from the parent template to the imported file.
Return
The evaluated content of the JSON file.
The return type depends on the file content and can be an object, array, string, or any valid JSON value.