aymerick / raymond

Handlebars for golang
MIT License
610 stars 101 forks source link

Do not transferred the context in RegisterHelper in function rendering #2

Closed ghostiam closed 8 years ago

ghostiam commented 8 years ago

Hello, I need to recursively load templates, but the context is not rendered

package main

import (
    "fmt"

    "github.com/aymerick/raymond"
)

type Author struct {
    FirstName string `json:"firstName"`
    LastName  string `json:"lastName"`
}

func main() {
    raymond.RegisterHelper("template", func(name string, options *raymond.Options) raymond.SafeString {
        context := options.Ctx()
        // Load template from file
        template := name + " - {{ firstName }} {{ lastName }}"

        result, _ := raymond.Render(template, context)
        return raymond.SafeString(result)
    })

    template := `By {{ template "namefile" }}`
    context := Author{"Alan", "Johnson"}

    result, _ := raymond.Render(template, context)

    fmt.Println(result)
}

Expected:

By namefile - Alan Johnson

but got:

By namefile -

Same thing on JS https://jsfiddle.net/Ghost_Russia/dv8tjekp/

ghostiam commented 8 years ago

If change the function, it works. May be an error here?

117 // Ctx returns current evaluation context.
118 func (options *Options) Ctx() interface{} {
- 119 return options.eval.curCtx()
+ 119 return options.eval.curCtx().Interface()
120 }
aymerick commented 8 years ago

I published raymond 2.0.0 with your bugfix.

Thanks a lot for reporting that issue.

ghostiam commented 8 years ago

@aymerick Thank you too for this amazing library!