Contents

Snippet

Embeds a code example from the project’s code snippets.

Declaration

@Snippet(path: String, slice: String?)

Parameters

  • path:

    The relative path from your package’s top-level “Snippets” directory to the snippet file that you want to embed in the page, without the .swift file extension. (required)

  • slice:

    The name of a snippet slice to limit the embedded code example to a certain line range. (optional)

    By default, the embedded code example includes the full snippet.

Overview

Use a Snippet directive to embed a code example from the project’s “Snippets” directory on the page. The path argument is the relative path from the package’s top-level “Snippets” directory to your snippet file without the .swift extension.

@Snippet(path: "example-snippet", slice: "setup")

If you prefer, you can specify the relative path from the package’s root directory (by including a “Snippets/” prefix). You can also include the package name—as defined in Package.swift—before the “Snippets/” prefix. Neither of these leading path components are necessary because all your snippet code files are always located in your package’s “Snippets” directory.

You can define named slices of your snippet by annotating the snippet file with // snippet.<name> and // snippet.end lines. A named slice automatically ends at the start of the next named slice, without an explicit snippet.end annotation.

If the referenced snippet includes annotated slices, you can limit the embedded code example to a certain line range by specifying a slice name. By default, the embedded code example includes the full snippet. For more information, see Slice up your snippet to break it up in your content.