Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
fixed template renderer to make correct aliases especially for lang f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
sio4 committed May 3, 2022
1 parent 7390982 commit ba856af
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 16 deletions.
18 changes: 2 additions & 16 deletions render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,8 @@ func (s *templateRenderer) updateAliases() error {
return nil
}

base := filepath.Base(path)
dir := filepath.Dir(path)

var exts []string
sep := strings.Split(base, ".")
if len(sep) >= 1 {
base = sep[0]
}
if len(sep) > 1 {
exts = sep[1:]
}

for _, ext := range exts {
pn := filepath.Join(dir, base+"."+ext)
s.aliases.Store(pn, path)
}
shortcut := strings.Replace(path, ".plush.", ".", 1)
s.aliases.Store(shortcut, path)

return nil
})
Expand Down
115 changes: 115 additions & 0 deletions render/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,118 @@ func Test_AssetPathNoManifestCorrupt(t *testing.T) {
r.NotEqual(expected, strings.TrimSpace(bb.String()))
}
}

/* test if i18n files (both with plush mid-extension and latecy) proceeded correctly.
*/
func Test_Template_resolve_DefaultLang_Plush(t *testing.T) {
r := require.New(t)

rootFS := memfs.New()
r.NoError(rootFS.WriteFile("index.plush.html", []byte("default <%= name %>"), 0644))
r.NoError(rootFS.WriteFile("index.plush.ko-kr.html", []byte("korean <%= name %>"), 0644))

e := NewEngine()
e.TemplatesFS = rootFS

re := e.Template("foo/bar", "index.plush.html")
r.Equal("foo/bar", re.ContentType())

bb := &bytes.Buffer{}
r.NoError(re.Render(bb, Data{"name": "Paul", "languages": []string{"es", "en"}}))
r.Equal("default Paul", strings.TrimSpace(bb.String()))
}

func Test_Template_resolve_UserLang_Plush(t *testing.T) {
r := require.New(t)

rootFS := memfs.New()
r.NoError(rootFS.WriteFile("index.plush.html", []byte("default <%= name %>"), 0644))
r.NoError(rootFS.WriteFile("index.plush.ko-kr.html", []byte("korean <%= name %>"), 0644))

e := NewEngine()
e.TemplatesFS = rootFS

re := e.Template("foo/bar", "index.plush.html")
r.Equal("foo/bar", re.ContentType())

bb := &bytes.Buffer{}
r.NoError(re.Render(bb, Data{"name": "Paul", "languages": []string{"ko-KR", "en"}}))
r.Equal("korean Paul", strings.TrimSpace(bb.String()))
}

func Test_Template_resolve_DefaultLang_Legacy(t *testing.T) {
r := require.New(t)

rootFS := memfs.New()
r.NoError(rootFS.WriteFile("index.html", []byte("default <%= name %>"), 0644))
r.NoError(rootFS.WriteFile("index.ko-kr.html", []byte("korean <%= name %>"), 0644))

e := NewEngine()
e.TemplatesFS = rootFS

re := e.Template("foo/bar", "index.html")
r.Equal("foo/bar", re.ContentType())

bb := &bytes.Buffer{}
r.NoError(re.Render(bb, Data{"name": "Paul", "languages": []string{"es", "en"}}))
r.Equal("default Paul", strings.TrimSpace(bb.String()))
}

func Test_Template_resolve_UserLang_Legacy(t *testing.T) {
r := require.New(t)

rootFS := memfs.New()
r.NoError(rootFS.WriteFile("index.html", []byte("default <%= name %>"), 0644))
r.NoError(rootFS.WriteFile("index.ko-kr.html", []byte("korean <%= name %>"), 0644))

e := NewEngine()
e.TemplatesFS = rootFS

re := e.Template("foo/bar", "index.html")
r.Equal("foo/bar", re.ContentType())

bb := &bytes.Buffer{}
r.NoError(re.Render(bb, Data{"name": "Paul", "languages": []string{"ko-KR", "en"}}))
r.Equal("korean Paul", strings.TrimSpace(bb.String()))
}

func Test_Template_resolve_DefaultLang_Mixed(t *testing.T) {
r := require.New(t)

rootFS := memfs.New()
r.NoError(rootFS.WriteFile("index.plush.html", []byte("default <%= name %>"), 0644))
r.NoError(rootFS.WriteFile("index.plush.ko-kr.html", []byte("korean <%= name %>"), 0644))

e := NewEngine()
e.TemplatesFS = rootFS

// `buffalo fix` renames templates but does not fix actions
// in this case, aliases will be used for template matching
re := e.Template("foo/bar", "index.html")
r.Equal("foo/bar", re.ContentType())

bb := &bytes.Buffer{}
r.NoError(re.Render(bb, Data{"name": "Paul", "languages": []string{"es", "en"}}))
r.Equal("default Paul", strings.TrimSpace(bb.String()))
}

func Test_Template_resolve_UserLang_Mixed(t *testing.T) {
r := require.New(t)

rootFS := memfs.New()
r.NoError(rootFS.WriteFile("index.plush.html", []byte("default <%= name %>"), 0644))
r.NoError(rootFS.WriteFile("index.plush.ko-kr.html", []byte("korean <%= name %>"), 0644))

e := NewEngine()
e.TemplatesFS = rootFS

// `buffalo fix` renames templates but does not fix actions
// in this case, aliases will be used for template matching
re := e.Template("foo/bar", "index.html")
r.Equal("foo/bar", re.ContentType())

bb := &bytes.Buffer{}
r.NoError(re.Render(bb, Data{"name": "Paul", "languages": []string{"ko-KR", "en"}}))
r.Equal("korean Paul", strings.TrimSpace(bb.String()))
}

0 comments on commit ba856af

Please sign in to comment.