// Builds a form context and renders a sub-template inside of it. // Example: // {{#FormFor (controller) (method) [enctype] (formId)} } // {{ > app/views/users/register.template }} // {{/FormFor}} // Where the register.template can now use any of the Form helpers. func BuildForm(params []string, data string) string { var controllerName string var httpMethod string var formId string var enctype string // Parse parameters if len(params) == 4 { controllerName = params[0] httpMethod = params[1] enctype = params[2] formId = params[3] } else if len(params) == 3 { controllerName = params[0] httpMethod = params[1] formId = params[2] } else if len(params) == 2 { controllerName = params[0] formId = params[0] httpMethod = params[1] } else if len(params) == 1 { controllerName = params[0] formId = params[0] httpMethod = "post" //default to post for a form. } // Try to generate a route for the form results url, err := buildUrlWithVerb(controllerName, httpMethod) if err != nil { fmt.Printf("Unable to build a route for: %s\n debug err: %s", params, err) return "" } // Opening and closing form tags var openTag string // Use default enctype unless it is specified if enctype != "" { openTag = fmt.Sprintf("<form id=\"%s\" action=\"%s\" method=\"%s\" enctype=\"%s\">", formId, url, httpMethod, enctype) } else { openTag = fmt.Sprintf("<form id=\"%s\" action=\"%s\" method=\"%s\">", formId, url, httpMethod) } closeTag := fmt.Sprintf("</form>") // Render inner-content with Form context formBody := mustache.Render(data, getFormHelpers()) return fmt.Sprintf("%s\n%s\n%s", openTag, formBody, closeTag) }
// CSS for the menu, and a bit more func GenerateMenuCSS(state pinterface.IUserState, stretchBackground bool, cs *ColorScheme) webhandle.SimpleContextHandle { return func(ctx *web.Context) string { ctx.ContentType("css") // one of the extra css files that are loaded after the main style retval := mustache.Render(menustyle_tmpl, cs) // The load order of background-color, background-size and background-image // is actually significant in some browsers! Do not reorder lightly. if stretchBackground { retval = "body {\nbackground-color: " + cs.Default_background + ";\nbackground-size: cover;\n}\n" + retval } else { retval = "body {\nbackground-color: " + cs.Default_background + ";\n}\n" + retval } retval += ".titletext { display: inline; }" return retval } }
// Render a page by inserting data at the {{{placeholders}}} for both html and css func RenderPage(page *onthefly.Page, templateContents map[string]string) (string, string) { // Note that the whitespace formatting of the generated html matter for the menu layout! return mustache.Render(page.String(), templateContents), mustache.Render(page.GetCSS(), templateContents) }
// Create a web.go compatible function that returns a string that is the HTML for this page func GenerateHTMLwithTemplate(page *onthefly.Page, tvg webhandle.TemplateValueGenerator) func(*web.Context) string { return func(ctx *web.Context) string { values := tvg(ctx) return mustache.Render(page.GetXML(true), values) } }