Giter Site home page Giter Site logo

go-template's Introduction

Go Template Components

๐Ÿšจ Possibly NSFW yet.


This is a drop-in replacement for Go's text/template and html/template packages, that adds a new {{component}} and {{slot}} element to the language.

The new component element allows you to render reusable HTML template components that can be used across your HTML templates.

Install

go get github.com/philippta/go-template@latest
import (
    "github.com/philippta/go-template/html/template" // for html templates
    "github.com/philippta/go-template/text/template" // for text templates
)

Simple example

There are two new elements to Go's templating language:

  • {{component "headline" .}} ... {{end}
  • {{slot}}

Defining a component

A new component can be defined using the regular {{define}} element. With the help of the new {{slot}} element, an outlet can be set which later renders the inner/child HTML.

{{define "navbar"}}
<nav class="navbar">
    <ul class="navbar-items">
        {{slot}}
    <ul>
</nav>
{{end}}

Rendering a component

To render a previously defined component, the {{component}} element is used. The component element has two required parameters. First, the name of the component to render; second, the pipeline (dot).

{{component "navbar" .}}
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
{{end}}

This will render the following HTML:

<nav class="navbar">
    <ul class="navbar-items">
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a></li>
    <ul>
</nav>

Drop-in Replacement

This package is a drop-in replacement for the text/template and html/template packages and can be used by simply replacing the imports.

import (
    // "html/template"
    "github.com/philippta/go-template/html/template"
)

func main() {
    t := template.Must(template.New("layout.html").ParseFiles(
        "components/navbar.html",
        "layout.html",
    ))

    t.Execute(os.Stdout, nil)
}

Passing Component Props

Passing in additional properties to a component can be achieved with a simple template function.

A full example can be found in the example directory.

func main() {
    funcs := template.FuncMap{
        "props": props,
    }

    t := template.Must(template.New("layout.html").Funcs(funcs).ParseFiles("layout.html"))
    t.Execute(os.Stdout, nil)
}

func props(v ...any) map[string]any {
	if len(v)%2 != 0 {
		panic("uneven number of key/value pairs")
	}

	m := map[string]any{}
	for i := 0; i < len(v); i += 2 {
		m[fmt.Sprint(v[i])] = v[i+1]
	}

	return m
}
{{define "card"}}
<div class="card">
    {{if .Image}}
    <img class="card-image" src="{{.Image}}" />
    {{end}}

    <div class="card-body">
        {{slot}}
    </div>

    {{if .Link}}
    <a class="card-link" href="{{.Link}}">Open</a>
    {{end}}
</div>
{{end}}

<body>
    {{component "card" (props "Image" "/static/dog.jpeg" "Link" "/blog/dogs" )}}
    <h1>Dogs</h1>
    <p>
        This is a blog post about dogs.
    </p>
    {{end}}
</body>

go-template's People

Contributors

philippta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.