Skip to content

Latest commit

 

History

History
54 lines (38 loc) · 1.19 KB

File metadata and controls

54 lines (38 loc) · 1.19 KB

Template Store Example

This document describes the implementation of an TemplateStore that uses an embedded file system as its storage type.

Add a go file to your project with configuration properties and the ReadTemplate() implementation

package your_package_name

import (
	"embed"
	"fmt"
)

type EmbeddedFileSystemTemplateStore struct {
	Folder  embed.FS
	RootDir string
}

// implementation of TemplateStore
func (tl *EmbeddedFileSystemTemplateStore) ReadTemplate(filename string) ([]byte, error) {

	fileName := fmt.Sprintf("%v/%v", tl.RootDir, filename)
	templateFile, err := tl.Folder.ReadFile(fileName)

	return templateFile, err
}

initialize your embedded folder. for details on go embedded package see embed

//go:embed all:templates
var folder embed.FS

create store and register with engine

	// use the embedded file system loader for now.
	embedFileSystemTemplateStore := &your_package_name.EmbeddedFileSystemTemplateStore{
		Folder:  folder,
		RootDir: "templates",
	}

    //create engine
    engine := liquid.NewEngine()

    //register with the engine
	engine.RegisterTemplateStore(embedFileSystemTemplateStore)

    //ready to go