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 ¶
- type Config
- type File
- type FileSystem
- type LookupFileSystem
- func (lfs LookupFileSystem) DefaultLayout() string
- func (lfs *LookupFileSystem) FileSystem(fs FileSystem) *LookupFileSystem
- func (lfs LookupFileSystem) IncludeNames() []string
- func (lfs LookupFileSystem) LayoutNames() []string
- func (lfs *LookupFileSystem) LookupAll() (err error)
- func (lfs LookupFileSystem) PageNames(hide bool) []string
- func (lfs LookupFileSystem) ReadFile(name string) (string, error)
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 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 (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