webview

package module
v0.0.0-...-f4c7a8f Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2014 License: MIT Imports: 9 Imported by: 0

README

webview

Small library to create structured html templates using html/template package from go

Documentation

Overview

Small library to create structured html templates using html/template package from go

The library is inspired by how rails structure it's view templates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowHtmlJsAndCss

func AllowHtmlJsAndCss(f Namer) bool

Filter function that allow only html,(js/json) and css files

All directories are allowed too

func DiscoverDelim

func DiscoverDelim(name string) (string, string)

Discover the best delimiter for the given filename

HTML => {{ / }} JS => <% / %> CSS => <% / %>

func LoadDirInto

func LoadDirInto(t TreeSet, dir Dir, funcs tt.FuncMap, filter Filter) error

Load all files from the given Dir into the given template

func LoadFileInto

func LoadFileInto(t TreeSet, f File, funcs tt.FuncMap) error

Read a file and register a new template under the filename

The name is given by TemplateName(f)

func Template

func Template(set TreeSet, alias map[string]string) (*template.Template, error)

Return a new template containing all templates from set.

The alias map can be used to access one template with two or more names

Consider this:

layout/main.html
The contents come from {{ template "contents" }}

index/index.html
I have the contents

user/index.html
I also have the contents

If you loaded all those files, you have a treeSet with three templates: "layout/main.html", "index/index.html" and "user/index.html"

Now you can use the alias to map "index/index.html" to "contents"

alias := map[string]string {
	"contents": "index/index.html"
}

When you execute the template, instead of having a "template contents not found" the system will execute the "index/index.html" template and put its result on "layout/main.html"

This keeps all the safety from html/template but enable your to use more dynamic templates without having to parse them every single time.

If you need a new template with a different alias, just call this function again passing a different alias map

func TemplateName

func TemplateName(f Namer) string

Return the unique name of the object.

If the object is also a Parenter, the name will contain the name of it's parents

Types

type Dir

type Dir interface {
	Namer
	Parenter
	// Return the files that hold any content that can be parsed from
	// the template engine
	ReadFiles() ([]File, error)
	// Return the list of sub-directories
	ReadDirs() ([]Dir, error)
}

A virtual directory

func DiskVFS

func DiskVFS(root string) (Dir, error)

create a new VFS at the given root

type File

type File interface {
	Namer
	// Return the contents of this file
	Contents() ([]byte, error)
	Parenter
}

A virtual file

type Filter

type Filter interface {
	// return
	Filter(f Namer) bool
}

Use to select which files should be parsed as a template

type FilterFunc

type FilterFunc func(f Namer) bool

Implements the Filter interface

func (FilterFunc) Filter

func (ff FilterFunc) Filter(f Namer) bool

Call the function

type Namer

type Namer interface {
	// The name of the item, only text that is allowed inside a URL
	// can be returned.
	//
	// Unicode MUST BE encoded as UTF-8 instead of using %xx
	//
	// The name here should only return its name without any information
	// from its parents
	//
	// Directories MUST NOT include the "/"
	Name() string

	// Should return if the current item represents a directory
	IsDir() bool
}

A item that have a name

type Parenter

type Parenter interface {
	// Return the parent of this directory, might return nil,
	// in this case, this Dir is the root
	Parent() Dir
}

Represent any entry in the filesystem that can have a parent

type TreeSet

type TreeSet map[string]*parse.Tree

A set of compiled templates

func LoadDir

func LoadDir(root Dir, funcs tt.FuncMap, filter Filter) (TreeSet, error)

Load all files under root and return a set of all templates if two templates have the same name (let's say that file a.html and b.html both define the template "nice_button"). Only one of those definitions will be available (the last one returned by the vfs)

Each template can be accessed by its full path from root, that means "layout/body.html" represents a file under "layout" with a name of "body.html"

Directories

Path Synopsis
fileserver enable the user to modify the path of a file request before serving the actual file.
fileserver enable the user to modify the path of a file request before serving the actual file.
Helper methods to combine webview templates with http requests objects Uses the gorilla context library
Helper methods to combine webview templates with http requests objects Uses the gorilla context library
Enable easy and fast http prototype to go
Enable easy and fast http prototype to go
This package wrap the npm module regenerator from Facebook to allow more people to use ES6 without having to compile lot's of files every new deploy.
This package wrap the npm module regenerator from Facebook to allow more people to use ES6 without having to compile lot's of files every new deploy.

Jump to

Keyboard shortcuts

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