Skip to content

Commit

Permalink
docs: one-pager design doc for JS templates (#717)
Browse files Browse the repository at this point in the history
Co-authored-by: HackMD <37423+hackmd-hub[bot]@users.noreply.github.com>
  • Loading branch information
alecthomas and HackMD authored Dec 8, 2023
1 parent 02e7b1d commit 7012f1a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/design/js-template-gen.md
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 }}`

0 comments on commit 7012f1a

Please sign in to comment.