Author SHA1 Message Date
andinus 62a63d786a Handle *string 2025-02-08 18:36:36 +05:30
andinus 0c7fd9a814 Release v0.1.4 2025-01-09 21:48:44 +05:30
andinus b82d9089bd Fix nil pointer dereference 2025-01-09 21:42:22 +05:30
andinus f0b9ab0d63 Fix handling of numeric types 2024-11-24 14:20:42 +05:30
andinus 8e83ac046b Update test file name 2024-11-18 22:25:17 +05:30
5 changed files with 50 additions and 5 deletions
+12
View File
@@ -10,6 +10,18 @@ https://pypi.org/project/template-nest/.
* News * News
** 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.
+36 -3
View File
@@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
) )
@@ -278,6 +279,10 @@ 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
}
if reflect.TypeOf(toRender).Kind() == reflect.Slice { if reflect.TypeOf(toRender).Kind() == reflect.Slice {
return nest.renderSlice(toRender) return nest.renderSlice(toRender)
} }
@@ -289,14 +294,42 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
case bool: case bool:
return fmt.Sprintf("%t", v), nil return fmt.Sprintf("%t", v), nil
case *string:
if v == nil {
return "", nil
}
return nest.Render(*v)
case string: case string:
if nest.option.NoEscapeInput { if nest.option.NoEscapeInput {
return v, nil return v, nil
} }
return html.EscapeString(v), nil return html.EscapeString(v), nil
case float64, int, int64: case float32:
return fmt.Sprintf("%v", v), nil return strconv.FormatFloat(float64(v), 'f', -1, 32), 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)
@@ -305,7 +338,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: %+v", v) return "", fmt.Errorf("unsupported template hash value type: %T: %+v", v, v)
} }
} }
+2 -2
View File
@@ -79,10 +79,10 @@ func TestRenderWithEscapedVariableAtStart01(t *testing.T) {
} }
page := templatenest.Hash{ page := templatenest.Hash{
"TEMPLATE": "03-var-at-begin-with-space", "TEMPLATE": "03-var-at-begin-with-escape",
} }
outputPath := "templates/output/10-var-at-begin-with-space.html" outputPath := "templates/output/10-var-at-begin-with-escape.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)