Makros
NEWMACRO() und die Makro-Syntax.
Auf dieser Seite
API
method
NEWMACRO(code, [nocompile], [isasync])
Kompiliert ein Makro zu einer Funktion fn(model, helpers).
NEWMACRO(code, [nocompile], [isasync])| Parameter | Typ | Beschreibung |
|---|---|---|
code |
String |
Das Makro, ein Befehl pro Zeile (oder durch ; getrennt). |
nocompile? |
Boolean |
Gibt den JavaScript-Code statt einer Funktion zurück. Standard: false |
isasync? |
Boolean |
Erzeugt eine asynchrone Funktion. Standard: false |
Rückgabe Function function(model, [helpers]) oder mit nocompile ein String.
Die Funktion gibt den Wert des ersten ausgeführten RETURN zurück, oder undefined. Wörter werden aus model gelesen, und name(...) ruft helpers.name mit dem Modell als this auf. Mit isasync ist die Funktion asynchron, und auf jeden Aufruf einer Hilfsfunktion wird gewartet. Mit nocompile erhältst du das erzeugte JavaScript als String, was nützlich ist, um eine Regel zu prüfen.
var shipping = NEWMACRO('IF weight > 10\n\tRETURN 15\nELSE IF weight > 2\n\tRETURN 8\nFI\nRETURN 4');
shipping({ weight: 5 }); // 8
var fn = NEWMACRO('RETURN rate(currency) * price', false, true);
var value = await fn({ price: 10, currency: 'EUR' }, { rate: async function(code) { return 1.1; } });
type
Macro syntax
Befehle und Werte von NEWMACRO().
Macro syntax| Syntax | Bedeutung |
|---|---|
IF condition … ELSE IF condition … ELSE … FI | Bedingungen; FI schließt sie |
RETURN expression | Gibt einen Wert zurück |
AND, OR | &&, || |
==, !=, >, >=, <, <=, +, -, *, / | Operatoren wie in JavaScript |
price, order.total | Eigenschaften des Modells, in Kleinbuchstaben gelesen |
#name | Eine temporäre Variable: #total = price * count |
name(a, b) | Ein Aufruf einer Hilfsfunktion |
"text", 'text' | Text; eine Zahl in Anführungszeichen wird zu einer Zahl |
TRUE, FALSE, YES, NO, NULL | Werte |
// text | Eine Kommentarzeile |
Schlüsselwörter können beliebig groß oder klein geschrieben sein.
IF total >= 100 AND customer.vip == YES
RETURN total * 0.9
FI
RETURN total