Performance
Templates parsed at app startup, evaluated on page visit. Use Go's embed package to bundle templates in final binary
Outdated version
You are viewing the outdated version of Textwire. Switch to the latest version to get all the new features and improvements
Prioritizing runtime performance and long-term stability for Go developers

@use('~base')
@insert('title', 'Book Store')
@insert('content')
<h1 class="text-2xl font-bold">Welcome to the Book Store</h1>
<p class="opacity-70 mb-5">See the list of our books</p>
@if(books.len() > 0)
<div>
@each(book in books)
@component('~book', { book })@end
@end
</div>
@else
<p>No books found</p>
@end
@end<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<title>@reserve('title') | Books Store</title>
</head>
<body class="max-w-[900px] my-0 mx-auto p-4">
<main>
@reserve('content')
</main>
</body>
</html><div class="flex justify-between items-center gap-3 p-5 odd:bg-gray-100 rounded-md">
<div class="flex items-center gap-5">
<img
src="{{ book.image }}"
alt="{{ book.title }}"
class="w-20 rounded-md shadow-md"
>
<div>
<h3 class="text-xl">{{ book.title }}</h3>
<p class="opacity-70">{{ book.author }}</p>
</div>
</div>
<button class="bg-green-600 text-white px-5 py-2 rounded-md shadow-md cursor-pointer">
Order
</button>
</div>templates/
├── layouts/
│ └── base.tw
│
├── components/
│ └── book.tw
│
├── views/
│ └── home.tw
│
└── error-page.twpackage main
import (
"fmt"
"net/http"
"github.com/textwire/textwire/v6"
"github.com/textwire/textwire/v6/pkg/fail"
)
var tpl *textwire.Template
func main() {
var err *fail.Error
tpl, err = textwire.NewTemplate(nil)
err.FatalOnError()
http.HandleFunc("/", homeHandler)
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println(err)
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
vars := map[string]any{
"books": []struct{ Title, Author, Image string }{
{"The Great Gatsby", "F. Scott Fitzgerald", "https://placehold.co/400"},
{"To Kill a Mockingbird", "Harper Lee", "https://placehold.co/400"},
{"1984", "George Orwell", "https://placehold.co/400"},
{"Pride and Prejudice", "Jane Austen", "https://placehold.co/400"},
{"The Catcher in the Rye", "J.D. Salinger", "https://placehold.co/400"},
},
}
tpl.Response(w, "views/home", vars).PrintOnError()
}