Examples

Here you will find applications that have been developed using GoHT. These applications are meant to demonstrate the capabilities of GoHT and to provide examples of how to use GoHT in your own projects.

Feature Examples

These smaller examples focus on individual GoHT features. The examples use placeholder Product and User domain types so they can stay focused on template syntax rather than application setup.

Slim Template

Slim templates use tag names directly. This example shows tags, attributes, interpolation, and @render composition:

package examples

@slim ProductShell(title string) {
	section.product-shell
		header
			h2= title
		main
			= @children
}

@slim ProductCard(product Product) {
	= @render ProductShell(product.Name)
		article.card{data-id: product.ID}
			p.price= "$#{product.Price}"
			a.btn{href: product.URL}= "View #{product.Name}"
}

EGO Template

EGO templates keep standard HTML and use Go-style tags for dynamic output and GoHT commands:

package examples

@ego ProductCardEgo(product Product) {
	<%@render ProductShell(product.Name) { %>
		<article class="card" data-id="<%= product.ID %>">
			<h3><%= product.Name %></h3>
			<p><%= product.Description %></p>
			<a href="<%= product.URL %>">View product</a>
		</article>
	<% } %>
}

Named Slots

Named slots let callers fill specific regions. A slot can also provide default content when no matching slot is supplied:

package examples

@haml ProductLayout(product Product) {
	%article.product
		%header
			%h2= product.Name
			=@slot badges
		%section
			=@children
		%footer
			=@slot actions
				%a{href: "/"} Continue browsing
}

HTTP Handlers

Generated template functions return goht.Template. In an HTTP handler, call Render with the request context and response writer:

func handleProduct(products ProductStore) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		product := products.Featured(r.Context())

		if err := ProductPage(product).Render(r.Context(), w); err != nil {
			http.Error(w, "failed to render product", http.StatusInternalServerError)
			return
		}
	}
}