github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/helper/bindata/tpl_loader.go (about)

     1  package bindata
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // tplLoader is a pongo2.TemplateLoader that loads templates from assets.
    12  type tplLoader struct {
    13  	Data *Data
    14  	Base string
    15  }
    16  
    17  func (t *tplLoader) Abs(base, name string) string {
    18  	idx := strings.IndexAny(name, ":/")
    19  	if idx != -1 && name[idx] == ':' {
    20  		// We have a shared resource. Return the name as-is
    21  		return name
    22  	}
    23  
    24  	return filepath.Join(t.Base, name)
    25  }
    26  
    27  func (t *tplLoader) Get(path string) (io.Reader, error) {
    28  	data := t.Data
    29  
    30  	idx := strings.IndexAny(path, ":/")
    31  	if idx != -1 && path[idx] == ':' {
    32  		// We have a shared resource. Get the proper data loader.
    33  		share := path[:idx]
    34  		raw, ok := t.Data.SharedExtends[share]
    35  		if !ok {
    36  			return nil, fmt.Errorf("share not found: %s", share)
    37  		}
    38  
    39  		data = raw
    40  		path = path[idx+1:]
    41  	}
    42  
    43  	raw, err := data.Asset(path)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return bytes.NewReader(raw), nil
    49  }