1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00
Former-commit-id: e760aa67d8750bfdd15fc84e2dedeb591c204ba9
This commit is contained in:
hiveminded
2017-09-11 12:18:30 +03:00
parent f23e21f747
commit 89f79799d1
3 changed files with 202 additions and 47 deletions

View File

@@ -22,6 +22,26 @@ type (
Error pongo2.Error
// FilterFunction conversion for pongo2.FilterFunction
FilterFunction func(in *Value, param *Value) (out *Value, err *Error)
// Parser conversion for pongo2.Parser
Parser pongo2.Parser
// Token conversion for pongo2.Token
Token pongo2.Token
// INodeTag conversion for pongo2.InodeTag
INodeTag pongo2.INodeTag
// TagParser the function signature of the tag's parser you will have
// to implement in order to create a new tag.
//
// 'doc' is providing access to the whole document while 'arguments'
// is providing access to the user's arguments to the tag:
//
// {% your_tag_name some "arguments" 123 %}
//
// start_token will be the *Token with the tag's name in it (here: your_tag_name).
//
// Please see the Parser documentation on how to use the parser.
// See `RegisterTag` for more information about writing a tag as well.
TagParser func(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error)
)
type tDjangoAssetLoader struct {
@@ -119,19 +139,58 @@ func (s *DjangoEngine) AddFunc(funcName string, funcBody interface{}) {
s.rmu.Unlock()
}
// AddFilter adds a filter to the template.
// AddFilter registers a new filter. If there's already a filter with the same
// name, RegisterFilter will panic. You usually want to call this
// function in the filter's init() function:
// http://golang.org/doc/effective_go.html#init
//
// Same as `RegisterFilter`.
func (s *DjangoEngine) AddFilter(filterName string, filterBody FilterFunction) *DjangoEngine {
s.rmu.Lock()
s.filters[filterName] = filterBody
s.rmu.Unlock()
return s.registerFilter(filterName, filterBody)
}
// RegisterFilter registers a new filter. If there's already a filter with the same
// name, RegisterFilter will panic. You usually want to call this
// function in the filter's init() function:
// http://golang.org/doc/effective_go.html#init
//
// See http://www.florian-schlachter.de/post/pongo2/ for more about
// writing filters and tags.
func (s *DjangoEngine) RegisterFilter(filterName string, filterBody FilterFunction) *DjangoEngine {
return s.registerFilter(filterName, filterBody)
}
func (s *DjangoEngine) registerFilter(filterName string, filterBody FilterFunction) *DjangoEngine {
fn := pongo2.FilterFunction(func(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
theOut, theErr := filterBody((*Value)(in), (*Value)(param))
return (*pongo2.Value)(theOut), (*pongo2.Error)(theErr)
})
pongo2.RegisterFilter(filterName, fn)
return s
}
// RegisterTag registers a new tag. You usually want to call this
// function in the tag's init() function:
// http://golang.org/doc/effective_go.html#init
//
// See http://www.florian-schlachter.de/post/pongo2/ for more about
// writing filters and tags.
func (s *DjangoEngine) RegisterTag(tagName string, parserFn TagParser) error {
fn := func(doc *pongo2.Parser, start *pongo2.Token, arguments *pongo2.Parser) (pongo2.INodeTag, *pongo2.Error) {
t, err := parserFn((*Parser)(doc), (*Token)(start), (*Parser)(arguments))
return t, (*pongo2.Error)(err)
}
return pongo2.RegisterTag(tagName, fn)
}
// Load parses the templates to the engine.
// It's alos responsible to add the necessary global functions.
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *DjangoEngine) Load() error {
if s.assetFn != nil && s.namesFn != nil {
// embedded
return s.loadAssets()
@@ -147,22 +206,6 @@ func (s *DjangoEngine) Load() error {
return s.loadDirectory()
}
// this exists because of moving the pongo2 to the vendors without conflictitions if users
// wants to register pongo2 filters they can use this django.FilterFunc to do so.
func (s *DjangoEngine) convertFilters() map[string]pongo2.FilterFunction {
filters := make(map[string]pongo2.FilterFunction, len(s.filters))
for k, v := range s.filters {
func(filterName string, filterFunc FilterFunction) {
fn := pongo2.FilterFunction(func(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
theOut, theErr := filterFunc((*Value)(in), (*Value)(param))
return (*pongo2.Value)(theOut), (*pongo2.Error)(theErr)
})
filters[filterName] = fn
}(k, v)
}
return filters
}
// LoadDirectory loads the templates from directory.
func (s *DjangoEngine) loadDirectory() (templateErr error) {
dir, extension := s.directory, s.extension
@@ -175,12 +218,6 @@ func (s *DjangoEngine) loadDirectory() (templateErr error) {
set := pongo2.NewSet("", fsLoader)
set.Globals = getPongoContext(s.globals)
// set the filters
filters := s.convertFilters()
for filterName, filterFunc := range filters {
pongo2.RegisterFilter(filterName, filterFunc)
}
s.mu.Lock()
defer s.mu.Unlock()
@@ -234,12 +271,6 @@ func (s *DjangoEngine) loadAssets() error {
set := pongo2.NewSet("", &tDjangoAssetLoader{baseDir: s.directory, assetGet: s.assetFn})
set.Globals = getPongoContext(s.globals)
// set the filters
filters := s.convertFilters()
for filterName, filterFunc := range filters {
pongo2.RegisterFilter(filterName, filterFunc)
}
if len(virtualDirectory) > 0 {
if virtualDirectory[0] == '.' { // first check for .wrong
virtualDirectory = virtualDirectory[1:]