Exemplo n.º 1
0
// RenderInto combines Render() and writing its results into an io.Writer.
// This is a convenience method for frameworks providing a Writer interface,
// such as net/http's ServeHTTP()
func (tx *Xslate) RenderInto(w io.Writer, template string, vars Vars) error {
	bc, err := tx.Loader.Load(template)
	if err != nil {
		return err
	}
	tx.VM.Run(bc, vm.Vars(vars), w)
	return nil
}
Exemplo n.º 2
0
// RenderString takes a string argument and treats it as the template
// content. Like `Render()`, this template is parsed and compiled. Because
// there's no way to establish template "freshness", the resulting bytecode
// from `RenderString()` is not cached for reuse.
//
// If you *really* want to change this behavior, it's not impossible to
// bend Xslate's Loader mechanism to cache strings as well, but the main
// Xslate library will probably not adopt this feature.
func (tx *Xslate) RenderString(template string, vars Vars) (string, error) {
	_, file, line, _ := runtime.Caller(1)
	bc, err := tx.Loader.LoadString(fmt.Sprintf("%s:%d", file, line), template)
	if err != nil {
		return "", err
	}

	buf := &bytes.Buffer{}
	tx.VM.Run(bc, vm.Vars(vars), buf)
	return buf.String(), nil
}
Exemplo n.º 3
0
// RenderString takes a string argument and treats it as the template
// content. Like `Render()`, this template is parsed and compiled. Because
// there's no way to establish template "freshness", the resulting bytecode
// from `RenderString()` is not cached for reuse.
//
// If you *really* want to change this behavior, it's not impossible to
// bend Xslate's Loader mechanism to cache strings as well, but the main
// Xslate library will probably not adopt this feature.
func (tx *Xslate) RenderString(template string, vars Vars) (string, error) {
	_, file, line, _ := runtime.Caller(1)
	bc, err := tx.Loader.LoadString(fmt.Sprintf("%s:%d", file, line), template)
	if err != nil {
		return "", errors.Wrap(err, "failed to parse template string")
	}

	buf := rbpool.Get()
	defer rbpool.Release(buf)

	tx.VM.Run(bc, vm.Vars(vars), buf)
	return buf.String(), nil
}
Exemplo n.º 4
0
// Configure is called automatically from `New()` to configure the xslate
// instance from arguments
func (tx *Xslate) Configure(args ConfigureArgs) error {
	// The compiler currently does not have any configurable options, but
	// one may want to replace the entire compiler struct
	defaults := map[string]func(*Xslate, Args) error{
		"Compiler": DefaultCompiler,
		"Parser":   DefaultParser,
		"Loader":   DefaultLoader,
		"VM":       DefaultVM,
	}

	for _, key := range []string{"Parser", "Compiler", "Loader", "VM"} {
		configKey := "Configure" + key
		configuror, ok := args.Get(configKey)
		if !ok {
			configuror = defaults[key]
		}

		args, ok := args.Get(key)
		if !ok {
			args = Args{}
		}

		err := tx.configureGeneric(configuror, args.(Args))
		if err != nil {
			return err
		}
	}

	// Configure Functions
	if funcs, ok := args.Get("Functions"); ok {
		tx.VM.SetFunctions(vm.Vars(funcs.(Args)))
	}

	if Debug {
		tx.DumpAST(true)
		tx.DumpByteCode(true)
	}

	return nil
}