Skip to content

Include Files

Reuse template fragments using [[ file ]] or [[ file.mg ]]. Includes are resolved relative to the including template's directory.

Example:

// filename: header.mg
<<This is the header content.>>
// filename: page.mg
[[ header ]]
<<
# Page Title

Content goes here.
>>

Rendered result

When rendering page.mg, the output will include the header content followed by the page body:

This is the header content.

# Page Title

Content goes here.

Behavior

  • Included files only have access to variables explicitly passed as parameters in the include directive.
  • Paths are resolved relative to the parent template's directory (the CLI and renderer set base_path).
  • Avoid circular includes; they can cause infinite loops or errors.

Composable Templates

Pass variables to included templates as explicit parameters in the include directive. For example:

// filename: greeting.mg
<<Hello, ${name}!>>
// filename: main.mg
[[ greeting name="Alice" ]]

Rendered result

When rendering main.mg, the output will be:

Hello, Alice!

More Examples

See the Using Includes page for comprehensive examples and patterns.

Tip: Use includes for headers, footers, and small shared components to keep templates DRY and maintainable.