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:
-
Pattern reuse: Common template patterns can be extracted and reused
-
Language extension: New control structures can be defined
-
Higher-order templates: Macros can accept template content as parameters
-
DSL creation: Domain-specific notations can be built on MTL
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
Creates a new MTL macro.
Instance Properties
The macro body defining its expansion.
Optional body parameter name.
Optional documentation string.
The name of the macro.
The parameters accepted by this macro.