Beispiel #1
0
// Get a template from the repository by id
func GetTemplate(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id := vars["id"]

	tmpl, err := getTemplateById(id)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	w.Header().Set("Content-Type", "application/json;")

	fmt.Fprintf(w, tmpl)
	return
}
Beispiel #2
0
// Generate a view of the template using the data passed
func ViewTemplate(w http.ResponseWriter, r *http.Request) {

	// Read in JSON data to bind
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	if err := r.Body.Close(); err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// unmarshal the json to map
	var objmap map[string]interface{}
	err = json.Unmarshal(body, &objmap)

	//msgData, err := gabs.ParseJSON([]byte(body))

	// load template from repository
	vars := mux.Vars(r)
	id := vars["id"]

	// get template from store
	tmpl, err := getTemplateById(id)

	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	// bind the data to the template retrieved from storage
	result, err := raymond.Render(tmpl, objmap)

	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	fmt.Fprintln(w, result)
}