Skip to content

GoLangsam/template

Repository files navigation

template

Use both "text/template" & "html/template" together under a common umbrella.

Software License Go Report Card GoDoc Build Status

background

Shall be written another day :-)

package docu

Package template implements the interface common to the standard packages "text/template" and "html/template" as type Template.

Thus, it exploits the fact, that

  package `html/template` provides the same interface as package `text/template`
  and should be used instead of `text/template` whenever the output is HTML
  as it automatically secures HTML output against certain attacks.

as said in go doc text/template and go doc html/template.

All methods and all package level functions are forewarded.

Instead of a single New(name) this package unsurprisingly provides two constructors:

  • template.Text(name) &

  • template.HTML(name).

Note: The package level functions ParseFiles & ParseGlob became

  • ParseTextFiles / ParseHTMLfile resp.

  • ParseTextGlob / ParseHTMLglob.

Thus, the exported type Template represents the template used, be it html or text.

Also the type FuncMap is forwarded.

Note: Clients in need to access any other type

  • such as ExecError (from "text/template") or

  • data types such as HTML, CSS, JS and friends

  • as well as Error and ErrorCode (from "html/template")

are requested to use the respective standard package directly for access to the error and data types.

For example escape_test.go uses

  import( data "html/template" )

and refers to

  data.HTML, data.CSS, data.JS ...

later.

Implementation notes

  • doc.go just documents the package (as quoted above) for go doc github.com/GoLangsam/template
  • template.go defines the interface type Template (and the convenient wrapper Must and the ubiquous type FuncMap)
  • forward.go forwards package level functions. It'is simply taken from "html/template" as noted inside.
  • wrapTemplateText.go and wrapTemplateHTML.go define respective (private) implementation types, foreward the methods (if need) and the Parse... functions. Intentionally they are as similar possible.

Examples and test

All *_test.go files from both standard packages are used! (Except as noted below.)

Adjusted are just things such as:

  • package declarations
    • template => template_test
    • in order to avoid spurious bug: duplicate flag definition debug
  • imports
    • "text/template" resp. "html/template" => "github.com/GoLangsam/template"
  • references to type Template
    • *Template => Template
  • constructors
    • New => Text resp. HTML
  • the global Parse-functions
    • template.Parse... => template.ParseText... resp. template.ParseHTML...
  • data types from "html/template"
    • imported as data, and types used accordingly, e.g.
    • template.HTML => data.HTML
  • few portions are deactivated / commented, as they require internals of the underlying package
    • css_test.go, html_test.go, js_test.go url_test.go and from "html/template" are entirely omitted for same reason
  • misspellings: Cincinatti => Cincinnati

As of now, no additional tests are provided. The author could not think of anything reasonable yet.

Your suggestions, remarks, questions and/or contributions are welcome ;-)


Think deep - code happy - be simple - see clear :-)


Support on Beerpay

Hey dude! Help me out for a couple of 🍻!

Beerpay Beerpay

About

Use "text/template" & "html/template" together under a common umbrella

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages