lookupfs

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lookupfs implements a filesystem backend for tpl2x. It can use native filesystem (by default) or embedded filesystem (which can be set via FileSystem func).

Example (LookupAllByPrefix)

Lookup filesystem for templates separated by directory

cfg := Config{
	Includes:  "includes",
	Layouts:   "layouts",
	Pages:     "pages",
	Ext:       ".html",
	DefLayout: "lay",
}
// Here we create a temporary directory and populate it with our sample
// template definition files; usually the template files would already
// exist in some location known to the program.
dir := createTestDir(cfg.Ext, []templateFile{
	{[]string{"includes"}, "inc", `inc1 here`},
	{[]string{"includes", "subdir1"}, "inc", `inc2 here`},
	{[]string{"layouts"}, "lay", `lay1 here`},
	{[]string{"layouts", "subdir2"}, "lay", `lay2 here`},
	{[]string{"pages"}, "page", `page1 here`},
	{[]string{"pages", "subdir3"}, "page", `page2 here`},
})
// Clean up after the test; another quirk of running as an example.
defer os.RemoveAll(dir)

cfg.Root = dir
fs := New(cfg)
err := fs.LookupAll()
if err != nil {
	log.Fatal(err)
}
fmt.Printf("includes: %v\n", fs.IncludeNames())
fmt.Printf("layouts: %v\n", fs.LayoutNames())
fmt.Printf("pages: %v\n", fs.PageNames(false))
Output:

includes: [inc subdir1/inc]
layouts: [lay subdir2/lay]
pages: [page subdir3/page]
Example (LookupAllBySuffix)

Lookup filesystem for templates separated by filename suffix

cfg := Config{
	Includes:  ".includes",
	Layouts:   ".layouts",
	Pages:     "not_used",
	Ext:       ".html",
	DefLayout: "lay",
	UseSuffix: true,
}
// Here we create a temporary directory and populate it with our sample
// template definition files; usually the template files would already
// exist in some location known to the program.
dir := createTestDir(cfg.Ext, []templateFile{
	{[]string{}, "inc.includes", `inc1 here`},
	{[]string{"subdir1"}, "inc.includes", `inc2 here`},
	{[]string{}, "lay.layouts", `lay1 here`},
	{[]string{"subdir2"}, "lay.layouts", `lay2 here`},
	{[]string{}, "page", `page1 here`},
	{[]string{"subdir3"}, "page", `page2 here`},
})
// Clean up after the test; another quirk of running as an example.
defer os.RemoveAll(dir)

cfg.Root = dir
fs := New(cfg)
err := fs.LookupAll()
if err != nil {
	log.Fatal(err)
}
fmt.Printf("includes: %v\n", fs.IncludeNames())
fmt.Printf("layouts: %v\n", fs.LayoutNames())
fmt.Printf("pages: %v\n", fs.PageNames(false))
Output:

includes: [inc subdir1/inc]
layouts: [lay subdir2/lay]
pages: [page subdir3/page]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Root       string `long:"templates" default:"tmpl/" description:"Templates root path"`
	Ext        string `long:"mask" default:".tmpl" description:"Templates filename mask"`
	Includes   string `long:"includes" default:"inc/" description:"Includes path"`
	Layouts    string `long:"layouts" default:"layout/" description:"Layouts path"`
	Pages      string `long:"pages" default:"page/" description:"Pages path"`
	UseSuffix  bool   `long:"use_suffix" description:"Template type defined by suffix"`
	Index      string `long:"index" default:"index" description:"Index page name"`
	DefLayout  string `long:"def_layout" default:"default" description:"Default layout template"`
	HidePrefix string `long:"hide_prefix" default:"." description:"Treat files with this prefix as hidden"`
}

Config holds config variables and its defaults

type File

type File struct {
	Path    string
	ModTime time.Time
}

File holds file metadata

type FileSystem

type FileSystem interface {
	Walk(root string, walkFn filepath.WalkFunc) error
	Open(name string) (http.File, error)
}

FileSystem holds all of used filesystem access methods

type LookupFileSystem

type LookupFileSystem struct {
	Includes map[string]File
	Layouts  map[string]File
	Pages    map[string]File
	// contains filtered or unexported fields
}

LookupFileSystem holds filesystem with template lookup functionality

func New

func New(cfg Config) *LookupFileSystem

New creates LookupFileSystem

func (LookupFileSystem) DefaultLayout

func (lfs LookupFileSystem) DefaultLayout() string

DefaultLayout returns default layout name This name has been checked for availability in LookupAll()

func (*LookupFileSystem) FileSystem

func (lfs *LookupFileSystem) FileSystem(fs FileSystem) *LookupFileSystem

FileSystem changes filesystem access object

func (LookupFileSystem) IncludeNames

func (lfs LookupFileSystem) IncludeNames() []string

IncludeNames return sorted slice of include names

func (LookupFileSystem) LayoutNames

func (lfs LookupFileSystem) LayoutNames() []string

LayoutNames return sorted slice of layout names

func (*LookupFileSystem) LookupAll

func (lfs *LookupFileSystem) LookupAll() (err error)

LookupAll scan filesystem for includes,pages and layouts

func (LookupFileSystem) PageNames

func (lfs LookupFileSystem) PageNames(hide bool) []string

PageNames return sorted slice of page names

func (LookupFileSystem) ReadFile

func (lfs LookupFileSystem) ReadFile(name string) (string, error)

ReadFile reads file via filesystem method

Jump to

Keyboard shortcuts

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