Skip to content

Library to load several text/template decomposed in multiple file and render them globally or partially.

License

Notifications You must be signed in to change notification settings

dvaumoron/partrenderer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Library to load several text/template decomposed in multiple file and render them globally or partially.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages