Skip to content

Commit

Permalink
Replace template dir file path hack with embed directive.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestefanello committed Dec 10, 2023
1 parent 0879aaf commit 3df20c0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mikestefanello/pagoda

go 1.19
go 1.21

require (
entgo.io/ent v0.12.5
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 6 additions & 24 deletions pkg/services/template_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -124,19 +106,19 @@ 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
}
}

// 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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/template_renderer_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions templates/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package templates

import (
"embed"
)

//go:embed *
var Templates embed.FS

0 comments on commit 3df20c0

Please sign in to comment.