Documentation Language: Swift

Structure

MTLMacro

Represents an MTL macro for language extension.

struct MTLMacro

Overview

Macros provide a mechanism for extending MTL with custom language constructs, allowing templates to capture and reuse complex patterns. They can accept both regular parameters and body content parameters.

Overview

MTL macros enable:

Body Parameters

Macros can define a special body parameter that captures the content between macro invocation tags, enabling higher-order template patterns:

[macro wrapDiv(content: Body)]
  <div>
    [content/]
  </div>
[/macro]

[wrapDiv()]
  Inner content here
[/wrapDiv]

Example Usage

// Simple macro
let repeatMacro = MTLMacro(
    name: "repeat",
    parameters: [
        MTLVariable(name: "times", type: "Integer")
    ],
    bodyParameter: "content",
    body: MTLBlock(statements: [
        MTLForStatement(
            binding: MTLBinding(
                variable: MTLVariable(name: "i", type: "Integer"),
                initExpression: MTLExpression(AQLCollectionExpression(
                    operation: .range,
                    arguments: [/* 1..times */]
                ))
            ),
            body: MTLBlock(statements: [
                MTLMacroInvocation(macroName: "content")
            ])
        )
    ])
)

// Conditional wrapper macro
let ifPublicMacro = MTLMacro(
    name: "ifPublic",
    parameters: [MTLVariable(name: "element", type: "Element")],
    bodyParameter: "content",
    body: MTLBlock(statements: [
        MTLIfStatement(
            condition: MTLExpression(/* element.isPublic */),
            thenBlock: MTLBlock(statements: [
                MTLMacroInvocation(macroName: "content")
            ])
        )
    ])
)

Topics

Initializers

Instance Properties

V
body

The macro body defining its expansion.

V
bodyParameter

Optional body parameter name.

V
documentation

Optional documentation string.

V
name

The name of the macro.

V
parameters

The parameters accepted by this macro.

Default Implementations

Relationships

Conforms To