-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for default (and user defined) functions
- Loading branch information
Showing
5 changed files
with
165 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package got | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// locate templates in possibly nested subfolders | ||
func findTemplatesRecursively(path string, extension string) (paths []string, err error) { | ||
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error { | ||
if err == nil { | ||
if strings.Contains(path, extension) { | ||
paths = append(paths, path) | ||
} | ||
} | ||
return err | ||
}) | ||
return | ||
} | ||
|
||
// Handles reading templates files in the given directory + ending path | ||
func loadTemplateFiles(dir, path, extension string) (templates map[string][]byte, err error) { | ||
var files []string | ||
files, err = findTemplatesRecursively(filepath.Join(dir, path), extension) | ||
if err != nil { | ||
return | ||
} | ||
|
||
templates = make(map[string][]byte) | ||
|
||
for _, path = range files { | ||
var b []byte | ||
b, err = ioutil.ReadFile(path) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Convert "templates/layouts/base.html" to "layouts/base" | ||
// For subfolders the extra folder name is included: | ||
// "templates/includes/sidebar/ad1.html" to "includes/sidebar/ad1" | ||
name := strings.TrimPrefix(filepath.Clean(path), filepath.Clean(dir)+"/") | ||
name = strings.TrimSuffix(name, filepath.Ext(name)) | ||
|
||
templates[name] = b | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package got | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"encoding/base32" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"html/template" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Functions I've found to be required in most every web-site template engine | ||
// Many borrowed from https://github.com/Masterminds/sprig | ||
|
||
// DefaultFunctions for templates | ||
var DefaultFunctions = template.FuncMap{ | ||
"title": strings.Title, | ||
"upper": strings.ToUpper, | ||
"lower": strings.ToLower, | ||
"trim": strings.TrimSpace, | ||
// Display singluar or plural based on count | ||
"plural": func(one, many string, count int) string { | ||
if count == 1 { | ||
return one | ||
} | ||
return many | ||
}, | ||
// Current Date (Local server time) | ||
"date": func() string { | ||
return time.Now().Format("2006-01-02") | ||
}, | ||
// Current Unix timestamp | ||
"unixtimestamp": func() string { | ||
return fmt.Sprintf("%d", time.Now().Unix()) | ||
}, | ||
// json encodes an item into a JSON string | ||
"json": func(v interface{}) string { | ||
output, _ := json.Marshal(v) | ||
return string(output) | ||
}, | ||
// Allow unsafe injection into HTML | ||
"noescape": func(a ...interface{}) template.HTML { | ||
return template.HTML(fmt.Sprint(a...)) | ||
}, | ||
// Allow unsafe URL injections into HTML | ||
"noescapeurl": func(u string) template.URL { | ||
return template.URL(u) | ||
}, | ||
// Modern Hash | ||
"sha256": func(input string) string { | ||
hash := sha256.Sum256([]byte(input)) | ||
return hex.EncodeToString(hash[:]) | ||
}, | ||
// Legacy | ||
"sha1": func(input string) string { | ||
hash := sha1.Sum([]byte(input)) | ||
return hex.EncodeToString(hash[:]) | ||
}, | ||
// Gravatar | ||
"md5": func(input string) string { | ||
hash := md5.Sum([]byte(input)) | ||
return hex.EncodeToString(hash[:]) | ||
}, | ||
// Popular encodings | ||
"base64encode": func(v string) string { | ||
return base64.StdEncoding.EncodeToString([]byte(v)) | ||
}, | ||
"base64decode": func(v string) string { | ||
data, err := base64.StdEncoding.DecodeString(v) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return string(data) | ||
}, | ||
"base32encode": func(v string) string { | ||
return base32.StdEncoding.EncodeToString([]byte(v)) | ||
}, | ||
"base32decode": func(v string) string { | ||
data, err := base32.StdEncoding.DecodeString(v) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return string(data) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters