func Home() func(w http.ResponseWriter, r *http.Request, params httprouter.Params) { contents, err := ioutil.ReadFile("views/home.html") if err != nil { panic(err) } template, err := raymond.Parse(string(contents)) if err != nil { panic(err) } return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) { var ring_string string // fmt.Println(Toy.Ring) if Toy.Ring == nil { ring_string = "" } else { ring_string = Toy.Ring.String() } context := map[string]interface{}{ "ring": ring_string, "keys": Toy.Data.Keys(), } output, err := template.Exec(context) if err != nil { panic(err) } w.Write([]byte(output)) } }
func TestBasicErrors(t *testing.T) { t.Parallel() var err error inputs := []string{ // this keyword nested inside path "{{#hellos}}{{text/this/foo}}{{/hellos}}", // this keyword nested inside helpers param "{{#hellos}}{{foo text/this/foo}}{{/hellos}}", } expectedError := regexp.QuoteMeta("Invalid path: text/this") for _, input := range inputs { _, err = raymond.Parse(input) if err == nil { t.Errorf("Test failed - Error expected") } match, errMatch := regexp.MatchString(expectedError, fmt.Sprint(err)) if errMatch != nil { panic("Failed to match regexp") } if !match { t.Errorf("Test failed - Expected error:\n\t%s\n\nGot:\n\t%s", expectedError, err) } } }
func (e *Engine) Build(name, tpl string) (interface{}, Error) { t, err := raymond.Parse(tpl) if err != nil { return nil, kit.AppError{ Code: "tpl_parse_error", Message: err.Error(), } } e.templates[name] = t return t, nil }
// BuildTemplates builds the handlebars templates func (e *Engine) BuildTemplates() error { if e.Config.Extensions == nil || len(e.Config.Extensions) == 0 { e.Config.Extensions = []string{".html"} } // register the global helpers if e.Config.Handlebars.Helpers != nil { raymond.RegisterHelpers(e.Config.Handlebars.Helpers) } // the render works like {{ render "myfile.html" theContext.PartialContext}} // instead of the html/template engine which works like {{ render "myfile.html"}} and accepts the parent binding, with handlebars we can't do that because of lack of runtime helpers (dublicate error) raymond.RegisterHelper("render", func(partial string, binding interface{}) raymond.SafeString { contents, err := e.executeTemplateBuf(partial, binding) if err != nil { return raymond.SafeString("Template with name: " + partial + " couldn't not be found.") } return raymond.SafeString(contents) }) var templateErr error dir := e.Config.Directory filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if info == nil || info.IsDir() { return nil } rel, err := filepath.Rel(dir, path) if err != nil { return err } ext := "" if strings.Index(rel, ".") != -1 { ext = filepath.Ext(rel) } for _, extension := range e.Config.Extensions { if ext == extension { buf, err := ioutil.ReadFile(path) contents := string(buf) if err != nil { templateErr = err break } name := filepath.ToSlash(rel) tmpl, err := raymond.Parse(contents) if err != nil { templateErr = err continue } e.mu.Lock() e.templateCache[name] = tmpl e.mu.Unlock() break } } return nil }) return templateErr }
func launchTests(t *testing.T, tests []Test) { t.Parallel() for _, test := range tests { var err error var tpl *raymond.Template if DUMP_TPL { filename := strconv.Itoa(dump_tpl_nb) if err := ioutil.WriteFile(path.Join(".", "dump_tpl", filename), []byte(test.input), 0644); err != nil { panic(err) } dump_tpl_nb += 1 } // parse template tpl, err = raymond.Parse(test.input) if err != nil { t.Errorf("Test '%s' failed - Failed to parse template\ninput:\n\t'%s'\nerror:\n\t%s", test.name, test.input, err) } else { if len(test.helpers) > 0 { // register helpers tpl.RegisterHelpers(test.helpers) } if len(test.partials) > 0 { // register partials tpl.RegisterPartials(test.partials) } // setup private data frame var privData *raymond.DataFrame if test.privData != nil { privData = raymond.NewDataFrame() for k, v := range test.privData { privData.Set(k, v) } } // render template output, err := tpl.ExecWith(test.data, privData) if err != nil { t.Errorf("Test '%s' failed\ninput:\n\t'%s'\ndata:\n\t%s\nerror:\n\t%s\nAST:\n\t%s", test.name, test.input, raymond.Str(test.data), err, tpl.PrintAST()) } else { // check output var expectedArr []string expectedArr, ok := test.output.([]string) if ok { match := false for _, expectedStr := range expectedArr { if expectedStr == output { match = true break } } if !match { t.Errorf("Test '%s' failed\ninput:\n\t'%s'\ndata:\n\t%s\npartials:\n\t%s\nexpected\n\t%q\ngot\n\t%q\nAST:\n%s", test.name, test.input, raymond.Str(test.data), raymond.Str(test.partials), expectedArr, output, tpl.PrintAST()) } } else { expectedStr, ok := test.output.(string) if !ok { panic(fmt.Errorf("Erroneous test output description: %q", test.output)) } if expectedStr != output { t.Errorf("Test '%s' failed\ninput:\n\t'%s'\ndata:\n\t%s\npartials:\n\t%s\nexpected\n\t%q\ngot\n\t%q\nAST:\n%s", test.name, test.input, raymond.Str(test.data), raymond.Str(test.partials), expectedStr, output, tpl.PrintAST()) } } } } } }