Skip to content

Commit

Permalink
uses the new helpers package (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates authored Apr 16, 2019
1 parent 6c16d88 commit b5ff37b
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 476 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ build:

test:
packr
$(GO_BIN) test -tags ${TAGS} ./...
$(GO_BIN) test -cover -tags ${TAGS} ./...

ci-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
Expand Down
78 changes: 1 addition & 77 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,83 +264,7 @@ fmt.Print(s)

## Helpers

### Builtin Helpers

* `json` - converts the interface to a JSON object
* `jsEscape` - escapes the interface to be JavaScript safe
* `htmlEscape` - escapes the interface to be HTML safe
* `upcase` - converts the string to upper case
* `downcase` - converts the string to lower case
* `contentFor` - stores a block of HTML to be used later
* `contentOf` - retrieves a block of HTML previously stored with `contentFor`
* `markdown` - converts the string from Markdown into HTML
* `len` - returns the length of the interface
* `debug` - returns the `%+v` of the interface wrapped in `<pre>` tags.
* `inspect` - returns the `%+v` of the interface
* `range` - interate between, and including two numbers
* `between` - iterate between, but not including, two numbers
* `until` - iterate until a number is reached
* `groupBy` - splits a slice or array into `n` groups
* `env` - returns the ENV variable for the specified key
* `truncate` - truncates a string to a specified length
* `form` - support for the [github.com/gobuffalo/tags/form](https://github.com/gobuffalo/tags/tree/master/form) package (Bootstrap version)
* `form_for` - support for the [github.com/gobuffalo/tags/form](https://github.com/gobuffalo/tags/tree/master/form) package (Bootstrap version) to build a form for a model

#### contentFor and contentOf

Use the `contentFor` and `contentOf` helpers to dry up your templates with reusable components.

For example, we can define a snippet that generates a fancy title using `contentFor`:

```
<% contentFor("fancy-title") { %>
<h1 class='fancy'><%= title %></h1>
<% } %>
```

The `fancy-title` name is how we will invoke this with `contentOf` elsewhere
in our template:

```
<%= contentOf("fancy-title", {"title":"Welcome to Plush"}) %>
```

* The second map argument is optional, for static content just use `<%= contentOf("fancy-title") %>`

Rendering this would generate this output:

```
<h1 class='fancy'>Welcome to Plush</h1>
```

As you can see, the `<%= title %>` has been replaced with the `Welcome to Plush` string.

#### truncate

`truncate` takes two optional parameters:
* `size` - the maximum length of the returned string
* `trail` - the string to append at the end of a truncated string, defaults to `...`

```html
<p><%= truncate("a long string", {"size": 10, "trail": "[more]"})%></p>
```

### From github.com/markbates/inflect

* `asciffy`
* `camelize`
* `camelize_down_first`
* `capitalize`
* `dasherize`
* `humanize`
* `ordinalize`
* `parameterize`
* `pluralize`
* `pluralize_with_size`
* `singularize`
* `tableize`
* `typeify`
* `underscore`
For a full list, and documentation of, all the Helpers included in Plush, see [`github.com/gobuffalo/helpers`](https://godoc.org/github.com/gobuffalo/helpers).

### Custom Helpers

Expand Down
10 changes: 6 additions & 4 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"regexp"
"time"

"github.com/gobuffalo/helpers/hctx"
"github.com/gobuffalo/plush/ast"
"github.com/pkg/errors"
)

var ErrUnknownIdentifier = errors.New("unknown identifier")

type compiler struct {
ctx *Context
ctx hctx.Context
program *ast.Program
curStmt ast.Statement
}
Expand Down Expand Up @@ -525,7 +526,8 @@ func (c *compiler) evalCallExpression(node *ast.CallExpression) (interface{}, er
}

hc := func(arg reflect.Type) {
if arg.ConvertibleTo(reflect.TypeOf(HelperContext{})) {
hhc := reflect.TypeOf((*hctx.HelperContext)(nil)).Elem()
if arg.ConvertibleTo(reflect.TypeOf(HelperContext{})) || arg.Implements(hhc) {
hargs := HelperContext{
Context: c.ctx,
compiler: c,
Expand Down Expand Up @@ -634,14 +636,14 @@ func (c *compiler) evalCallExpression(node *ast.CallExpression) (interface{}, er
}

func (c *compiler) evalForExpression(node *ast.ForExpression) (interface{}, error) {
octx := c.ctx
octx := c.ctx.(*Context)
defer func() {
c.ctx = octx
}()
c.ctx = octx.New()
// must copy all data from original (it includes application defined helpers)
for k, v := range octx.data {
c.ctx.data[k] = v
c.ctx.Set(k, v)
}

iter, err := c.evalExpression(node.Iterable)
Expand Down
57 changes: 0 additions & 57 deletions content_helper.go

This file was deleted.

79 changes: 0 additions & 79 deletions content_helper_test.go

This file was deleted.

4 changes: 3 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package plush
import (
"context"
"sync"

"github.com/gobuffalo/helpers/hctx"
)

var _ context.Context = &Context{}
Expand All @@ -18,7 +20,7 @@ type Context struct {
// New context containing the current context. Values set on the new context
// will not be set onto the original context, however, the original context's
// values will be available to the new context.
func (c *Context) New() *Context {
func (c *Context) New() hctx.Context {
cc := NewContext()
cc.outer = c
return cc
Expand Down
72 changes: 10 additions & 62 deletions forms.go
Original file line number Diff line number Diff line change
@@ -1,70 +1,18 @@
package plush

import (
"fmt"
"html/template"

"github.com/gobuffalo/tags"
"github.com/gobuffalo/tags/form"
"github.com/gobuffalo/tags/form/bootstrap"
"github.com/gobuffalo/helpers/forms"
"github.com/gobuffalo/helpers/forms/bootstrap"
)

// FormHelper implements a Plush helper around the
// form.New function in the github.com/gobuffalo/tags/form package
func FormHelper(opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return form.New(opts)
})
}

// FormForHelper implements a Plush helper around the
// form.NewFormFor function in the github.com/gobuffalo/tags/form package
func FormForHelper(model interface{}, opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return form.NewFormFor(model, opts)
})
}

// BootstrapFormHelper implements a Plush helper around the
// bootstrap.New function in the github.com/gobuffalo/tags/form/bootstrap package
func BootstrapFormHelper(opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return bootstrap.New(opts)
})
}
// FormHelper is deprecated use github.com/gobuffalo/helpers/forms#Form instead.
var FormHelper = forms.Form

// BootstrapFormForHelper implements a Plush helper around the
// bootstrap.NewFormFor function in the github.com/gobuffalo/tags/form/bootstrap package
func BootstrapFormForHelper(model interface{}, opts tags.Options, help HelperContext) (template.HTML, error) {
return helper(opts, help, func(opts tags.Options) helperable {
return bootstrap.NewFormFor(model, opts)
})
}
// FormForHelper is deprecated use github.com/gobuffalo/helpers/forms#FormFor instead.
var FormForHelper = forms.FormFor

type helperable interface {
SetAuthenticityToken(string)
Append(...tags.Body)
HTMLer
}
// BootstrapFormHelper is deprecated use github.com/gobuffalo/helpers/forms/bootstrap#Form instead.
var BootstrapFormHelper = bootstrap.Form

func helper(opts tags.Options, help HelperContext, fn func(opts tags.Options) helperable) (template.HTML, error) {
hn := "f"
if n, ok := opts["var"]; ok {
hn = n.(string)
delete(opts, "var")
}
if opts["errors"] == nil && help.Context.Value("errors") != nil {
opts["errors"] = help.Context.Value("errors")
}
form := fn(opts)
if help.Value("authenticity_token") != nil && opts["method"] != "GET" {
form.SetAuthenticityToken(fmt.Sprint(help.Value("authenticity_token")))
}
help.Context.Set(hn, form)
s, err := help.Block()
if err != nil {
return "", err
}
form.Append(s)
return form.HTML(), nil
}
// BootstrapFormForHelper is deprecated use github.com/gobuffalo/helpers/forms/bootstrap#FormFor instead.
var BootstrapFormForHelper = bootstrap.FormFor
Loading

0 comments on commit b5ff37b

Please sign in to comment.