Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

clevergo/views

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Views Build Status Coverage Status Go Report Card GoDoc Release

Views is a templates(html/template) manager, it provides the following features:

  • File System: it use http.FileSystem to parse template files, allows to embed view files into go binary easilly by third-party tools, such as packr, statik etc. See example.
  • Simple: it bases on html/template, nothing more.
  • Cache: allow to cache parsed templates(default to enabled), see benchmark.
  • Global settings: it provides some useful setting for all templates, such as suffix, delimiters, funcMap etc.

Usage

$ go get github.com/clevergo/views/v2

Please take a look of the example.

Structure

Assume directory structure looks like:

views/
	layouts/                  contains layout files.
    	main.tmpl
    	page.tmpl
    	...
    	partials/             contains partial files.
        	head.tmpl
        	header.tmpl
        	footer.tmpl
        	...
	site/                     contains site's views.
	    home.tmpl
	    ...
	user/                     contains user's views.
    	login.tmpl
    	setting.tmpl
    	signup.tmpl
    	...
	...

Initialize

fs = http.Dir("./views") // file system, you can use packr, statik or other file system instead.
// options
opts := []views.Option{
	// views.Suffix(".tmpl"), // template suffix, default to .tmpl.
	// views.Delims("{{", "}}"), // template delimiters, default to "{{" and "}}".
	views.DefaultLayout("main"),
	views.LayoutsDir("layouts"),   // layout directory, relatived to views path.
	views.PartialsDir("partials"), // partials layout, relatived to layouts directory.
	// global function map for all templates.
	views.FuncMap(template.FuncMap{
		"title": strings.Title,
	}),
	views.Cache(false), // disabled caching for developing.
}
manager = views.New(viewsPath, opts...)
// add main layout.
manager.AddLayout("main", "head", "header", "footer")
// add a new layout.
manager.AddLayout("page", "head")
// add function to global funcMap
manager.AddFunc("foo", func() string {
    return "bar"
})

Render

// render with default layout
manager.Render(w, "site/index", nil)

// render with particular layout: page.
manager.RenderLayout(w, "page", "user/login", nil)

// render with data
manager.Render(w, "site/index", map[string]interface{}{
	"foo": "bar",
})

// render without layout.
manager.RenderPartial(w, "site/partial", nil)

Benchmark

$ go test -bench=.
BenchmarkManagerRender-12                           6873            237087 ns/op
BenchmarkManagerRenderPartial-12                   29976             37867 ns/op
BenchmarkManagerRenderCache-12                    236474              4909 ns/op
BenchmarkManagerRenderPartialCache-12             359468              3175 ns/op

The benchmark is base on the example that mentioned above, the result is depended on how complicated the template is.