templates

package
v1.0.0-rc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2018 License: GPL-3.0 Imports: 5 Imported by: 9

Documentation

Overview

Package templates is used to easily retrieve parsed templates without hassle.

LoadedTemplate

To easily load cached templates and put them together the LoadedTemplate facility can be used.

To load templates just use the load function which returns a LoadedTemplate:

templates.Load("header", "content")

Here "header" and "content" are the names of the loaded templates. As you can see the file extension and the base path is omitted. The loader figures the paths out on his own.

Loaded templates are cached and won't be reread from the disk as long as they remain in the cache. That is until they are removed from it like that:

templates.Uncache("header")

Or by clearing the whole cache:

templates.ClearCache()

The the newly loaded templates can be further used. And modified by for example adding functions:

templates.Load("header", "content").Func(template.FuncMap{"test" : Test})

Here "test" is the name of the function with which it is referred to inside the template and Test is the function itself.

Functions used in a template need to be added before a template is loaded. If you don't have loaded templates before hand you can just use:

template.Load().Func(template.FuncMap{"test" : Test}).Append("usesTest")

An empty load clause given you a LoadedTemplate so you can add functions before actually loading a template.

Here you can already see the Append function of a LoadedTemplate which can be used to append new templates to the bunch later down the road.

Finally if you want to execute a specific template you can use Lookup find it. Lets say the content template contains a definition of the "tab" template and we want to get it:

template.Load("header", "content").Lookup("tab")

But be careful other than the other methods the Lookup function doesn't return the LoadedTemplate it is used on. It actually returns only the "tab" template which doesn't include the templates in "header" or "content", but only the "tab" template and every template defined inside it.

After you have found the template you want to execute you can use the Get function to retrieve the actual template from the standard library and execute it:

template.Load("header", "content").Lookup("tab").Get().Execute(w, c)

This will load the "header" and "content" template files and retrieve the "tab" template out of them and execute it on the writer w with the context c.

Index

Constants

View Source
const (
	HeaderDef  = "header"
	TabContent = "tab-content"
	Content    = "content"

	HeaderPrint = "header-print"

	Error = "error"
)

The names of the templates

View Source
const (
	// Static
	Header = "static/header"
	Navbar = "static/navbar"
	Footer = "static/footer"
	About  = "static/about"

	ContentSidebar   = "static/content-sidebar"
	ContentNoSidebar = "static/content-no-sidebar"

	// Explore
	MenuExplore     = "explore/menu-explore"
	ExploreUsers    = "explore/list-users"
	ExploreProjects = "explore/list-projects"
	ExploreGroups   = "explore/list-groups"

	// Modals
	SignIn            = "modal/sign-in"
	TestStepEdit      = "modal/teststep-edit"
	ManageVersions    = "modal/manage-versions"
	TestCaseSelection = "modal/testcase-selection"
	ErrorModal        = "modal/error"
	ManageLabels      = "modal/manage-labels"
	WrongFile         = "modal/wrong-file"

	// User
	NewAccount = "creation/new-account"

	// Project
	ContentProjectTabs = "tab-project/content-project-tabs"
	NewProject         = "creation/new-project"
	Settings           = "tab-project/settings/settings"

	// Execution
	ExecutionStartPage = "tab-project/execution/startpage"
	ExecutionStep      = "tab-project/execution/step"
	ExecutionSummary   = "tab-project/execution/summary"

	// Protocols
	ProtocolsList         = "tab-project/protocols/list"
	TestCaseProtocols     = "tab-project/protocols/testcase"
	TestSequenceProtocols = "tab-project/protocols/testsequence"
	PrintProtocols        = "tab-project/protocols/list"
	PrintCaseProtocol     = "tab-project/protocols/testcase-print"
	PrintSequenceProtocol = "tab-project/protocols/testsequence-print"

	// TestCases
	NewTestCase     = "tab-project/testcases/new"
	TestCasesList   = "tab-project/testcases/list"
	ShowTestCase    = "tab-project/testcases/show"
	EditTestCase    = "tab-project/testcases/edit"
	TestCaseHistory = "tab-project/testcases/history"
	PrintTestCases  = "tab-project/testcases/list-print"
	PrintCase       = "tab-project/testcases/show-print"

	// TestSequences
	NewTestSequence     = "tab-project/testsequences/new"
	TestSequencesList   = "tab-project/testsequences/list"
	ShowTestSequence    = "tab-project/testsequences/show"
	EditTestSequence    = "tab-project/testsequences/edit"
	TestSequenceHistory = "tab-project/testsequences/history"
	PrintSequences      = "tab-project/testsequences/list-print"
	PrintSequence       = "tab-project/testsequences/show-print"

	// Group
	NewGroup = "creation/new-group"

	// Comments
	Comments = "tab-project/comments"

	// Members
	Members      = "tab-project/members/members"
	AssignMember = "modal/add-member"
	RemoveMember = "modal/remove-member"
)

This const contains the paths of the template files

Variables

This section is empty.

Functions

func Append

func Append(base *template.Template, names ...string) *template.Template

Append parses the templates with the given names together onto the given base template. Every loaded template is cached together with the base so be careful to pass only a minimal base that contains everything needed to load the templates with given names (normally functions).

The given base should've never been executed yet. If it has been this method will panic.

If the base is nil it is omitted. If no names are passed for the templates either the base or if its nil a new template with an empty string as name will be returned.

The name only consists of the filename without extension. So for example instead of 'templates/header.tmpl' only 'header' needs to be passed. If the template file is nested into a directory you can simply type: 'my/special/dir/header'

func ClearCache

func ClearCache()

ClearCache clears the cache of templates forcing a reload of every previously cached template.

func Get

func Get(names ...string) *template.Template

Get parses the templates with the given names together as a tree. If no names are passed the loaded template tree will only contain a template with an empty string as name.

The name only consists of the filename without extension. So for example instead of 'templates/header.tmpl' only 'header' needs to be passed. If the template file is nested into a directory you can simply type: 'my/special/dir/header'

func Uncache

func Uncache(names ...string)

Uncache removes the templates with given name form the cache forcing a reload of these templates when they are loaded the next time.

Types

type LoadedTemplate

type LoadedTemplate interface {
	// Append appends the templates with given names to the tree of this template.
	//
	// If this call doesn't get any arguments a new template with an empty
	// string as name is added to the tree.
	//
	// This method does directly modify the template it is called on and
	// returns the said template for chaining.
	Append(names ...string) LoadedTemplate

	// Funcs adds functions to the templates tree that can be used by the
	// templates accordingly.
	//
	// This method does directly modify the template it is called on and
	// returns the said template for chaining.
	Funcs(funcMap template.FuncMap) LoadedTemplate

	// Lookup searches the template tree for a template with given name.
	//
	// This call doesn't affect the template its called on and returns a new
	// template representing the found template.
	// If no template with given name is found nil is returned.
	Lookup(name string) LoadedTemplate

	// Clone clones a loaded templates so it can be reused.
	//
	// Changes to the returned template don't affect this template.
	Clone() LoadedTemplate

	// Get gets the loaded standard library template.
	Get() *template.Template
}

LoadedTemplate is a convenience type used to improve the usage of templates in this project.

func Load

func Load(names ...string) LoadedTemplate

Load loads a template tree containing templates with the given names.

If a template can't be found this method panics. If no names are passed the loaded template tree will only contain a template with an empty string as name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL