templates

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

README

godoc codecov Go Report Card

templates - text and html template utilities

A collection of utilties for working with text and html templates.

Installation

> go get github.com/go-corelibs/templates@latest

Examples

AddParseTree

func htmlExample() {
    initial, _ := htmlTemplate.New("initial").Parse(`{{ printf "initial test" }}`)
    another, _ := htmlTemplate.New("another").Parse(`{{ printf "another test" }}`)
    err := templates.AddParseTree(another, initial) // err == nil
}

func textExample() {
    initial, _ := textTemplate.New("initial").Parse(`{{ printf "initial test" }}`)
    another, _ := textTemplate.New("another").Parse(`{{ printf "another test" }}`)
    err := templates.AddParseTree(another, initial) // err == nil
}

Lookup

Lookup is primarily useful for theme rendering systems where a parent template (tt in this example) is pre-loaded with all of the available source files in the current theme. Some themes may support custom content types and the theme render needs to be able to lookup an existing template matching a list of names in a specific order of priority or preference. This example is looking for page.html.tmpl first, then page.html and finally page.tmpl. If none of these are present, the found return value will be nil.

func main() {
    list := []string{
        "page.html.tmpl",
        "page.html",
        "page.tmpl",
    }
    found := templates.Lookup(tt, list...)
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package templates provides text and html template utilities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddParseTree

func AddParseTree[V Type, T Template[V]](src, dst T) (err error)

AddParseTree adds all the individual `src.Templates()` and adds their parse trees to the `dst` template instance

func GetParseTree

func GetParseTree[T Type](t T) (tree *parse.Tree)

GetParseTree returns the text/template/parse.Tree associated with the given template

func Lookup

func Lookup[T Type](t T, names ...string) (tmpl T)

Lookup returns the first template matching one of the names given

Types

type Template

type Template[V Type] interface {
	Name() string
	Templates() []V
	Lookup(name string) (t V)
	AddParseTree(name string, tree *parse.Tree) (t V, err error)
}

Template is a generic type interface for interacting with either text or html template instances

type Type

type Type interface {
	*textTemplate.Template | *htmlTemplate.Template
}

Type is a generic type constraint for the text/template.Template and html/template.Template concrete types

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL