Git Product home page Git Product logo

renders's People

Contributors

chilts avatar cnphpbb avatar unknwon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

renders's Issues

NewTemplateFileSystem for fileb0x (similar to vfsgen, assetfs, etc)

I needed to use macaron.Render with fileb0x instead of bindata. I think that this code will be helpful for anyone who might need to use vfsgen, packr, statik, rice and other bindata-style systems.

package router

import (
	"bytes"
	"fmt"
	"io"
	"path/filepath"

	"code.example.com/me/go-static"

	"gopkg.in/macaron.v1"
)

// Replace the one from macaron.Render since the files field is private
// TplFileSystem implements TemplateFileSystem interface.
type TplFileSystem struct {
	files []macaron.TemplateFile
}

// NewTemplateFileSystem creates new template file system with given options.
func NewTemplateFileSystem(lastDir string, exts ...string) macaron.TemplateFileSystem {
	fs := TplFileSystem{}
	fs.files = make([]macaron.TemplateFile, 0, 10)
	if 0 == len(exts) {
		exts = []string{".tmpl", ".html"}
	}

	// This is the fileb0x WalkDirs
	paths, _ := static.WalkDirs(lastDir, false)
	for _, path := range paths {
		r, err := filepath.Rel(lastDir, path)
		ext := macaron.GetExt(r)

		for _, extension := range exts {
			if ext != extension {
				continue
			}

			var data []byte
			// Loop over candidates of directory, break out once found.
			// The file always exists because it's inside the walk function,
			// and read original file is the worst case.
			path = filepath.Join(lastDir, r)
			data, err = static.ReadFile(path)
			if err != nil {
				panic(err)
			}

			name := filepath.ToSlash((r[0 : len(r)-len(ext)]))
			fs.files = append(fs.files, macaron.NewTplFile(name, data, ext))
		}
	}

	return fs
}

func (fs TplFileSystem) ListFiles() []macaron.TemplateFile {
	return fs.files
}

func (fs TplFileSystem) Get(name string) (io.Reader, error) {
	for i := range fs.files {
		if fs.files[i].Name()+fs.files[i].Ext() == name {
			return bytes.NewReader(fs.files[i].Data()), nil
		}
	}
	return nil, fmt.Errorf("file '%s' not found", name)
}

This could probably be updated to use a generic interface that would work with many different FileSystem implementations and be created as a PR. However, for now I'll just leave this an example that others can easily follow.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.