svelte-template-directives
Svelte template directive guidance. Use when working with snippets, attachments, dynamic HTML, declaration tags, debugging tags, or global DOM events.
- License 许可
- MIT
- 最近更新
- 2026-08-04
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- svelte-template-directives
- metadata
- { "last_updated": "2026-05-31", "verified_against": "Svelte 5 official docs and sveltejs/svelte#18282" }
- description
- Svelte template directive guidance. Use when working with snippets, attachments, dynamic HTML, declaration tags, debugging tags, or global DOM events.
02 资源文件 2
- 参考文档references/attach-patterns.md6.9 KB
- 参考文档references/other-directives.md4.7 KB
03 SKILL.md 正文
Svelte Template Directives
@attach (Svelte 5.29+)
The reactive alternative to use: actions. Re-runs when dependencies
change, passes through components via spread, supports cleanup functions.
<script>
import ImageZoom from 'js-image-zoom';
function useZoom(options) {
return (element) => {
new ImageZoom(element, options);
return () => console.log('cleanup');
};
}
</script>
<!-- Re-runs if options changes (use: wouldn't!) -->
<figure {@attach useZoom({ width: 400 })}>
<img src="photo.jpg" alt="zoomable" />
</figure>
Quick Reference
| Directive | Purpose | Reactive? |
|---|---|---|
{@attach} |
DOM manipulation, 3rd-party | Yes |
{@html} |
Render raw HTML strings | Yes |
{@render} |
Render snippets | Yes |
{let ...} / {const ...} |
Local declarations in markup | N/A |
{@debug} |
Pause debugger on value change | N/A |
{#each (key)} |
Keyed iteration (always key!) | Yes |
<svelte:window> |
Window event listeners | N/A |
@attach vs use: Actions
| Feature | use: |
@attach |
|---|---|---|
| Re-runs on arg change | No | Yes |
| Composable | Limited | Fully |
| Pass through props | Manual | Auto via spread |
| Convert legacy | N/A | fromAction() |
Reference Files
- attach-patterns.md - Real-world @attach examples
- other-directives.md - @html, @render, declaration tags, @debug
Notes
@attachrequires Svelte 5.29+- Use
fromActionfromsvelte/attachmentsto convert legacy actions - Attachments pass through wrapper components when you spread props
- Always use keyed each blocks — never use index as key
- Use
<svelte:window>/<svelte:document>for global events, not$effect - Use
{const ...}/{let ...}declaration tags for local markup variables;{@const ...}is legacy syntax. - Last verified: 2026-05-31
<!-- PROGRESSIVE DISCLOSURE GUIDELINES:
- Keep this file ~50 lines total (max ~150 lines)
- Use 1-2 code blocks only (recommend 1)
- Keep description <200 chars for Level 1 efficiency
- Move detailed docs to references/ for Level 3 loading
- This is Level 2 - quick reference ONLY, not a manual -->
04 同仓库更多 Skills
sveltekit-structure
SvelteKit structure guidance. Use for routing, layouts, error handling, SSR, or svelte:boundary. Covers file naming, nested layouts, error boundaries, pending UI, and hydration.
sveltekit-data-flow
SvelteKit data flow guidance. Use when working with load functions, form actions, server/client boundaries, serialization, or invalidation.
svelte-styling
Svelte CSS styling patterns. Use for scoped styles, CSS custom properties, style: directive, :global, or styling child components.
svelte-runes
Implement Svelte 5 runes correctly. Use for reactive state, props, effects, $state.raw, $derived.by, $props, and $bindable.
svelte-layerchart
Build Svelte LayerChart components. Use for tooltip snippets, Chart context access, gradients, highlights, axes, and Svelte 5 snippet patterns.
svelte-core-bestpractices
Svelte 5 core best practices. Use when creating, editing, or reviewing .svelte, .svelte.ts, or .svelte.js files. Routes to deeper Svelte skills.