generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: one-pager design doc for JS templates (#717)
Co-authored-by: HackMD <37423+hackmd-hub[bot]@users.noreply.github.com>
- Loading branch information
1 parent
02e7b1d
commit 7012f1a
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Design Doc: JS Interpreter for user codegen | ||
|
||
## Motivation | ||
|
||
`ftl schema generate` currently relies almost entirely on Go templates for all functionality. For complex types, this means that users have to write recursive inline templates that become quite difficult to reason about. Additionally, most end users aren't going to be Go template experts. The Dart template is an example, with >40 lines of complex recursive templates. | ||
|
||
## Goals | ||
|
||
Simplify how the template can be extended with complex functions in such a way that most users will find it straightforward. | ||
|
||
## Design | ||
|
||
Embed the [goja](https://github.com/dop251/goja) JS interpreter and extend the templating support to load a `template.js` file from the template directory. Each top-level function in the JavaScript file will be exposed in the Go template function map. | ||
|
||
## Alternatives Considered | ||
|
||
Another approach is to use JavaScript for all templating, removing the need for Go templates altogether. The JS VM would have functions for creating files, directories, etc. and ftl would load it and execute it. The JS file would then be responsible for generating everything. | ||
|
||
This would be a lot more work for now, vs. the simpler solution above, so we deferred this decision until later. | ||
|
||
There's also a middle-ground where, with the proposed | ||
solution, users can create template files with a single | ||
line such as `{{ . | generate }}` |