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
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Oct 31, 2018
1 parent 617e05a commit 848df35
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 65 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)
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
61 changes: 23 additions & 38 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 Down Expand Up @@ -37,31 +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)

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

var body template.HTML
var err error

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

}
return s.exec(name, dd)
}

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 @@ -72,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 @@ -109,18 +105,7 @@ func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
}

helpers := map[string]interface{}{
"partial": func(name string, dd Data) (template.HTML, error) {
// call the partial copying the current context data,
// and any new args, before calling the partial helper
m := Data{}
for k, v := range data {
m[k] = v
}
for k, v := range dd {
m[k] = v
}
return s.partial(name, m)
},
"partialFeeder": s.partialFeeder,
}

helpers = s.addAssetsHelpers(helpers)
Expand Down

0 comments on commit 848df35

Please sign in to comment.