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

Commit

Permalink
removed local partial and integrated with plush's partial (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 authored and markbates committed Nov 1, 2018
1 parent a8eac54 commit e9e3594
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 70 deletions.
29 changes: 3 additions & 26 deletions render/js.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package render

import (
"html/template"
"strings"

"github.com/gobuffalo/plush"
"github.com/pkg/errors"
"github.com/markbates/oncer"
)

// JavaScript renders the named files using the 'application/javascript'
Expand Down Expand Up @@ -41,28 +38,8 @@ func (e *Engine) JavaScript(names ...string) Renderer {
}

// JSTemplateEngine renders files with a `.js` extension through Plush.
// It also implements a new `partial` helper that will run non-JS partials
// through `JSEscapeString` before injecting.
// Deprecated: use github.com/gobuffalo/plush.BuffaloRenderer instead.
func JSTemplateEngine(input string, data map[string]interface{}, helpers map[string]interface{}) (string, error) {
var pf partFunc
var ok bool
if pf, ok = helpers["partial"].(func(string, Data) (template.HTML, error)); !ok {
return "", errors.New("could not find a partial function")
}

helpers["partial"] = func(name string, dd Data) (template.HTML, error) {
if strings.Contains(name, ".js") {
return pf(name, dd)
}
h, err := pf(name, dd)
if err != nil {
return "", errors.WithStack(err)
}
he := template.JSEscapeString(string(h))
return template.HTML(he), nil
}

oncer.Deprecate(0, "render.JSTemplateEngine", "Use github.com/gobuffalo/plush.BuffaloRenderer instead.")
return plush.BuffaloRenderer(input, data, helpers)
}

type partFunc func(string, Data) (template.HTML, error)
31 changes: 25 additions & 6 deletions render/js_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package render_test
package render

