Пример #1
0
func (c *Conn) Render(file interface{}, forms ...*forms.Form) {
	// TODO: c.Tmpl["h"] = &helpers.Helpers{}
	res := ""
	switch file.(type) {
	case string:
		uri, _ := file.(string)
		// add flash to the template context if it exists
		c.addTemplateFlash()
		res = mustache.RenderFile(uri, c.Tmpl)
	case view:
		v, _ := file.(view)
		// add flash to the viewhelper view to be rendered
		view := c.addViewFlash(v)
		res = mustache.RenderFile(view.Uri(), view)
	}
	// if there are forms supplied, the user failed
	// at providing valid input, so we must fill in the
	// appropriate form values along with the error messages
	for _, form := range forms {
		defaultVals := make(map[string]string)
		for name, field := range form.Fields {
			defaultVals[name] = field.Value()
		}
		res = htmlfiller.Fill(res, defaultVals, form.Errors)
	}
	c.WriteString(res)
}
Пример #2
0
func (s *S) TestFill(c *C) {
	defaultVals := make(map[string]string)
	defaultVals["elem1"] = "val"
	defaultVals["elem2"] = "opt2"
	defaultVals["elem3"] = "val"

	errors := make(map[string]string)
	errors["elem1"] = "Invalid value"
	errors["elem2"] = "Invalid option"
	errors["elem3"] = "Invalid value"

	html := `<form action="">
    <span class="error_message" id="elem1_error"></span>
    <input type="text" name="elem1"/>
    <span class="error_message" id="elem2_error"></span>
    <select name="elem2">
    <option value="opt1">Option 1</option>
    <option value="opt2">Option 2</option>
    </select>
    <span class="error_message" id="elem3_error"></span>
    <textarea name="elem3"></textarea>
    </form>`
	obsHtml, _ := htmlfiller.Fill(html, defaultVals, errors)
	expHtml := `<form action="">
    <span class="error_message" id="elem1_error">Invalid value</span>
    <input type="text" name="elem1" value="val"/>
    <span class="error_message" id="elem2_error">Invalid option</span>
    <select name="elem2">
    <option value="opt1">Option 1</option>
    <option value="opt2" selected="selected">Option 2</option>
    </select>
    <span class="error_message" id="elem3_error">Invalid value</span>
    <textarea name="elem3">val</textarea>
    </form>`
	c.Check(obsHtml, Equals, expHtml)

	html = `<form action="">
    <span class="error_message" id="elem1_error"></span>
    <input type="text" name="elem1" value="old"/>
    <span class="error_message" id="elem2_error"></span>
    <select name="elem2">
    <option value="opt1" selected="selected">Option 1</option>
    <option value="opt2">Option 2</option>
    </select>
    <span class="error_message" id="elem3_error"></span>
    <textarea name="elem3">old</textarea>
    </form>`
	obsHtml, _ = htmlfiller.Fill(html, defaultVals, errors)
	expHtml = `<form action="">
    <span class="error_message" id="elem1_error">Invalid value</span>
    <input type="text" name="elem1" value="val"/>
    <span class="error_message" id="elem2_error">Invalid option</span>
    <select name="elem2">
    <option value="opt1">Option 1</option>
    <option value="opt2" selected="selected">Option 2</option>
    </select>
    <span class="error_message" id="elem3_error">Invalid value</span>
    <textarea name="elem3">val</textarea>
    </form>`
	c.Check(obsHtml, Equals, expHtml)
}