Files
api_web_manager/web/render/render.go

95 lines
3.7 KiB
Go

package render
// light renderer package that used for generating html templates
// loading the template files from views folder
// using part folder as template parts
// using tag folder as templates of html tags
import (
"api_manager/lib"
"os"
"strings"
)
// loading default head and foot template and adding custom string in the middle
func Template_with_string(str string) string {
// loading head.html and foot.html
// handling error by lib function and exiting if it occures
head, err := os.ReadFile(Get_template("head","")); lib.Handle_err(err, map[string]string{"die":"1"})
foot, err := os.ReadFile(Get_template("foot","")); lib.Handle_err(err, map[string]string{"die":"1"})
// combining html parts
template := "" + string(head) + str + string(foot)
lib.Debug(template,"Template_with_string") // debuging the template
return template
}
// loading default head and foot template and adding html template from file in the middle
func Template_with_page(str string) string {
// loading head.html and foot.html
// handling error by lib function and exiting if it occures
head, err := os.ReadFile(Get_template("head","")); lib.Handle_err(err, map[string]string{"die":"1"})
foot, err := os.ReadFile(Get_template("foot","")); lib.Handle_err(err, map[string]string{"die":"1"})
// loading custom html template file
page, err := os.ReadFile(Get_template(str,"part")); lib.Handle_err(err, map[string]string{"die":"1"})
// combining html parts
template := "" + string(head) + string(page) + string(foot)
lib.Debug(template,"Template_with_page") // debuging the template output
return template
}
// loading default head and foot template and adding html template from tag file in the middle
func Template_of_tag(html_tag string) string {
// loading head.html and foot.html
// handling error by lib function and exiting if it occures
tag, err := os.ReadFile(Get_template(html_tag,"tag")); lib.Handle_err(err, map[string]string{"die":"1"})
lib.Debug(string(tag),"Template_of_tag") // debuging the template output
return string(tag)
}
// loading default head and foot template and adding html template from part file in the middle
func Template_of_part(part_name string) string {
// loading head.html and foot.html
// handling error by lib function and exiting if it occures
head, err := os.ReadFile(Get_template("head","")); lib.Handle_err(err, map[string]string{"die":"1"})
foot, err := os.ReadFile(Get_template("foot","")); lib.Handle_err(err, map[string]string{"die":"1"})
page, err := os.ReadFile(Get_template(part_name,"part")); lib.Handle_err(err, map[string]string{"die":"1"})
// combining html parts
template := "" + string(head) + string(page) + string(foot)
lib.Debug(template,"Template_of_part") // debuging the template output
return template
}
// getting template string and replacing templating tags with variables
func Put_vars_to_template(template string, vars map[string]string) string {
// run through vars array gettin key as tag to find in template and value as value to replace it with
for tag, value := range vars {
// find the tag and replace it with value
template = strings.Replace(template, "{{"+tag+"}}", value, 1)
}
lib.Debug(template,"Put_vars_to_template") // debuging the template output
return template
}
// searchin default folder and retriving the template files in web views folder
// type of template searching for sub folder in views
func Get_template(path_to_template string, type_of_template string) string{
path := ""
if type_of_template == ""{
path = lib.Project_dir()+"/web/views/"+path_to_template+".html";
}else {
path = lib.Project_dir()+"/web/views/"+type_of_template+"/"+path_to_template+".html";
}
lib.Debug(path,"Get_template") // debuging the template
return path;
}