import (
"bytes"
Expand All @@ -8,7 +8,6 @@ import (
"strings"
"testing"

"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr"
"github.com/stretchr/testify/require"
)
Expand All @@ -30,7 +29,7 @@ func Test_JavaScript(t *testing.T) {
t.Run("without a layout", func(st *testing.T) {
r := require.New(st)

j := render.New(render.Options{
j := New(Options{
TemplatesBox: packr.NewBox(tmpDir),
}).JavaScript

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

re := render.New(render.Options{
re := New(Options{
JavaScriptLayout: filepath.Base(layout.Name()),
TemplatesBox: packr.NewBox(tmpDir),
})
Expand Down Expand Up @@ -94,7 +93,7 @@ func Test_JavaScript_JS_Partial(t *testing.T) {
r.NoError(err)
defer os.RemoveAll(dir)

re := render.New(render.Options{
re := New(Options{
TemplatesBox: packr.NewBox(dir),
})

Expand All @@ -115,14 +114,34 @@ func Test_JavaScript_JS_Partial(t *testing.T) {
r.Equal("let a = 1;\nalert('hi!');", bb.String())
}

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

const testJS = "let a = 1;\n<%= partial(\"part\") %>"
const partJS = "alert('Hi <%= name %>!');"

err := withHTMLFile("test.js", testJS, func(e *Engine) {
err := withHTMLFile("_part.js", partJS, func(e *Engine) {
bb := &bytes.Buffer{}
renderer := e.JavaScript("test.js")
r.Equal("application/javascript", renderer.ContentType())
err := renderer.Render(bb, Data{"name": "Yonghwan"})
r.NoError(err)
r.Equal("let a = 1;\nalert('Hi Yonghwan!');", bb.String())
})
r.NoError(err)
})
r.NoError(err)
}

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

dir, err := ioutil.TempDir("", "")
r.NoError(err)
defer os.RemoveAll(dir)

re := render.New(render.Options{
re := New(Options{
TemplatesBox: packr.NewBox(dir),
})

Expand Down
54 changes: 54 additions & 0 deletions render/partials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,60 @@ func Test_Template_Partial_Form(t *testing.T) {

}

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

const forHTML = `<%= for(user) in users { %><%= partial("row") %><% } %>`
const rowHTML = `Hi <%= user.Name %>, `
const result = `Hi Mark, Hi Yonghwan,`

users := []struct {
Name string
}{{Name: "Mark"}, {Name: "Yonghwan"}}

err := withHTMLFile("for.html", forHTML, func(e *Engine) {
err := withHTMLFile("_row.html", rowHTML, func(e *Engine) {

re := e.Template("text/html; charset=utf-8", "for.html")
bb := &bytes.Buffer{}
err := re.Render(bb, Data{"users": users})
r.NoError(err)
r.Equal(result, strings.TrimSpace(bb.String()))

})
r.NoError(err)
})
r.NoError(err)

}

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

const forHTML = `<%= for(user) in users { %><%= partial("row", {say:"Hi"}) %><% } %>`
const rowHTML = `<%= say %> <%= user.Name %>, `
const result = `Hi Mark, Hi Yonghwan,`

users := []struct {
Name string
}{{Name: "Mark"}, {Name: "Yonghwan"}}

err := withHTMLFile("for.html", forHTML, func(e *Engine) {
err := withHTMLFile("_row.html", rowHTML, func(e *Engine) {

re := e.Template("text/html; charset=utf-8", "for.html")
bb := &bytes.Buffer{}
err := re.Render(bb, Data{"users": users})
r.NoError(err)
r.Equal(result, strings.TrimSpace(bb.String()))

})
r.NoError(err)
})
r.NoError(err)

}

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

Expand Down
2 changes: 1 addition & 1 deletion render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func New(opts Options) *Engine {
opts.TemplateEngines["txt"] = plush.BuffaloRenderer
}
if _, ok := opts.TemplateEngines["js"]; !ok {
opts.TemplateEngines["js"] = JSTemplateEngine
opts.TemplateEngines["js"] = plush.BuffaloRenderer
}
if _, ok := opts.TemplateEngines["md"]; !ok {
opts.TemplateEngines["md"] = MDTemplateEngine
Expand Down
60 changes: 23 additions & 37 deletions render/template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"fmt"
"html/template"
"io"
"os"
Expand All @@ -17,15 +16,13 @@ type templateRenderer struct {
*Engine
contentType string
names []string
data Data
}

func (s templateRenderer) ContentType() string {
return s.contentType
}

func (s *templateRenderer) Render(w io.Writer, data Data) error {
s.data = data
var body template.HTML
var err error
for _, name := range s.names {
Expand All @@ -39,39 +36,7 @@ func (s *templateRenderer) Render(w io.Writer, data Data) error {
return nil
}

func (s templateRenderer) partial(name string, dd Data) (template.HTML, error) {
d, f := filepath.Split(name)
name = filepath.Join(d, "_"+f)
m := Data{}
for k, v := range s.data {
m[k] = v
}
for k, v := range dd {
m[k] = v
}

if _, ok := m["layout"]; ok {

var body template.HTML
var err error

body, err = s.exec(name, m)
if err != nil {
return body, err
}
m["yield"] = body
d, f := filepath.Split(fmt.Sprintf("%v", m["layout"]))
name = filepath.Join(d, "_"+f)

}

return s.exec(name, m)
}

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

func fixExtension(name string, ct string) string {
if filepath.Ext(name) == "" {
switch {
case strings.Contains(ct, "html"):
Expand All @@ -82,6 +47,27 @@ func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
name += ".md"
}
}
return name
}

// partialFeeder returns template string for the name from `TemplateBox`.
// It should be registered as helper named `partialFeeder` so plush can
// find it with the name.
func (s templateRenderer) partialFeeder(name string) (string, error) {
ct := strings.ToLower(s.contentType)

d, f := filepath.Split(name)
name = filepath.Join(d, "_"+f)
name = fixExtension(name, ct)

return s.TemplatesBox.FindString(name)
}

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

name = fixExtension(name, ct)

// Try to use localized version
templateName := name
Expand Down Expand Up @@ -119,7 +105,7 @@ func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
}

helpers := map[string]interface{}{
"partial": s.partial,
"partialFeeder": s.partialFeeder,
}

helpers = s.addAssetsHelpers(helpers)
Expand Down

0 comments on commit e9e3594

Please sign in to comment.