Compare commits
No commits in common. "master" and "v0.1.0" have entirely different histories.
24
README.org
24
README.org
@ -10,30 +10,6 @@ https://pypi.org/project/template-nest/.
|
|||||||
|
|
||||||
* News
|
* News
|
||||||
|
|
||||||
** v0.1.6 - 2025-12-02
|
|
||||||
|
|
||||||
+ Treat empty hash as nil
|
|
||||||
|
|
||||||
Tom: I think that if go's behaviour is to insert an empty map when nil is
|
|
||||||
returned as a templatenest.Hash, then we need to change the templatenest
|
|
||||||
behaviour to regard an empty map as being nil.
|
|
||||||
|
|
||||||
** v0.1.5 - 2025-02-22
|
|
||||||
|
|
||||||
+ Handle all pointer types generically.
|
|
||||||
|
|
||||||
** v0.1.4 - 2025-01-09
|
|
||||||
|
|
||||||
+ Fix nil pointer dereference error.
|
|
||||||
|
|
||||||
** v0.1.3 - 2024-11-24
|
|
||||||
|
|
||||||
+ Fix handling of numeric types.
|
|
||||||
|
|
||||||
Earlier numeric values would result in an error (value type not supported),
|
|
||||||
this was due to a programming error. I've fixed the error and added support
|
|
||||||
for more numeric types.
|
|
||||||
|
|
||||||
** v0.1.0 - 2024-11-18
|
** v0.1.0 - 2024-11-18
|
||||||
|
|
||||||
+ Initial Release.
|
+ Initial Release.
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -279,19 +278,6 @@ func (nest *TemplateNest) MustRender(toRender interface{}) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
||||||
if toRender == nil {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle pointer types generically
|
|
||||||
if reflect.TypeOf(toRender).Kind() == reflect.Ptr {
|
|
||||||
if reflect.ValueOf(toRender).IsNil() {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
// Dereference the pointer and recursively call Render
|
|
||||||
return nest.Render(reflect.ValueOf(toRender).Elem().Interface())
|
|
||||||
}
|
|
||||||
|
|
||||||
if reflect.TypeOf(toRender).Kind() == reflect.Slice {
|
if reflect.TypeOf(toRender).Kind() == reflect.Slice {
|
||||||
return nest.renderSlice(toRender)
|
return nest.renderSlice(toRender)
|
||||||
}
|
}
|
||||||
@ -309,30 +295,8 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
|||||||
}
|
}
|
||||||
return html.EscapeString(v), nil
|
return html.EscapeString(v), nil
|
||||||
|
|
||||||
case float32:
|
case float64, int, int64:
|
||||||
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
|
return fmt.Sprintf("%v", v), nil
|
||||||
case float64:
|
|
||||||
return strconv.FormatFloat(v, 'f', -1, 64), nil
|
|
||||||
case int:
|
|
||||||
return strconv.Itoa(v), nil
|
|
||||||
case int8:
|
|
||||||
return strconv.FormatInt(int64(v), 10), nil
|
|
||||||
case int16:
|
|
||||||
return strconv.FormatInt(int64(v), 10), nil
|
|
||||||
case int32:
|
|
||||||
return strconv.FormatInt(int64(v), 10), nil
|
|
||||||
case int64:
|
|
||||||
return strconv.FormatInt(v, 10), nil
|
|
||||||
case uint:
|
|
||||||
return strconv.FormatUint(uint64(v), 10), nil
|
|
||||||
case uint8:
|
|
||||||
return strconv.FormatUint(uint64(v), 10), nil
|
|
||||||
case uint16:
|
|
||||||
return strconv.FormatUint(uint64(v), 10), nil
|
|
||||||
case uint32:
|
|
||||||
return strconv.FormatUint(uint64(v), 10), nil
|
|
||||||
case uint64:
|
|
||||||
return strconv.FormatUint(v, 10), nil
|
|
||||||
|
|
||||||
case Hash:
|
case Hash:
|
||||||
return nest.renderHash(v)
|
return nest.renderHash(v)
|
||||||
@ -341,7 +305,7 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
|||||||
return nest.renderHash(v)
|
return nest.renderHash(v)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("unsupported template hash value type: %T: %+v", v, v)
|
return "", fmt.Errorf("unsupported template hash value type: %+v", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,15 +328,10 @@ func (nest *TemplateNest) renderSlice(toRender interface{}) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (nest *TemplateNest) renderHash(hash map[string]interface{}) (string, error) {
|
func (nest *TemplateNest) renderHash(hash map[string]interface{}) (string, error) {
|
||||||
// If the hash is completely empty, return an empty string.
|
|
||||||
if len(hash) == 0 {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
tLabel, ok := hash[*nest.option.NameLabel]
|
tLabel, ok := hash[*nest.option.NameLabel]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
"encountered hash with no name label (name label: `%+v`)", nest.option.NameLabel,
|
"encountered hash with no name label (name label: `%s`)", nest.option.NameLabel,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -174,19 +174,6 @@ func TestRenderNoNameLabel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderEmptyHash(t *testing.T) {
|
|
||||||
nest, err := templatenest.New(templatenest.Option{TemplateDir: "templates"})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to initialize TemplateNest: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
page := templatenest.Hash{}
|
|
||||||
|
|
||||||
render, err := nest.Render(page)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.Equal(t, "", render)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRenderSimplePageArrays(t *testing.T) {
|
func TestRenderSimplePageArrays(t *testing.T) {
|
||||||
nest, err := templatenest.New(templatenest.Option{
|
nest, err := templatenest.New(templatenest.Option{
|
||||||
TemplateDir: "templates",
|
TemplateDir: "templates",
|
||||||
|
|||||||
@ -79,10 +79,10 @@ func TestRenderWithEscapedVariableAtStart01(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
page := templatenest.Hash{
|
page := templatenest.Hash{
|
||||||
"TEMPLATE": "03-var-at-begin-with-escape",
|
"TEMPLATE": "03-var-at-begin-with-space",
|
||||||
}
|
}
|
||||||
|
|
||||||
outputPath := "templates/output/10-var-at-begin-with-escape.html"
|
outputPath := "templates/output/10-var-at-begin-with-space.html"
|
||||||
outputContents, err := ioutil.ReadFile(outputPath)
|
outputContents, err := ioutil.ReadFile(outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error reading file (`%s`): %+v", outputPath, err)
|
t.Fatalf("error reading file (`%s`): %+v", outputPath, err)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user