💡 Web projects can be hard. Our solutions specialists can help!

Templating in SvelteKit

In this tutorial we'll go over basic SvelteKit templating concepts and see how you can use templating on your site.

By Mike Neumegen

Svelte gives us complete control over how pages and components are rendered. We can loop over content, output variables, run logic statements, or pull in data from external sources. We’ve already encountered some templating — the curly braces { } to output the className in the previous lesson.

How do I use templating in SvelteKit?

Templating is one of the most common things you’ll be doing in SvelteKit, so let’s go through some examples to demonstrate how it works.

Output a string

<p>You can write normal HTML, and when you want to switch to Svelte,
you can use single curly braces like this: { "Hello!" }</p>

Output prop value

export let favorite_treat = 'bone';

<p>My favorite treat is a { favorite_treat }</p>


export let goodBoy = true;

{#if goodBoy}
<p>One treat please.</p>
<p>No treats for me.</p>


export let whoLetTheDogsOut = [

{#each whoLetTheDogsOut as name}

Interactive element

let count = 0;

function handleClick() {
count += 1;

<button on:click={handleClick}>
clicks: {count}

<!-- When you click the button it will run the handleClick function
and live update {count} -->

That gives you some of the basic tools to play with. You’ll be using these concepts over and over again through your SvelteKit journey.

Putting it all together

We’ve seen a lot of different concepts here. Let’s put it into practice by adding a footer to the website the date and time the page was rendered.

First, we’ll create a component for the footer. Create /src/lib/Footer.svelte with the following content:

let now = new Date();

Website was generated { today }

Just like we did earlier, now we can import Footer into our layout and render the component:

import Nav from '$lib/Nav.svelte';
import Footer from '$lib/Footer.svelte';

<h1>Svelte's space</h1>
<Nav className="alt" />


<Footer />

<style lang="scss">
:global(body) {
width: 400px;
margin: 0 auto;
font-family: sans-serif;

What’s next?

Let’s put our templating knowledge to the test by creating a blog.