added variable templating, template parts
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
{
|
||||
"main":{"asd":"asd"},
|
||||
"main":{"body":"message"},
|
||||
"sec":"/api/sec",
|
||||
"thi":"/api/thi"
|
||||
}
|
||||
@ -3,6 +3,7 @@ package manager
|
||||
// package for the web menagment of the api paths
|
||||
|
||||
import (
|
||||
"api_manager/lib"
|
||||
"api_manager/web/api"
|
||||
"api_manager/web/render"
|
||||
"fmt"
|
||||
@ -29,3 +30,18 @@ func Show_apis(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
fmt.Fprint(w, render.Template_with_string(eval))
|
||||
}
|
||||
|
||||
func Render_apis(w http.ResponseWriter, req *http.Request) {
|
||||
apis := api.Apis()
|
||||
eval := "<div>"
|
||||
for tag, varr := range apis {
|
||||
eval_group := ""
|
||||
eval_group += tag
|
||||
eval_group += "-"
|
||||
eval_group += fmt.Sprintf("%v", varr)
|
||||
eval_group += "<br>"
|
||||
eval += eval_group
|
||||
}
|
||||
eval += "</div>"
|
||||
fmt.Fprint(w, render.Put_vars_to_template(render.Template_with_part("json_out"), lib.Map_args("json_code", eval)))
|
||||
}
|
||||
|
||||
@ -47,19 +47,42 @@ func Template_with_page_vars(str string, tags_and_variables map[string]string) s
|
||||
|
||||
func Template_of_tag(str string, html_tag string) string {
|
||||
tag, err := os.ReadFile("web/views/tag/" + html_tag + ".html")
|
||||
result := strings.Replace(string(tag), "<fnr>", str, 1)
|
||||
result := strings.Replace(string(tag), "{{fnr}}", str, 1)
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func Template_with_part(part_name string) string {
|
||||
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")
|
||||
lib.Handle_err(err, lib.Map_args("die", "1"))
|
||||
|
||||
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 + `>`
|
||||
find_tag := `{{` + tag + `}}`
|
||||
|
||||
template_result := strings.Replace(string(content), find_tag, variable, 1)
|
||||
|
||||
return template_result
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return template
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
<title>{{template_title}}</title>
|
||||
|
||||
<style></style>
|
||||
</head>
|
||||
<body>
|
||||
@ -1 +1 @@
|
||||
<welcome_message>
|
||||
{{welcome_message}}
|
||||
3
web/views/part/json_out.html
Normal file
3
web/views/part/json_out.html
Normal file
@ -0,0 +1,3 @@
|
||||
<pre>
|
||||
{{json_code}}
|
||||
</pre>
|
||||
@ -1 +1 @@
|
||||
<key>:<val>
|
||||
{{key}}:{{val}}
|
||||
@ -1,3 +1,3 @@
|
||||
<div>
|
||||
<p><fnr></p>
|
||||
<p>{{fnr}}</p>
|
||||
</div>
|
||||
@ -12,7 +12,7 @@ func Server() {
|
||||
http.HandleFunc("/", main_page)
|
||||
|
||||
http.HandleFunc("/mgr/add", manager.Add_api)
|
||||
http.HandleFunc("/mgr/show", manager.Show_apis)
|
||||
http.HandleFunc("/mgr/show", manager.Render_apis)
|
||||
http.HandleFunc("/api/", api.Get_api)
|
||||
|
||||
http.ListenAndServe(":8090", nil)
|
||||
|
||||
Reference in New Issue
Block a user