go-macaron / renders Goto Github PK
View Code? Open in Web Editor NEWPackage renders is a middleware, that provides template render for Macaron.
License: Apache License 2.0
Package renders is a middleware, that provides template render for Macaron.
License: Apache License 2.0
Hi I just want to run macaron with json content.
so how to disable scheme load function ?
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.
I can not use the for function inside the template.
Would that be a bug? Or has it not been implemented yet?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.