code refacturing, added render package comments
This commit is contained in:
@ -1,87 +1,77 @@
|
||||
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"
|
||||
"fmt"
|
||||
"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)
|
||||
fmt.Println(err)
|
||||
|
||||
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")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
foot, err := os.ReadFile("web/views/foot.html")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
// 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"))
|
||||
|
||||
template := "" + string(head) + string(page) + string(foot)
|
||||
fmt.Println(err)
|
||||
|
||||
return template
|
||||
}
|
||||
|
||||
func Template_with_page_vars(str string, tags_and_variables map[string]string) string {
|
||||
head, err := os.ReadFile("web/views/head.html")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
foot, err := os.ReadFile("web/views/foot.html")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
|
||||
page := ""
|
||||
for tag, varr := range tags_and_variables {
|
||||
page = templating(str, tag, varr)
|
||||
}
|
||||
// combining html parts
|
||||
template := "" + string(head) + string(page) + string(foot)
|
||||
|
||||
return template
|
||||
}
|
||||
|
||||
func Template_of_tag(str string, html_tag string) string {
|
||||
// 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")
|
||||
result := strings.Replace(string(tag), "{{fnr}}", str, 1)
|
||||
// handling error by lib function and exiting if it occures
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
|
||||
return result
|
||||
return string(tag)
|
||||
}
|
||||
|
||||
func Template_with_part(part_name string) string {
|
||||
// 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")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
page, err := os.ReadFile("web/views/part/" + part_name + ".html")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
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)
|
||||
fmt.Println(err)
|
||||
|
||||
return template
|
||||
}
|
||||
|
||||
func templating(file string, tag string, variable string) string {
|
||||
content, err := os.ReadFile("web/views/" + file + ".html")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
|
||||
find_tag := `{{` + tag + `}}`
|
||||
|
||||
template_result := strings.Replace(string(content), find_tag, variable, 1)
|
||||
|
||||
return template_result
|
||||
}
|
||||
|
||||
// getting template string and replacing templating tags with variables
|
||||
func Put_vars_to_template(template string, vars map[string]string) string {
|
||||
for key, value := range vars {
|
||||
fmt.Print(key, value)
|
||||
template = strings.Replace(template, "{{"+key+"}}", value, 1)
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user