templatefs

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT, MIT Imports: 11 Imported by: 0

README

templatefs

Go lang template http.FileSystem

Installation

$ go get github.com/dolfly/httpfs/templatefs

Usage

import "github.com/dolfly/httpfs/templatefs"

Examples

TemplateFS
package main

import (
	"github.com/dolfly/httpfs/templatefs"
	"github.com/dolfly/httpfs/templatefs/engines/markdown"
	"net/http"
)

func main() {
	fs := templatefs.New(http.Dir("templates"), nil, &markdown.Engine{})

	http.Handle("/", http.FileServer(fs))
	http.ListenAndServe(":8080", nil)
}

Simple markdown webserver

TemplateFS_custom_layout
package main

import (
	"github.com/dolfly/httpfs/templatefs"
	"github.com/dolfly/httpfs/templatefs/engines/markdown"
	"net/http"
	"strings"
	"unicode"
)

func main() {
	s := `
<!DOCTYPE html>
<html>
<head>
	<title>{{ .Title }}</title>
	<meta charset="utf-8">
	{{- if .CSS}}
	<link rel="stylesheet" type="text/css" href="{{ .CSS }}">
	{{- end}}
</head>
<body>
{{ .Body | safehtml }}
</body>
</html>
`

	layout, err := templatefs.NewLayout(strings.TrimLeftFunc(s, unicode.IsSpace))
	if err != nil {
		return
	}

	fs := templatefs.New(http.Dir("templates"), layout, &markdown.Engine{})

	http.Handle("/", http.FileServer(fs))
	http.ListenAndServe(":8080", nil)
}

Simple markdown webserver with custom layout

Author

gnue

License

MIT

Documentation

Overview

Go lang template http.FileSystem

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLayout

func NewLayout(s string) (*template.Template, error)

Types

type Engine

type Engine interface {
	Render(input []byte) []byte
	PageInfo(input []byte) *Page
	Exts() []string
}

type File

type File struct {
	// contains filtered or unexported fields
}

func (*File) Close

func (f *File) Close() error

func (*File) Read

func (f *File) Read(p []byte) (int, error)

func (*File) Readdir

func (f *File) Readdir(count int) ([]os.FileInfo, error)

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

type FileInfo

type FileInfo struct {
	// contains filtered or unexported fields
}

func (*FileInfo) IsDir

func (f *FileInfo) IsDir() bool

func (*FileInfo) ModTime

func (f *FileInfo) ModTime() time.Time

func (*FileInfo) Mode

func (f *FileInfo) Mode() os.FileMode

func (*FileInfo) Name

func (f *FileInfo) Name() string

func (*FileInfo) Size

func (f *FileInfo) Size() int64

func (*FileInfo) Sys

func (f *FileInfo) Sys() interface{}

type FileSystem

type FileSystem struct {
	Engines      map[string]Engine
	PageTemplete *template.Template
	FileSystem   http.FileSystem
	// contains filtered or unexported fields
}

func New

func New(fs http.FileSystem, layout *template.Template, engines ...Engine) *FileSystem

func (*FileSystem) FindEngine

func (t *FileSystem) FindEngine(name string) Engine

func (*FileSystem) Open

func (t *FileSystem) Open(name string) (http.File, error)

func (*FileSystem) RegEngine

func (t *FileSystem) RegEngine(e Engine, exts ...string)

type Page

type Page struct {
	Title      string
	CSS        string
	Layout     string
	Permalink  string
	Published  string
	Categories []string
	Tags       []string
}

Directories

Path Synopsis
engines

Jump to

Keyboard shortcuts

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