Skip to content

Commit

Permalink
feat: add str2html to built-in HTML template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aofei committed Mar 11, 2021
1 parent 02b9249 commit 02db429
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
8 changes: 5 additions & 3 deletions air.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ type Air struct {
// The HTML template functions described in
// https://pkg.go.dev/text/template#hdr-Functions and the following are
// always available:
// * locstr
// Returns a localized string for its argument. It works exactly
// the same as the `Request.LocalizedString`
// * str2html
// Returns a `template.HTML` for its argument.
// * strlen
// Returns the number of characters of its argument.
// * substr
Expand All @@ -429,9 +434,6 @@ type Air struct {
// * timefmt
// Returns a textual representation of its first argument for the
// time layout (the second argument).
// * locstr
// Returns a localized string for its argument. It works exactly
// the same as the `Request.LocalizedString`
//
// Default value: nil
RendererTemplateFuncMap template.FuncMap `mapstructure:"-"`
Expand Down
24 changes: 15 additions & 9 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ func (r *renderer) load() {
r.a.RendererTemplateRightDelim,
).
Funcs(template.FuncMap{
"strlen": strlen,
"substr": substr,
"timefmt": timefmt,
"locstr": locstr,
"locstr": locstr,
"str2html": str2html,
"strlen": strlen,
"substr": substr,
"timefmt": timefmt,
}).
Funcs(r.a.RendererTemplateFuncMap)
if r.loadError = filepath.Walk(
Expand Down Expand Up @@ -142,6 +143,16 @@ func (r *renderer) render(
}).Execute(w, v)
}

// locstr returns the key without any changes.
func locstr(key string) string {
return key
}

// str2html returns a `template.HTML` for the s.
func str2html(s string) template.HTML {
return template.HTML(s)
}

// strlen returns the number of characters of the s.
func strlen(s string) int {
return len([]rune(s))
Expand All @@ -158,8 +169,3 @@ func substr(s string, i, j int) string {
func timefmt(t time.Time, layout string) string {
return t.Format(layout)
}

// locstr returns the key without any changes.
func locstr(key string) string {
return key
}
13 changes: 9 additions & 4 deletions renderer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package air

import (
"html/template"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -78,6 +79,14 @@ func TestRendererRender(t *testing.T) {
assert.NoError(t, r.render(ioutil.Discard, "test.html", nil, locstr))
}

func TestLocstr(t *testing.T) {
assert.Equal(t, "Foobar", locstr("Foobar"))
}

func TestStr2html(t *testing.T) {
assert.Equal(t, template.HTML("Foobar"), str2html("Foobar"))
}

func TestStrlen(t *testing.T) {
assert.Equal(t, 6, strlen("Foobar"))
assert.Equal(t, 2, strlen("测试"))
Expand All @@ -95,7 +104,3 @@ func TestTimefmt(t *testing.T) {
timefmt(time.Unix(0, 0).UTC(), time.RFC3339),
)
}

func TestLocstr(t *testing.T) {
assert.Equal(t, "Foobar", locstr("Foobar"))
}

0 comments on commit 02db429

Please sign in to comment.