79 lines
2.6 KiB
Go
79 lines
2.6 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
|
|
head, err := os.ReadFile("web/views/head.html")
|
|
foot, err := os.ReadFile("web/views/foot.html")
|
|
// handling error by lib function and exiting if it occures
|
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
|
|
// combining html parts
|
|
template := "" + string(head) + str + string(foot)
|
|
|
|
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
|
|
head, err := os.ReadFile("web/views/head.html")
|
|
foot, err := os.ReadFile("web/views/foot.html")
|
|
// loading custom html template file
|
|
page, err := os.ReadFile("web/views/" + str + ".html")
|
|
// handling error by lib function and exiting if it occures
|
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
|
|
// combining html parts
|
|
template := "" + string(head) + string(page) + string(foot)
|
|
|
|
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
|
|
tag, err := os.ReadFile("web/views/tag/" + html_tag + ".html")
|
|
// handling error by lib function and exiting if it occures
|
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
|
|
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
|
|
head, err := os.ReadFile("web/views/head.html")
|
|
page, err := os.ReadFile("web/views/part/" + part_name + ".html")
|
|
foot, err := os.ReadFile("web/views/foot.html")
|
|
// handling error by lib function and exiting if it occures
|
|
lib.Handle_err(err, lib.Map_args("die", "1"))
|
|
|
|
// combining html parts
|
|
template := "" + string(head) + string(page) + string(foot)
|
|
|
|
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)
|
|
}
|
|
|
|
return template
|
|
}
|