Macro
NEWMACRO() e la sintassi delle macro.
In questa pagina
API
method
NEWMACRO(code, [nocompile], [isasync])
Compila una macro in una funzione fn(model, helpers).
NEWMACRO(code, [nocompile], [isasync])| Parametro | Tipo | Descrizione |
|---|---|---|
code |
String |
La macro, un comando per riga (o separati da ;). |
nocompile? |
Boolean |
Restituisce il codice JavaScript invece di una funzione. Predefinito: false |
isasync? |
Boolean |
Crea una funzione async. Predefinito: false |
Restituisce Function function(model, [helpers]), o una stringa con nocompile.
La funzione restituisce il valore del primo RETURN eseguito, o undefined. Le parole vengono lette da model e name(...) chiama helpers.name con il modello come this. Con isasync la funzione è async e ogni chiamata a un helper viene attesa. Con nocompile ottieni il JavaScript generato come stringa, utile per verificare una regola.
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
Comandi e valori di NEWMACRO().
Macro syntax| Sintassi | Significato |
|---|---|
IF condition … ELSE IF condition … ELSE … FI | Condizioni; FI le chiude |
RETURN expression | Restituisce un valore |
AND, OR | &&, || |
==, !=, >, >=, <, <=, +, -, *, / | Operatori come in JavaScript |
price, order.total | Proprietà del modello, lette in minuscolo |
#name | Una variabile temporanea: #total = price * count |
name(a, b) | Una chiamata a un helper |
"text", 'text' | Testo; un numero tra virgolette diventa un numero |
TRUE, FALSE, YES, NO, NULL | Valori |
// text | Una riga di commento |
Le parole chiave possono essere maiuscole o minuscole.
IF total >= 100 AND customer.vip == YES
RETURN total * 0.9
FI
RETURN total