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

Commit

Permalink
Merge pull request #761 from gobuffalo/partial-extensions
Browse files Browse the repository at this point in the history
no longer need extensions for templates in actions or partials for HTML, JS, and Markdown
  • Loading branch information
markbates authored Nov 17, 2017
2 parents 7111498 + ec39280 commit a08810f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
13 changes: 9 additions & 4 deletions render/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package render_test

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -14,7 +14,12 @@ import (
func Test_HTML(t *testing.T) {
r := require.New(t)

tmpFile, err := ioutil.TempFile("", "test")
tmpDir := filepath.Join(os.TempDir(), "html_test")
err := os.MkdirAll(tmpDir, 0766)
r.NoError(err)
defer os.Remove(tmpDir)

tmpFile, err := os.Create(filepath.Join(tmpDir, "test.html"))
r.NoError(err)
defer os.Remove(tmpFile.Name())

Expand All @@ -37,7 +42,7 @@ func Test_HTML(t *testing.T) {
t.Run("with a layout", func(st *testing.T) {
r := require.New(st)

layout, err := ioutil.TempFile("", "test")
layout, err := os.Create(filepath.Join(tmpDir, "layout.html"))
r.NoError(err)
defer os.Remove(layout.Name())

Expand All @@ -61,7 +66,7 @@ func Test_HTML(t *testing.T) {

st.Run("overriding the HTMLLayout", func(sst *testing.T) {
r := require.New(sst)
nlayout, err := ioutil.TempFile("", "test-layout2")
nlayout, err := os.Create(filepath.Join(tmpDir, "layout2.html"))
r.NoError(err)
defer os.Remove(nlayout.Name())

Expand Down
14 changes: 8 additions & 6 deletions render/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ import (
func Test_JavaScript(t *testing.T) {
r := require.New(t)

tmpFile, err := ioutil.TempFile("", "test")
tmpDir := filepath.Join(os.TempDir(), "markdown_test")
err := os.MkdirAll(tmpDir, 0766)
r.NoError(err)
defer os.Remove(tmpDir)

tmpFile, err := os.Create(filepath.Join(tmpDir, "test.js"))
r.NoError(err)
defer os.Remove(tmpFile.Name())

_, err = tmpFile.Write([]byte("<%= name %>"))
r.NoError(err)
Expand All @@ -39,9 +43,8 @@ func Test_JavaScript(t *testing.T) {
t.Run("with a layout", func(st *testing.T) {
r := require.New(st)

layout, err := ioutil.TempFile("", "test")
layout, err := os.Create(filepath.Join(tmpDir, "layout.js"))
r.NoError(err)
defer os.Remove(layout.Name())

_, err = layout.Write([]byte("<body><%= yield %></body>"))
r.NoError(err)
Expand All @@ -63,9 +66,8 @@ func Test_JavaScript(t *testing.T) {

st.Run("overriding the JavaScriptLayout", func(sst *testing.T) {
r := require.New(sst)
nlayout, err := ioutil.TempFile("", "test-layout2")
nlayout, err := os.Create(filepath.Join(tmpDir, "layout2.js"))
r.NoError(err)
defer os.Remove(nlayout.Name())

_, err = nlayout.Write([]byte("<html><%= yield %></html>"))
r.NoError(err)
Expand Down
3 changes: 1 addition & 2 deletions render/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package render_test

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -47,7 +46,7 @@ func Test_Markdown(t *testing.T) {
t.Run("with a layout", func(st *testing.T) {
r := require.New(st)

layout, err := ioutil.TempFile("", "test")
layout, err := os.Create(filepath.Join("", "test.html"))
r.NoError(err)
defer os.Remove(layout.Name())

Expand Down
16 changes: 14 additions & 2 deletions render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,25 @@ func (s templateRenderer) partial(name string, dd Data) (template.HTML, error) {
}

func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
ct := strings.ToLower(s.contentType)
data["contentType"] = ct

if filepath.Ext(name) == "" {
switch {
case strings.Contains(ct, "html"):
name += ".html"
case strings.Contains(ct, "javascript"):
name += ".js"
case strings.Contains(ct, "markdown"):
name += ".md"
}
}

source, err := s.TemplatesBox.MustBytes(name)
if err != nil {
return "", err
}

data["contentType"] = strings.ToLower(s.contentType)

helpers := map[string]interface{}{
"partial": s.partial,
}
Expand Down
60 changes: 44 additions & 16 deletions render/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,48 @@ func Test_Template_Partial(t *testing.T) {

type ji func(string, ...string) render.Renderer

table := []ji{
render.New(render.Options{
TemplatesBox: packr.NewBox(tPath),
}).Template,
}
j := render.New(render.Options{
TemplatesBox: packr.NewBox(tPath),
}).Template

for _, j := range table {
re := j("foo/bar", "index.html")
r.Equal("foo/bar", re.ContentType())
bb := &bytes.Buffer{}
err = re.Render(bb, render.Data{"name": "Mark"})
r.NoError(err)
r.Equal("Foo > Mark", strings.TrimSpace(bb.String()))
}
re := j("foo/bar", "index.html")
r.Equal("foo/bar", re.ContentType())
bb := &bytes.Buffer{}
err = re.Render(bb, render.Data{"name": "Mark"})
r.NoError(err)
r.Equal("Foo > Mark", strings.TrimSpace(bb.String()))
}

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

tPath, err := ioutil.TempDir("", "")
r.NoError(err)
defer os.Remove(tPath)

partFile, err := os.Create(filepath.Join(tPath, "_foo.html"))
r.NoError(err)

_, err = partFile.Write([]byte("Foo > <%= name %>"))
r.NoError(err)

tmpFile, err := os.Create(filepath.Join(tPath, "index.html"))
r.NoError(err)

_, err = tmpFile.Write([]byte(`<%= partial("foo") %>`))
r.NoError(err)

type ji func(string, ...string) render.Renderer

j := render.New(render.Options{
TemplatesBox: packr.NewBox(tPath),
}).HTML

re := j("index.html")
bb := &bytes.Buffer{}
err = re.Render(bb, render.Data{"name": "Mark"})
r.NoError(err)
r.Equal("Foo > Mark", strings.TrimSpace(bb.String()))
}

func Test_AssetPath(t *testing.T) {
Expand Down Expand Up @@ -101,7 +129,7 @@ func Test_AssetPath(t *testing.T) {

for original, expected := range cases {

tmpFile, err := ioutil.TempFile(tdir, "test")
tmpFile, err := os.Create(filepath.Join(tdir, "test.html"))
r.NoError(err)

_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
Expand Down Expand Up @@ -136,7 +164,7 @@ func Test_AssetPathNoManifest(t *testing.T) {

for original, expected := range cases {

tmpFile, err := ioutil.TempFile(tdir, "test")
tmpFile, err := os.Create(filepath.Join(tdir, "test.html"))
r.NoError(err)

_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
Expand Down Expand Up @@ -171,7 +199,7 @@ func Test_AssetPathManifestCorrupt(t *testing.T) {

for original, expected := range cases {

tmpFile, err := ioutil.TempFile(tdir, "test")
tmpFile, err := os.Create(filepath.Join(tdir, "test.html"))
r.NoError(err)

_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
Expand Down

0 comments on commit a08810f

Please sign in to comment.