---
title: Macro
project: Total.js V5
version: 0.0.20
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v5/0-0-20/reference/macros/
updated: 2026-11-18T16:56:54.572Z
---

# Macro

> NEWMACRO() e la sintassi delle macro.

## API

### NEWMACRO(code, [nocompile], [isasync])

Type: `method`

Compila una macro in una funzione `fn(model, helpers)`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `code` | `String` | yes |  | La macro, un comando per riga (o separati da `;`). |
| `nocompile` | `Boolean` | no | `false` | Restituisce il codice JavaScript invece di una funzione. |
| `isasync` | `Boolean` | no | `false` | Crea una funzione async. |

Returns: `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.

Example: Spedizione
```js
var shipping = NEWMACRO('IF weight > 10\n\tRETURN 15\nELSE IF weight > 2\n\tRETURN 8\nFI\nRETURN 4');
shipping({ weight: 5 }); // 8
```

Example: Helper asincroni
```js
var fn = NEWMACRO('RETURN rate(currency) * price', false, true);
var value = await fn({ price: 10, currency: 'EUR' }, { rate: async function(code) { return 1.1; } });
```

### Macro syntax

Type: `type`

Comandi e valori di `NEWMACRO()`.

| 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.

Example: Regola
```js
IF total >= 100 AND customer.vip == YES
	RETURN total * 0.9
FI
RETURN total
```
