partrenderer

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: Apache-2.0 Imports: 7 Imported by: 2

README

PartRenderer

Library to load several go template decomposed in multiple file and render them globally or partially (useful with htmx).

Getting started

In order to use PartRenderer in your project (with the go langage already installed), you can use the command :

go get github.com/dvaumoron/partrenderer@latest

Then you can import it :

import "github.com/dvaumoron/partrenderer"

And use it in two step :

// parse templates
renderer, err := partrenderer.MakePartRenderer(componentsPath, viewsPath)
// and use them
err = renderer.ExecuteTemplate(writer, viewName, data)

The first call with :

  • componentsPath indicate a directory to walk in order to load all component templates
  • viewsPath indicate a directory to walk in order to load all view templates (which can see components)

The second call has the same signature as Template.ExecuteTemplate where viewName has no extention ("hello/index" for an index.html file in hello folder) and can have a part selector (like in "hello/index#body", without this selector "root" is used).

With componentsPath/main.html like :

{{define "root"}}
    <html>
        <head>
            <meta charset="utf-8"/>
            {{template "header" .}}
        </head>
        <body>
             {{template "body" .}}
        </body>
</html>
{{end}}

And viewsPath/hello/index.html like :

{{define "header"}}
    <title>Hello World</title>
{{end}}
{{define "body"}}
    <h1 class="greetings">Hello World</h1>
{{end}}

See advanced examples of componentsPath and viewsPath templates.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrViewNotFound = errors.New("view not found")

Functions

func AlwaysReload added in v0.3.0

func AlwaysReload(err error) bool

func NeverReload added in v0.3.0

func NeverReload(err error) bool

func ReloadOnViewNotFound added in v0.3.0

func ReloadOnViewNotFound(err error) bool

Types

type LoadOption added in v0.2.0

type LoadOption func(loadInfos) loadInfos

func WithFileExt added in v0.2.0

func WithFileExt(ext string) LoadOption

option to use an alternate extension to filter loaded file (default is ".html")

func WithFs added in v0.2.0

func WithFs(fs afero.Fs) LoadOption

option to use an alternate file system

func WithFuncs added in v0.2.0

func WithFuncs(customFuncs template.FuncMap) LoadOption

allow to load a template.FuncMap before parsing the go templates

func WithReloadRule added in v0.3.0

func WithReloadRule(rule ReloadRule) LoadOption

option to change the rule to reload on error (default is ReloadOnViewNotFound)

type PartRenderer

type PartRenderer struct {
	Separator string
	RootName  string
	// contains filtered or unexported fields
}

func MakePartRenderer

func MakePartRenderer(componentsPath string, viewsPath string, opts ...LoadOption) (PartRenderer, error)

The componentsPath argument indicates a directory to walk in order to load all component templates

The viewsPath argument indicates a directory to walk in order to load all view templates (which can see components)

func (PartRenderer) ExecuteTemplate

func (r PartRenderer) ExecuteTemplate(w io.Writer, viewName string, data any) error

Find a template and render it, global and partial rendering depend on PartRenderer.RootName and PartRenderer.Separator. Could try a reload on error depending on the ReloadRule option.

type ReloadRule added in v0.3.0

type ReloadRule = func(error) bool

a true trigger a reload

Jump to

Keyboard shortcuts

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