Пример #1
0
// DefaultLoader sets up and assigns the default loader to be used by Xslate.
func DefaultLoader(tx *Xslate, args Args) error {
	var tmp interface{}

	tmp, ok := args.Get("CacheDir")
	if !ok {
		tmp, _ = ioutil.TempDir("", "go-xslate-cache-")

	}
	cacheDir := tmp.(string)

	tmp, ok = args.Get("LoadPaths")
	if !ok {
		cwd, _ := os.Getwd()
		tmp = []string{cwd}
	}
	paths := tmp.([]string)

	cache, err := loader.NewFileCache(cacheDir)
	if err != nil {
		return err
	}
	fetcher, err := loader.NewFileTemplateFetcher(paths)
	if err != nil {
		return err
	}

	tmp, ok = args.Get("CacheLevel")
	if !ok {
		tmp = 1
	}
	cacheLevel := tmp.(int)
	tx.Loader = loader.NewCachedByteCodeLoader(cache, loader.CacheStrategy(cacheLevel), fetcher, tx.Parser, tx.Compiler)
	return nil
}
Пример #2
0
func ShioriConfigureLoader(tx *xslate.Xslate, args xslate.Args) error {
	var asset Asset
	if val, ok := args.Get(`Asset`); !ok {
		return ErrAssetIsNotFound
	} else {
		switch val.(type) {
		case func(string) ([]byte, error):
			asset = AssetFunc(val.(func(string) ([]byte, error)))
		case AssetFunc:
			asset = val.(AssetFunc)
		case Asset:
			asset = val.(Asset)
		default:
			return ErrInvalidAssetInterface
		}
	}

	var cacheInstance loader.Cache
	if val, ok := args.Get(`Cache`); ok {
		switch val.(type) {
		case string:
			if cache, err := loader.NewFileCache(val.(string)); err != nil {
				return err
			} else {
				cacheInstance = cache
			}
		case fmt.Stringer:
			if cache, err := loader.NewFileCache(val.(fmt.Stringer).String()); err != nil {
				return err
			} else {
				cacheInstance = cache
			}
		case loader.Cache:
			cacheInstance = val.(loader.Cache)
		default:
			return ErrInvalidCacheArgs
		}
	} else {
		cacheInstance = make(loader.MemoryCache)
	}

	var cacheLevel loader.CacheStrategy
	if val, ok := args.Get(`CacheLevel`); !ok {
		cacheLevel = 1
	} else {
		if data, ok := val.(loader.CacheStrategy); !ok {
			return ErrInvalidCacheLevel
		} else {
			cacheLevel = data
		}
	}

	tx.Loader = loader.NewCachedByteCodeLoader(cacheInstance, cacheLevel, &fetcher{asset}, tx.Parser, tx.Compiler)
	return nil
}