How to Use Goview: A Minimalist Template Library for Go

Jul 26, 2024 | Programming

Welcome to our guide on Goview, a lightweight and idiomatic template library for building Go web applications using Golang’s html/template. In this article, we’ll walk you through the installation process, explore its features, delve into usage examples, and provide troubleshooting tips.

Contents

Install

To install Goview, simply run:

go get github.com/fooling/goview

Features

  • Lightweight – utilize Golang htmltemplate syntax.
  • Easy – simplifies template management for web applications.
  • Fast – supports caching for quick template rendering.
  • Include syntax – allows for file inclusions.
  • Master layout – supports master layout configuration.
  • Extension – configures template file extensions.
  • Dynamic reload – supports automatic reloading of templates when in development.
  • Multiple engines – accommodates various template engines for frontend and backend.
  • No external dependencies – purely uses Go’s html template package.
  • Supports Go.Rice – integrates easily for static files.
  • Compatible with major Go frameworks: Gin, Iris, Echo, Go-Chi.

Docs

For complete documentation, visit the Goview on GoDoc.

Supports

Goview supports various frameworks:

Usage

To illustrate how to use Goview, we can think of templates like baking ingredients; you need the right mixture and ratios to create the final product, which in this case is your web page. Let’s break down the different key areas:

Overview

Consider your project structure as a recipe book where each recipe contains specific ingredients (HTML templates) and instructions (logic). Here’s a simple structure you would have:


go
-- app
    -- views
        -- index.html
        -- page.html
    -- layouts
        -- footer.html
        -- master.html

Config

You can configure Goview like preparing your kitchen. Here’s how to set up your instance:


gv := goview.New(goview.Config{
    Root:      "views",
    Extension: ".tpl",
    Master:    "layouts/master",
    Partials:  []string{"partials/ad"},
    Funcs: template.FuncMap{
        "sub": func(a, b int) int { return a - b },
        "copy": func() string { return time.Now().Format("2006") },
    },
    DisableCache: true,
    Delims:       DelimsLeft: "{", Right: "}",
})

Include Syntax

To include parts of templates like a chef incorporates different flavors, use the following syntax:


{{ template "layouts/footer" . }}

Render Name

Rendering is akin to putting your dish in the oven to bake. Here’s how you can render templates:


goview.Render(writer, http.StatusOK, "index", goview.M)

Custom Template Functions

Just like adding unique spices can elevate a dish, custom template functions can enhance your templates. You can set global and temporary functions as described in the usage section.

Examples

To see more examples and further clarify the usage, visit the Goview examples.

Troubleshooting

If you encounter issues while working with Goview, consider the following troubleshooting tips:

  • Check file paths in your configuration. Incorrect paths often lead to template rendering errors.
  • Ensure your templates have the correct file extensions and match the configuration settings.
  • Review the browser console for errors related to missing resources.
  • If dynamic reload isn’t functioning, verify that caching is disabled in your configuration.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox