Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

How to Reuse Markup in Svelte 5 with Snippets

Svelte 5 snippets let you reuse a template fragment with parameters or pass caller-defined markup into a component, without creating a component for every small repeat.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Svelte 5, declare a reusable template fragment with {#snippet name(parameters)}...{/snippet}, then insert it with {@render name(arguments)}. Snippets are useful for repeated markup inside a component and for letting a caller provide markup to another component. They remain scoped template constructs—not a universal replacement for components.

How Svelte snippets work

A snippet gives a name to a piece of markup so you can render it in more than one place. Parameters let each rendering use different data. Svelte’s official guide demonstrates this with a figure template used in conditional branches: Svelte snippet reference.

As an Amazon Associate I earn from qualifying purchases.

{#snippet figure(image)}
  <figure>
    <img src={image.src} alt={image.caption} />
    <figcaption>{image.caption}</figcaption>
  </figure>
{/snippet}

{#each images as image}
  {#if image.href}
    <a href={image.href}>{@render figure(image)}</a>
  {:else}
    {@render figure(image)}
  {/if}
{/each}

Here, the snippet owns the shared figure markup, while the surrounding loop and condition decide which images are linked. The image parameter makes the data needed by each rendering explicit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where snippets fit—and where they do not

Use a snippet when a small markup fragment repeats locally, or when a component should let its caller customize how part of its content is rendered. A separate component is often clearer when the UI has its own responsibilities, state, lifecycle, or broad reuse needs. That is a design choice, not a Svelte compiler rule.

Question Snippet Separate component
Where does reuse happen? Within a component, or as markup supplied to another component. Across component boundaries as an independently defined unit.
Who controls the markup? The component defining the snippet, or its caller when the snippet is passed in. The component that owns the markup.
How does item-specific data get in? Pass it as a snippet parameter, such as {@render row(item)}. Pass data through the component’s props.
What content-passing syntax applies? Svelte 5 snippets and {@render ...}. A component boundary; its internal design determines how it accepts content.

How to pass a snippet to another component

A component can receive a snippet as a named prop, or a caller can declare a snippet inside the component element so it is supplied implicitly. The child decides where to render it and what arguments to pass.

<Table data={fruits} {header} {row} />

In this pattern, Table can render a no-argument header with {@render header()} and a row for each data item with {@render row(item)}. The official guide uses this approach to let the caller control table markup: Svelte snippet reference.

Implicit children content

Content written between a component’s opening and closing tags becomes its implicit children snippet. The child reads it from $props() and renders it with {@render children()}. For example, ordinary content such as <Button>click me</Button> is received as children. If the component also needs content between its tags, do not also pass a separate children prop.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Optional snippet props

If a snippet prop may be absent, render it conditionally or use optional chaining. For example, {@render header?.()} avoids rendering an unavailable header. An {#if} branch is useful when the absent case needs fallback markup.

Typing snippet props with TypeScript

Import Snippet as a type from svelte. Use Snippet for a snippet with no parameters, and Snippet<[T]> for one that accepts a value of type T. A generic table can keep its data and row renderer aligned:

<script lang="ts" generics="T">
  import type { Snippet } from 'svelte';

  let {
    data,
    row
  }: {
    data: T[];
    row: Snippet<[T]>;
  } = $props();
</script>

With this type relationship, the row snippet’s argument corresponds to each element in data. See the official snippet reference for the documented typing patterns.

Scope, nesting, and exporting

Snippets follow lexical scope: a snippet can refer to values visible where it is declared, and it can be nested, recursive, or mutually referential. A snippet declared inside a block is not necessarily available outside that block, so declare it where every intended render site can access it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Exporting is narrower than ordinary in-component reuse. The Svelte documentation says a top-level snippet can be exported from a <script module> only when it does not directly or indirectly reference declarations from a non-module script. The documented module-script export feature requires Svelte 5.5.0 or newer: Svelte snippet reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Snippets versus slots in Svelte 5

Svelte 5 deprecates slots in favor of snippets for passing markup. For new Svelte 5 code, use snippets and {@render ...}. Do not combine legacy <slot> syntax and render tags in the same component; the compiler reports an error for that combination. Existing Svelte 4 projects may still use older APIs, so account for the project’s version and migration state. See the compiler errors reference.

Common snippet mistakes

  • Using a snippet like an ordinary value: a snippet is rendered with {@render snippet()}, not by writing {snippet}.
  • Rendering an optional snippet unconditionally: use optional chaining or an {#if} branch with a fallback.
  • Mixing legacy slot patterns with implicit children: combining let: directives with the implicit children snippet render pattern is invalid; use a named snippet in that case. See Svelte runtime errors.
  • Declaring a snippet outside its usable scope: check whether the declaration sits inside a block that limits where it can be referenced.

How to choose

  • Choose a snippet for repeated, localized markup or caller-supplied rendering customization.
  • Pass item data as snippet parameters when the same template needs to render different rows, images, or other values.
  • Choose a separate component when the UI is an independently meaningful unit with its own responsibilities or state.
  • Use Svelte 5 snippet syntax for new markup-passing code, while treating existing slot-based code according to its current version and migration needs.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.