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

# Makros

> NEWMACRO() und die Makro-Syntax.

## API

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

Type: `method`

Kompiliert ein Makro zu einer Funktion `fn(model, helpers)`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `code` | `String` | yes |  | Das Makro, ein Befehl pro Zeile (oder durch `;` getrennt). |
| `nocompile` | `Boolean` | no | `false` | Gibt den JavaScript-Code statt einer Funktion zurück. |
| `isasync` | `Boolean` | no | `false` | Erzeugt eine asynchrone Funktion. |

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

Example: Versand
```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: Asynchrone Hilfsfunktionen
```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`

Befehle und Werte von `NEWMACRO()`.

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

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