Want to try fastn for your company's website?
Book a Demo

RFC-3: Variable Interpolation In Strings

In this RFC we propose variable interpolation in strings, which can it easier to show data in UI.
📝
This is a RFC document

This document exists to describe a proposal for enhancing the fastn language. This is a Request For Comment. Please share your comments by posting them in the pull request for this RFC if this RFC is not merged yet. If the RFC is merged, you can post comment on our official Discord, or open a discussion on Github.

Learn about our RFC process. View all active RFCs. WIP RFCs, and RFCs awaiting initial comments can be found on Github, as Pull Requests, with label rfc.

Status

accepted

Motivation

Say we want to show "Hello, Jack", where Jack is stored in a variable, $name, currently we have to either write a function to concatenate Hello, and $name to form the string, or place two ftd.text nodes, wrapped in a ftd.row. Neither is very nice. So we are proposing variable interpolation, which allows easy generation of such strings with data embedded in it.

Detailed Design

Allow $var access in Strings

Any string can now refer to any variable using the $<var-name> syntax, so e.g., we can write Hello, $name, and it will expand into Hello, Jack is $name is Jack.

We already support this if the entire string content was just $<var-name>, we initialise string to it.

Interpolation Creates Formula

In fastn language, formula re-evalutes it's value whenever the underlying variable changes. This means if the variable used in any string interpolation changes, the string will automatically change as well.

$ curly syntax

We can also do: The total is $${ count * price }.

Multi line $ curly

-- ftd.text: The total is ${
count * price
}
Lang:
ftd

Escaping interpolation

Sometimes we want to show literally Hello, $name, in this case the author can write Hello, $name, escape the special handling by putting a `` in front of $.

We already do this if the string only contains $<var-name>: $<var-name>

Alternatives

This was the most logical proposal given we already support $<var-name> for a string. The behaviour described here generalises this.

Teaching Notes

It should be relatively easy to teach. A lot of people intuitively write that and get surpised that it already doesn't work.

Unresolved Questions

None we are aware of.