Expressions & formats
Expressions compute values; formats decide how those values look.
Expressions
Expressions appear in <t-out expr="…">, <t-let expr="…"> and
\val{…} inside formulas. They're ordinary JavaScript expressions. A name in an expression
is one of three things:
- A function you registered with
registerFunction. - A standard JavaScript global from a short list:
Math,Number,String,Boolean,Array,Object,JSON,Date,Intl,parseInt,parseFloat,isNaN,isFinite,Infinity,NaNandundefined. - A variable: every other name, whether or not it has been declared yet.
There are no other names, so Math is spelled out (Math.sqrt(x), Math.PI),
and nothing else from window leaks in: a variable called top, name or
length is simply a variable. If a variable and a registered function share a name, the variable
wins once it has a value.
A circle with radius
Your own functions
For anything longer than a one-liner, write a JavaScript function and register it. Every expression on the page can then call it by name:
At
Registration can happen at any time, even after the page has rendered: outputs that call a function
re-run as soon as it's registered. For short names, register what you need:
registerFunction("sqrt", Math.sqrt).
To give a single scope its own function, store it as a variable instead:
scopeOf(el).set("f", (x) => x * 2).
Conditional text
There's no special element for conditions. Use a ternary, which can return text:
The thermostat is set to
Writing expressions inside HTML
- Use single quotes for strings inside a double-quoted attribute:
expr="x > 0 ? 'yes' : 'no'". <,>and&&work as-is inside attribute values.- Inside element text (such as a
\val{}in a<t-math>), write<as<and&as&.
Errors and missing values
- A variable that is never declared is
undefined. Arithmetic on it givesNaN, which is shown as–. - An expression that throws shows
⚠. Hover over it to see the error message.
Expressions are compiled with new Function. If your site sends a
Content-Security-Policy header, it needs to allow 'unsafe-eval'.
Formats
The format attribute (on <t-num>, <t-var> and
<t-out>) and the optional argument of \val[…]{…} control how a
value is displayed. A format is either printf-style or the name of a registered format.
printf-style
| Format | Value | Shows |
|---|---|---|
%d | 4.6 | 5 (rounded to an integer) |
%.2f | 3.14159 | 3.14 |
%f | 2 | 2.000000 (six decimals, as in C) |
%,d | 1234567 | 1,234,567 |
%+.1f | 2 | +2.0 (always show the sign) |
$%,.2f | 1647.009 | $1,647.01 |
%d km | 42 | 42 km |
%s | "monthly" | monthly |
%% | a literal % |
Text around the placeholder is kept, so prefixes and suffixes need no extra attributes.
Named formats
| Name | Value | Shows |
|---|---|---|
percent | 0.256 | 26% (multiplies by 100) |
dollars | 1499.5 | $1,500 |
int | 2.5 | 3 |
Register your own from JavaScript. The function receives the value and returns a string:
import { registerFormat } from "/tangle.js";
registerFormat("hz", (v) => (v < 1000 ? `${v.toFixed(0)} Hz` : `${(v / 1000).toFixed(2)} kHz`));
Without a format
- A variable with a
stepis shown with as many decimals as the step:step="0.1"gives2.0,2.1, …. - Other numbers are shown as integers when they are integers, rounded when they are 1000 or more, and with four significant digits otherwise.
- Text values are shown as-is.