Replace template dir file path hack with embed directive.
This commit is contained in:
parent
0879aaf21d
commit
3df20c01a7
@ -904,7 +904,7 @@ If the current [environment](#environments) is set to `config.EnvLocal`, which i
|
||||
|
||||
### File configuration
|
||||
|
||||
To make things easier and less repetitive, parameters given to the _template renderer_ must not include the `templates` directory or the template file extensions. These are stored as constants within the `config` package. If your project has a need to change either of these, simply adjust the `TemplateDir` and `TemplateExt` constants.
|
||||
To make things easier and less repetitive, parameters given to the _template renderer_ must not include the `templates` directory or the template file extensions. The file extension is stored as a constant (`TemplateExt`) within the `config` package.
|
||||
|
||||
## Funcmap
|
||||
|
||||
|
@ -9,9 +9,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// TemplateDir stores the name of the directory that contains templates
|
||||
TemplateDir = "../templates"
|
||||
|
||||
// TemplateExt stores the extension used for the template files
|
||||
TemplateExt = ".gohtml"
|
||||
|
||||
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module github.com/mikestefanello/pagoda
|
||||
|
||||
go 1.19
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
entgo.io/ent v0.12.5
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"github.com/mikestefanello/pagoda/pkg/middleware"
|
||||
"github.com/mikestefanello/pagoda/pkg/services"
|
||||
"github.com/mikestefanello/pagoda/pkg/tests"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -98,7 +99,7 @@ func TestController_RenderPage(t *testing.T) {
|
||||
expectedTemplates := make(map[string]bool)
|
||||
expectedTemplates[p.Name+config.TemplateExt] = true
|
||||
expectedTemplates[p.Layout+config.TemplateExt] = true
|
||||
components, err := os.ReadDir(c.TemplateRenderer.GetTemplatesPath() + "/components")
|
||||
components, err := templates.Templates.ReadDir("components")
|
||||
require.NoError(t, err)
|
||||
for _, f := range components {
|
||||
expectedTemplates[f.Name()] = true
|
||||
@ -131,7 +132,7 @@ func TestController_RenderPage(t *testing.T) {
|
||||
expectedTemplates := make(map[string]bool)
|
||||
expectedTemplates[p.Name+config.TemplateExt] = true
|
||||
expectedTemplates["htmx"+config.TemplateExt] = true
|
||||
components, err := os.ReadDir(c.TemplateRenderer.GetTemplatesPath() + "/components")
|
||||
components, err := templates.Templates.ReadDir("components")
|
||||
require.NoError(t, err)
|
||||
for _, f := range components {
|
||||
expectedTemplates[f.Name()] = true
|
||||
|
@ -5,13 +5,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
"github.com/mikestefanello/pagoda/pkg/funcmap"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -24,9 +22,6 @@ type (
|
||||
// funcMap stores the template function map
|
||||
funcMap template.FuncMap
|
||||
|
||||
// templatePath stores the complete path to the templates directory
|
||||
templatesPath string
|
||||
|
||||
// config stores application configuration
|
||||
config *config.Config
|
||||
}
|
||||
@ -58,19 +53,11 @@ type (
|
||||
|
||||
// NewTemplateRenderer creates a new TemplateRenderer
|
||||
func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer {
|
||||
t := &TemplateRenderer{
|
||||
return &TemplateRenderer{
|
||||
templateCache: sync.Map{},
|
||||
funcMap: funcmap.GetFuncMap(),
|
||||
config: cfg,
|
||||
}
|
||||
|
||||
// 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))
|
||||
t.templatesPath = filepath.Join(filepath.Dir(d), config.TemplateDir)
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
// Parse creates a template build operation
|
||||
@ -81,11 +68,6 @@ func (t *TemplateRenderer) Parse() *templateBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
// GetTemplatesPath gets the complete path to the templates directory
|
||||
func (t *TemplateRenderer) GetTemplatesPath() string {
|
||||
return t.templatesPath
|
||||
}
|
||||
|
||||
// getCacheKey gets a cache key for a given group and ID
|
||||
func (t *TemplateRenderer) getCacheKey(group, key string) string {
|
||||
if group != "" {
|
||||
@ -124,10 +106,10 @@ func (t *TemplateRenderer) parse(build *templateBuild) (*TemplateParsed, error)
|
||||
// Parse all files provided
|
||||
if len(build.files) > 0 {
|
||||
for k, v := range build.files {
|
||||
build.files[k] = fmt.Sprintf("%s/%s%s", t.templatesPath, v, config.TemplateExt)
|
||||
build.files[k] = fmt.Sprintf("%s%s", v, config.TemplateExt)
|
||||
}
|
||||
|
||||
parsed, err = parsed.ParseFiles(build.files...)
|
||||
parsed, err = parsed.ParseFS(templates.Templates, build.files...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -135,8 +117,8 @@ func (t *TemplateRenderer) parse(build *templateBuild) (*TemplateParsed, error)
|
||||
|
||||
// Parse all templates within the provided directories
|
||||
for _, dir := range build.directories {
|
||||
dir = fmt.Sprintf("%s/%s/*%s", t.templatesPath, dir, config.TemplateExt)
|
||||
parsed, err = parsed.ParseGlob(dir)
|
||||
dir = fmt.Sprintf("%s/*%s", dir, config.TemplateExt)
|
||||
parsed, err = parsed.ParseFS(templates.Templates, dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
"github.com/mikestefanello/pagoda/templates"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -37,7 +37,7 @@ func TestTemplateRenderer(t *testing.T) {
|
||||
expectedTemplates := make(map[string]bool)
|
||||
expectedTemplates["htmx"+config.TemplateExt] = true
|
||||
expectedTemplates["error"+config.TemplateExt] = true
|
||||
components, err := os.ReadDir(c.TemplateRenderer.GetTemplatesPath() + "/components")
|
||||
components, err := templates.Templates.ReadDir("components")
|
||||
require.NoError(t, err)
|
||||
for _, f := range components {
|
||||
expectedTemplates[f.Name()] = true
|
||||
|
8
templates/templates.go
Normal file
8
templates/templates.go
Normal file
@ -0,0 +1,8 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
//go:embed *
|
||||
var Templates embed.FS
|
Loading…
Reference in New Issue
Block a user