Restore local live reloading of templates.

This commit is contained in:
mikestefanello 2023-12-12 20:07:58 -05:00
parent 3df20c01a7
commit 81c65fcc30
5 changed files with 63 additions and 21 deletions

View File

@ -99,7 +99,7 @@ func TestController_RenderPage(t *testing.T) {
expectedTemplates := make(map[string]bool) expectedTemplates := make(map[string]bool)
expectedTemplates[p.Name+config.TemplateExt] = true expectedTemplates[p.Name+config.TemplateExt] = true
expectedTemplates[p.Layout+config.TemplateExt] = true expectedTemplates[p.Layout+config.TemplateExt] = true
components, err := templates.Templates.ReadDir("components") components, err := templates.Get().ReadDir("components")
require.NoError(t, err) require.NoError(t, err)
for _, f := range components { for _, f := range components {
expectedTemplates[f.Name()] = true expectedTemplates[f.Name()] = true
@ -132,7 +132,7 @@ func TestController_RenderPage(t *testing.T) {
expectedTemplates := make(map[string]bool) expectedTemplates := make(map[string]bool)
expectedTemplates[p.Name+config.TemplateExt] = true expectedTemplates[p.Name+config.TemplateExt] = true
expectedTemplates["htmx"+config.TemplateExt] = true expectedTemplates["htmx"+config.TemplateExt] = true
components, err := templates.Templates.ReadDir("components") components, err := templates.Get().ReadDir("components")
require.NoError(t, err) require.NoError(t, err)
for _, f := range components { for _, f := range components {
expectedTemplates[f.Name()] = true expectedTemplates[f.Name()] = true

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"io/fs"
"sync" "sync"
"github.com/mikestefanello/pagoda/config" "github.com/mikestefanello/pagoda/config"
@ -103,25 +104,28 @@ func (t *TemplateRenderer) parse(build *templateBuild) (*TemplateParsed, error)
parsed := template.New(build.base + config.TemplateExt). parsed := template.New(build.base + config.TemplateExt).
Funcs(t.funcMap) Funcs(t.funcMap)
// Parse all files provided // Format the requested files
if len(build.files) > 0 { for k, v := range build.files {
for k, v := range build.files { build.files[k] = fmt.Sprintf("%s%s", v, config.TemplateExt)
build.files[k] = fmt.Sprintf("%s%s", v, config.TemplateExt)
}
parsed, err = parsed.ParseFS(templates.Templates, build.files...)
if err != nil {
return nil, err
}
} }
// Parse all templates within the provided directories // Include all files within the requested directories
for _, dir := range build.directories { for k, v := range build.directories {
dir = fmt.Sprintf("%s/*%s", dir, config.TemplateExt) build.directories[k] = fmt.Sprintf("%s/*%s", v, config.TemplateExt)
parsed, err = parsed.ParseFS(templates.Templates, dir) }
if err != nil {
return nil, err // Get the templates
} var tpl fs.FS
if t.config.App.Environment == config.EnvLocal {
tpl = templates.GetOS()
} else {
tpl = templates.Get()
}
// Parse the templates
parsed, err = parsed.ParseFS(tpl, append(build.files, build.directories...)...)
if err != nil {
return nil, err
} }
// Store the template so this process only happens once // Store the template so this process only happens once

View File

@ -37,7 +37,7 @@ func TestTemplateRenderer(t *testing.T) {
expectedTemplates := make(map[string]bool) expectedTemplates := make(map[string]bool)
expectedTemplates["htmx"+config.TemplateExt] = true expectedTemplates["htmx"+config.TemplateExt] = true
expectedTemplates["error"+config.TemplateExt] = true expectedTemplates["error"+config.TemplateExt] = true
components, err := templates.Templates.ReadDir("components") components, err := templates.Get().ReadDir("components")
require.NoError(t, err) require.NoError(t, err)
for _, f := range components { for _, f := range components {
expectedTemplates[f.Name()] = true expectedTemplates[f.Name()] = true

View File

@ -2,7 +2,28 @@ package templates
import ( import (
"embed" "embed"
"io/fs"
"os"
"path"
"path/filepath"
"runtime"
) )
//go:embed * //go:embed *
var Templates embed.FS var templates embed.FS
// Get returns a file system containing all templates via embed.FS
func Get() embed.FS {
return templates
}
// GetOS returns a file system containing all templates which will load the files directly from the operating system.
// This should only be used for local development in order to faciliate live reloading.
func GetOS() fs.FS {
// Gets the complete templates directory path
// This is needed in case this is called from a package outside of main, such as within tests
_, b, _, _ := runtime.Caller(0)
d := path.Join(path.Dir(b))
p := filepath.Join(filepath.Dir(d), "templates")
return os.DirFS(p)
}

View File

@ -0,0 +1,17 @@
package templates
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGet(t *testing.T) {
_, err := Get().Open("pages/home.gohtml")
require.NoError(t, err)
}
func TestGetOS(t *testing.T) {
_, err := GetOS().Open("pages/home.gohtml")
require.NoError(t, err)
}