Documentation
¶
Overview ¶
*
- tmplfn are a collection of functions useful to add to the default Go template/text and template/html template definitions *
- @author R. S. Doiel *
- Copyright (c) 2017, Caltech
- All rights not granted herein are expressly reserved by Caltech. *
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. *
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. *
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. *
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
Index ¶
- Variables
- func Assemble(tmplFuncs template.FuncMap, templateFilenames ...string) (*template.Template, error)
- func AssembleString(tmplFuncs template.FuncMap, src string) (*template.Template, error)
- func AssembleTemplateMap(tmplFuncs template.FuncMap, templateSrcMap map[string]string) (map[string]*template.Template, error)
- func Join(maps ...template.FuncMap) template.FuncMap
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // Version of tmplfn package Version = `v0.0.9` // TimeMap provides a common set of time/date related functions for use in text/template or html/template TimeMap = template.FuncMap{ "year": func(s string) string { var ( dt time.Time err error ) if s == "now" { dt = time.Now() } else { dt, err = time.Parse("2006-01-02", normalizeDate(s)) if err != nil { return "" } } return dt.Format("2006") }, "rfc3339": func(s string) string { var ( dt time.Time err error ) if s == "now" { dt = time.Now() } else { dt, err = time.Parse("2006-01-02", normalizeDate(s)) if err != nil { return "" } } return dt.Format(time.RFC3339) }, "rfc1123": func(s string) string { var ( dt time.Time err error ) if s == "now" { dt = time.Now() } else { dt, err = time.Parse("2006-01-02", normalizeDate(s)) if err != nil { return "" } } return dt.Format(time.RFC1123) }, "rfc1123z": func(s string) string { var ( dt time.Time err error ) if s == "now" { dt = time.Now() } else { dt, err = time.Parse("2006-01-02", normalizeDate(s)) if err != nil { return "" } } return dt.Format(time.RFC1123Z) }, "rfc822z": func(s string) string { var ( dt time.Time err error ) if s == "now" { dt = time.Now() } else { dt, err = time.Parse("2006-01-02", normalizeDate(s)) if err != nil { return "" } } return dt.Format(time.RFC822Z) }, "rfc822": func(s string) string { var ( dt time.Time err error ) if s == "now" { dt = time.Now() } else { dt, err = time.Parse("2006-01-02", normalizeDate(s)) if err != nil { return "" } } return dt.Format(time.RFC822) }, "datefmt": func(dt, outputFmtYMD, outputFmtYM, outputFmtY string) string { var ( inputFmt string outputFmt string ) switch { case len(dt) == 4: inputFmt = "2006" outputFmt = outputFmtY case len(dt) > 4 && len(dt) <= 7: inputFmt = "2006-01" outputFmt = outputFmtYM default: inputFmt = "2006-01-02" outputFmt = outputFmtYMD } d, err := time.Parse(inputFmt, dt) if err != nil { return fmt.Sprintf("%s, %s", dt, err.Error()) } return d.Format(outputFmt) }, } PageMap = template.FuncMap{ "nl2p": func(s string) string { return strings.Replace(strings.Replace(s, "\n\n", "<p>", -1), "\n", "<br />", -1) }, "contains": func(s, substring string) bool { return strings.Contains(s, substring) }, "title": func(s string) string { return strings.Title(s) }, "add": func(a, b int) int { return a + b }, "sub": func(a, b int) int { return a - b }, "arraylength": func(a []string) int { return len(a) }, "mapsize": func(m map[string]string) int { return len(m) }, "prevPage": func(from, size, max int) int { next := from - size if next < 0 { return 0 } return next }, "nextPage": func(from, size, max int) int { next := from + size if next > max { return from } return next }, "getType": func(t interface{}) string { switch tp := t.(type) { default: return fmt.Sprintf("%T", tp) } }, "join": func(li []interface{}, sep string) string { var l []string for _, item := range li { l = append(l, fmt.Sprintf("%s", item)) } return strings.Join(l, sep) }, "synopsis": func(s string) string { return doc.Synopsis(s) }, "encodeURIComponent": func(s string) string { u, err := url.Parse(s) if err != nil { log.Printf("Bad encoding request: %s, %s\n", s, err) return "" } return strings.Replace(u.String(), "&", "%26", -1) }, "stringify": func(data interface{}, prettyPrint bool) string { if prettyPrint == true { if buf, err := json.MarshalIndent(data, "", "\t"); err == nil { return string(buf) } } else if buf, err := json.Marshal(data); err == nil { return string(buf) } return "" }, "codeblock": func(src string, start int, end int, hint string) string { result := []string{} lines := strings.Split(src, "\n") if start < 1 { start = 0 } if end < 1 { end = len(lines) } if (end - start) > 0 { result = append(result, fmt.Sprintf("```%s", hint)) } for i, line := range lines[start:end] { if len(line) > 0 { result = append(result, fmt.Sprintf(" %s", line)) } else if i > 0 && i < (end-1) { result = append(result, "") } } if len(result) > 0 { result = append(result, "```") } return strings.Join(result, "\n") }, } //IterationMaps produce lists that then can be applied to range function IterationMap = template.FuncMap{ "forInt": func(start, end, inc int) []int { var result []int if start == end { return []int{start} } else if start < end { for i := start; i <= end; i = i + inc { result = append(result, i) } } else { for i := end; i >= start; i = i - inc { result = append(result, i) } } return result }, "forInt64": func(start, end, inc int64) []int64 { var result []int64 if start == end { return []int64{start} } else if start < end { for i := start; i <= end; i = i + inc { result = append(result, i) } } else { for i := end; i >= start; i = i - inc { result = append(result, i) } } return result }, } //ConversionMap holds functions related for converting types and presentations ConversionMap = template.FuncMap{ "atoi": func(s string, defaultInt int) int { i, err := strconv.Atoi(s) if err != nil { return defaultInt } return i }, "jsonInt": func(n json.Number, defaultInt int) int { i, err := n.Int64() if err != nil { return defaultInt } return int(i) }, "jsonInt64": func(n json.Number, defaultInt64 int64) int64 { i, err := n.Int64() if err != nil { return defaultInt64 } return i }, } //Dotpath methods from datatools/dotpath in templates DotpathMap = template.FuncMap{ "dotpath": func(p string, data interface{}, defaultVal interface{}) interface{} { if val, err := dotpath.Eval(p, data); err == nil { return val } return defaultVal }, "ifDotpathExists": func(p string, data interface{}, existsVal interface{}, notExistsVal interface{}) interface{} { if _, err := dotpath.Eval(p, data); err == nil { return existsVal } return notExistsVal }, } )
Functions ¶
func Assemble ¶
Assemble support a very simple template setup of an outer HTML file with a content include along with common template functions.
func AssembleString ¶
AssembleString like Assemble but using a string as a source for the template
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.