code refacturing, added render package comments
This commit is contained in:
10
lib/lib.go
10
lib/lib.go
@ -55,9 +55,9 @@ func Map_args(array ...string) map[string]string {
|
||||
return result
|
||||
}
|
||||
|
||||
// func Map_of_string_to_josn(array map[string]string) string {
|
||||
// jData, err := json.Marshal(array)
|
||||
// lib.Handle_err(err, lib.Map_args())
|
||||
func Map_of_string_to_josn(array map[string]string) string {
|
||||
jData, err := json.Marshal(array)
|
||||
Handle_err(err, Map_args())
|
||||
|
||||
// return string(jData)
|
||||
// }
|
||||
return string(jData)
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
{
|
||||
"main":{"body":"message"},
|
||||
"sec":"/api/sec",
|
||||
"thi":"/api/thi"
|
||||
"sec":"second",
|
||||
"thi":"third"
|
||||
}
|
||||
@ -15,33 +15,24 @@ func Add_api(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Fprintf(w, "good")
|
||||
}
|
||||
|
||||
func Show_apis(w http.ResponseWriter, req *http.Request) {
|
||||
apis := api.Apis()
|
||||
eval := "<div>"
|
||||
for tag, varr := range apis {
|
||||
eval_group := ""
|
||||
eval_group += fmt.Sprintf("%v", varr)
|
||||
eval_group += "-"
|
||||
eval_group += tag
|
||||
eval_group += "<br>"
|
||||
eval += render.Template_of_tag(eval_group, "text")
|
||||
}
|
||||
eval += "</div>"
|
||||
|
||||
fmt.Fprint(w, render.Template_with_string(eval))
|
||||
}
|
||||
|
||||
func Render_apis(w http.ResponseWriter, req *http.Request) {
|
||||
apis := api.Apis()
|
||||
eval := "<div>"
|
||||
eval := ""
|
||||
i := 0
|
||||
for tag, varr := range apis {
|
||||
eval_group := ""
|
||||
eval_group += tag
|
||||
eval_group += "-"
|
||||
eval_group += "<br>-"
|
||||
eval_group += fmt.Sprintf("%v", varr)
|
||||
eval_group += "<br>"
|
||||
eval += eval_group
|
||||
|
||||
eval += render.Put_vars_to_template(
|
||||
render.Template_of_tag("li"),
|
||||
lib.Map_args("li_id", fmt.Sprint(i), "content", eval_group))
|
||||
i++
|
||||
}
|
||||
eval += "</div>"
|
||||
fmt.Fprint(w, render.Put_vars_to_template(render.Template_with_part("json_out"), lib.Map_args("json_code", eval)))
|
||||
fmt.Fprint(w,
|
||||
render.Put_vars_to_template(
|
||||
render.Template_of_part("json_out_as_list"),
|
||||
lib.Map_args("li_from_json_code", eval)))
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
<pre>
|
||||
{{json_code}}
|
||||
</pre>
|
||||
3
web/views/part/json_out_as_list.html
Normal file
3
web/views/part/json_out_as_list.html
Normal file
@ -0,0 +1,3 @@
|
||||
<ul>
|
||||
{{li_from_json_code}}
|
||||
</ul>
|
||||
1
web/views/tag/li.html
Normal file
1
web/views/tag/li.html
Normal file
@ -0,0 +1 @@
|
||||
<li class="list-item{{li_id}}">{{content}}</li>
|
||||
@ -21,5 +21,5 @@ func Server() {
|
||||
// wellcoming main web page
|
||||
func main_page(w http.ResponseWriter, req *http.Request) {
|
||||
templ := map[string]string{"welcome_message": "hello world! this is api server with easy web menagment"}
|
||||
fmt.Fprint(w, render.Template_with_page_vars("main", templ))
|
||||
fmt.Fprint(w, render.Put_vars_to_template(render.Template_of_part("main"), templ))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user