Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 1.83 KB

README.md

File metadata and controls

60 lines (44 loc) · 1.83 KB

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.