Skip to content

Views

Views are svelte components exported by app/exports/server.ts and/or app/exports/client.ts.

  • main.go
  • Directoryapp
    • Directoryexports
      • client.ts
      • server.ts

Views that are meant to be rendered on the server should be exported by app/exports/server.ts.

app/exports/server.ts
import Welcome from '$lib/views/Welcome.svelte'
import Profile from '$lib/views/Profile.svelte'
export const views = {
"Welcome": Welcome,
"Profile": Profile,
}

Use SendView() to send a view from a route handler.

lib/handlers/welcome.go
package lib
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/views"
)
func Welcome(con *connections.Connection) {
// Sends file "app/lib/components/views/Welcome.svelte"
con.SendView(views.View{Name: "Welcome"})
}

The Name of the view will be used to lookup the view component exported by server.ts and/or client.ts.

There is no way to specify a “default view”.

However, you can use SendFileOrElse() in order to send a file or run custom logic if it doesn’t exist.

lib/handlers/welcome.go
package lib
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/views"
)
func Welcome(con *connections.Connection) {
con.SendFileOrElse(func () {
con.SendView(views.View{Name: "Welcome"})
})
}

Usually you would map this handler to the default GET / pattern, which automatically captures all unmatched requests.

package main
import (
"embed"
"github.com/razshare/frizzante/web"
)
//go:embed app/dist
var efs embed.FS
var server = servers.New()
func main() {
server.Efs = efs
server.AddRoute(routes.Route{
Pattern: "GET /",
Handler: handlers.Welcome,
})
server.Start()
}

Optionally, you can send data along with the views by adding a Data property to your View.

lib/handlers/welcome.go
package lib
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/views"
)
func Welcome(con *connections.Connection) {
con.SendView(views.View{
Name: "Welcome",
Data: map[string]string{
"name": "world",
},
})
}

These properties are passed down to your view component.

app/lib/views/Hello.svelte
<script lang="ts">
type Props = { name: string }
let {name}:Props = $props()
</script>
<h1>Hello {name}</h1>

You can also use getContext(“view”) to retrieve your data.

app/lib/views/Hello.svelte
<script lang="ts">
import type { View } from "$frizzante/types.ts"
const view = getContext("view") as View<{ name: string }>
</script>
<h1>Hello {view.data.name}</h1>

You can choose how to render views with the RenderMode property.

Using RenderModeFull, the view is rendered on both the server and the client.
This is the default mode.

lib/handlers/welcome.go
package lib
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/views"
)
func Welcome(con *connections.Connection) {
con.SendView(views.View{
Name: "Welcome",
RenderMode: views.RenderModeFull, // Renders on server
// and client.
})
}