Skip to content
Gasv0.10.0

gas/template

template/fs

import "github.com/gasmod/gas/template/fs"

Package fs reads templates from an fs.FS, such as an embed.FS, and cannot be written to.

Store implements gas.TemplateProvider with the contract of the template package. Register and RegisterFS return template.ErrReadOnly.

//go:embed templates
var embedded embed.FS
sub, err := fs.Sub(embedded, "templates")
if err != nil {
return err
}
store, err := templatefs.NewStore(sub)()

After this Get(ctx, “pages/home.html”) returns the content of templates/pages/home.html. In an app, register the constructor:

gas.WithSingletonService[gas.TemplateProvider](templatefs.NewStore(sub))

Get looks a name up in these steps:

  1. It stats the name, so a name that is not there costs no directory read.
  2. It reads the name element by element. Each directory on the way is read, a chunk of entries at a time, until an entry whose name is the element byte for byte. So Home.html is not found for a file home.html, even on a file system that ignores case, such as macOS’s.
  3. Every element but the last must be a directory, and the last a regular file.

The fs.FS must therefore support reading directories, as fs.WalkDir needs too.

The errors of a lookup:

  • A directory that cannot be read, or a file that is there but cannot be read, is an error wrapping template.ErrRead.
  • Nothing there is template.ErrTemplateNotFound.
  • A file over the store’s maximum size, WithMaxSize, is an error wrapping template.ErrTooLarge, and is not read past it.

List walks the fs.FS with fs.WalkDir and returns the names Get would find, sorted in byte order. It checks the context before each entry. A failure to walk is an error wrapping template.ErrList that names the path where the walk failed, inside the fs.FS.

The store never follows a symlink it can tell is one. An entry that fs.ReadDir reports with fs.ModeSymlink is neither listed nor read, nor walked through on the way to a name. os.DirFS and os.Root’s FS report symlinks so. An fs.FS whose directory entries do not report them gets whatever its Open does.

os.DirFS follows symlinks out of its directory when a file is opened: with evil.html a symlink to /etc/passwd.html, fs.ReadFile on it returns that file. The store refuses evil.html because the directory entry is a symlink, but a symlink that replaces a file between that check and the read is still followed.

For a directory on disk, use gas/template/dir, which reads through an os.Root, opens every file without blocking and checks what it opened. Or give this store os.Root’s FS, which refuses every path out of the root:

root, err := os.OpenRoot("templates")
store, err := templatefs.NewStore(root.FS())()

An fs.FS has no way to open a file without blocking. So over a directory on disk, a FIFO put in place of a template between Get’s lookup and its open makes Get wait for a writer. Keep this store to file systems that do not change under it, such as an embed.FS, or to a directory no one the app does not trust can write to.

A Store holds no state of its own beyond its fs.FS and extensions, and is safe for concurrent use when the fs.FS is, as embed.FS, os.DirFS and os.Root’s FS are.

The constructor returns template.ErrNilFS for a nil fs.FS, and an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.

The methods return errors wrapping template.ErrInvalidName, template.ErrTemplateNotFound, template.ErrRead, template.ErrTooLarge, template.ErrList, the context’s error and template.ErrReadOnly, with the texts the template package lists.

A path in an error is the path inside the fs.FS. The error of an *fs.PathError is kept without its path, which some file systems give in full.

func NewStore(fsys iofs.FS, opts ...Option) func() (*Store, error)

NewStore returns the constructor of a Store over fsys with opts applied, in the shape the gas container expects. The constructor returns template.ErrNilFS for a nil fsys and an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.

type Option func(*Store)

Option configures a Store.

func WithExtensions(exts ...string) Option

WithExtensions sets the extensions a template name must end in. The constructor checks them, as described in the template package documentation.

Default: template.DefaultExtension.

func WithMaxSize(n int64) Option

WithMaxSize sets the most bytes a template may hold. The constructor checks it with template.CheckMaxSize.

Default: template.DefaultMaxSize.

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

Store reads templates from an fs.FS and cannot be written to, as described in the package documentation. It is safe for concurrent use when its fs.FS is.

func (s *Store) Get(_ context.Context, name string) ([]byte, error)

Get returns the content of the template name, read from the file system, as described in the package documentation. Its errors wrap template.ErrInvalidName, template.ErrTemplateNotFound, template.ErrRead or template.ErrTooLarge.

func (s *Store) List(ctx context.Context) ([]string, error)

List returns the names of the templates of the file system in byte order, as described in the package documentation. A failure to walk it is an error wrapping template.ErrList.

func (s *Store) Register(_ context.Context, name string, _ []byte) error

Register returns an error wrapping template.ErrInvalidName for a name the store does not accept, and template.ErrReadOnly for any other.

func (*Store) RegisterFS(context.Context, iofs.FS) error

RegisterFS returns template.ErrReadOnly.