Improved performance dramatically with the compiled templates.

Added the benchmark script.
The template generator now tracks the variables which are used the most in a template.
This commit is contained in:
Azareal 2016-12-17 03:39:53 +00:00
parent 7b1f27bf41
commit 2b9a9704fb
10 changed files with 774 additions and 306 deletions

59
general_test.go Normal file
View File

@ -0,0 +1,59 @@
package main
import "testing"
import "io/ioutil"
import "html/template"
func BenchmarkTemplates(b *testing.B) {
user := User{0,"Bob",0,false,false,false,false,false,false,"",false,"","","","",""}
admin := User{1,"Admin",0,true,true,true,true,true,false,"",false,"","","","",""}
var noticeList map[int]string = make(map[int]string)
noticeList[0] = "test"
topic := TopicUser{0,"Lol",template.HTML("Hey everyone!"),0,false,false,"",0,"","","",no_css_tmpl,0,"","","",""}
var replyList map[int]interface{} = make(map[int]interface{})
replyList[0] = Reply{0,0,"Hey everyone!",template.HTML("Hey everyone!"),0,"","",0,0,"",no_css_tmpl,0,"","","",""}
pi := Page{"Topic Blah","topic",user,noticeList,replyList,topic}
pi2 := Page{"Topic Blah","topic",admin,noticeList,replyList,topic}
w := ioutil.Discard
b.Run("compiled_writer_collated_useradmin", func(b *testing.B) {
for i := 0; i < b.N; i++ {
template_topic(pi2,w)
}
})
b.Run("compiled_writer_useradmin", func(b *testing.B) {
for i := 0; i < b.N; i++ {
template_topic2(pi2,w)
}
})
b.Run("compiled_useradmin", func(b *testing.B) {
for i := 0; i < b.N; i++ {
w.Write([]byte(template_topic3(pi2)))
}
})
b.Run("interpreted_useradmin", func(b *testing.B) {
for i := 0; i < b.N; i++ {
templates.ExecuteTemplate(w,"topic.html", pi2)
}
})
b.Run("compiled_writer_collated_userguest", func(b *testing.B) {
for i := 0; i < b.N; i++ {
template_topic(pi,w)
}
})
b.Run("compiled_writer_userguest", func(b *testing.B) {
for i := 0; i < b.N; i++ {
template_topic2(pi,w)
}
})
b.Run("compiled_userguest", func(b *testing.B) {
for i := 0; i < b.N; i++ {
w.Write([]byte(template_topic3(pi)))
}
})
b.Run("interpreted_userguest", func(b *testing.B) {
for i := 0; i < b.N; i++ {
templates.ExecuteTemplate(w,"topic.html", pi)
}
})
}

Binary file not shown.

BIN
images/gosora-test.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -8,6 +8,7 @@ import (
"mime"
"strings"
"path/filepath"
"io"
"io/ioutil"
"os"
"html/template"
@ -31,7 +32,7 @@ var external_sites map[string]string = make(map[string]string)
var groups map[int]Group = make(map[int]Group)
var forums map[int]Forum = make(map[int]Forum)
var static_files map[string]SFile = make(map[string]SFile)
var ctemplates map[string]func(Page)string = make(map[string]func(Page)string)
var ctemplates map[string]func(Page,io.Writer) = make(map[string]func(Page,io.Writer))
func compile_templates() {
var c CTemplateSet

View File

@ -413,7 +413,7 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){
pi := Page{topic.Title,"topic",user,noticeList,replyList,topic}
if ctemplates["topic"] != nil {
w.Write([]byte(ctemplates["topic"](pi)))
ctemplates["topic"](pi,w)
} else {
err = templates.ExecuteTemplate(w,"topic.html", pi)
if err != nil {

View File

@ -1,176 +1,130 @@
package main
import "io"
import "strconv"
func init() {
ctemplates["profile"] = template_profile
}
func template_profile(tmpl_profile_vars Page) (tmpl string) {
func template_profile(tmpl_profile_vars Page, w io.Writer) {
var extra_data User = tmpl_profile_vars.Something.(User)
tmpl += `<!doctype html>
w.Write([]byte(`<!doctype html>
<html lang="en">
<head>
<title>`
tmpl += tmpl_profile_vars.Title
tmpl += `</title>
<title>` + tmpl_profile_vars.Title + `</title>
<link href="/static/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
var session = "`
tmpl += tmpl_profile_vars.CurrentUser.Session
tmpl += `";
var session = "` + tmpl_profile_vars.CurrentUser.Session + `";
</script>
<script type="text/javascript" src="/static/global.js"></script>
</head>
<body>
<div class="container">
`
tmpl += `<div class="nav">
<div class="nav">
<ul>
<li class="menu_overview"><a href="/">Overview</a></li>
<li class="menu_forums"><a href="/forums/">Forums</a></li>
<li class="menu_topics"><a href="/">Topics</a></li>
<li class="menu_create_topic"><a href="/topics/create/">Create Topic</a></li>
`
`))
if tmpl_profile_vars.CurrentUser.Loggedin {
tmpl += `
w.Write([]byte(`
<li class="menu_account"><a href="/user/edit/critical/">Account</a></li>
<li class="menu_account"><a href="/user/`
tmpl += strconv.Itoa(tmpl_profile_vars.CurrentUser.ID)
tmpl += `">Profile</a></li>
`
<li class="menu_account"><a href="/user/` + strconv.Itoa(tmpl_profile_vars.CurrentUser.ID) + `">Profile</a></li>
`))
if tmpl_profile_vars.CurrentUser.Is_Super_Mod {
tmpl += `<li class="menu_account"><a href="/panel/forums/">Panel</a></li>`
w.Write([]byte(`<li class="menu_account"><a href="/panel/forums/">Panel</a></li>`))
}
tmpl += `
<li class="menu_logout"><a href="/accounts/logout?session=`
tmpl += tmpl_profile_vars.CurrentUser.Session
tmpl += `">Logout</a></li>
`
w.Write([]byte(`
<li class="menu_logout"><a href="/accounts/logout?session=` + tmpl_profile_vars.CurrentUser.Session + `">Logout</a></li>
`))
} else {
tmpl += `
w.Write([]byte(`
<li class="menu_register"><a href="/accounts/create/">Register</a></li>
<li class="menu_login"><a href="/accounts/login/">Login</a></li>
`
`))
}
tmpl += `
w.Write([]byte(`
</ul>
</div>`
tmpl += `
`
</div>
`))
if len(tmpl_profile_vars.NoticeList) != 0 {
for _, item := range tmpl_profile_vars.NoticeList {
tmpl += `<div class="alert">`
tmpl += item
tmpl += `</div>`
w.Write([]byte(`<div class="alert">` + item + `</div>`))
}
}
tmpl += `
w.Write([]byte(`
<div class="colblock_left" style="max-width: 220px;">
<div class="rowitem" style="padding: 0;"><img src="`
tmpl += extra_data.Avatar
tmpl += `" style="max-width: 100%;margin: 0;"/></div>
<div class="rowitem" style="padding: 0;"><img src="` + extra_data.Avatar + `" style="max-width: 100%;margin: 0;"/></div>
<div class="rowitem" style="text-transform: capitalize;">
<span style="font-size: 18px;">`
tmpl += extra_data.Name
tmpl += `</span>`
<span style="font-size: 18px;">` + extra_data.Name + `</span>`))
if extra_data.Tag != "" {
tmpl += `<span class="username" style="float: right;">`
tmpl += extra_data.Tag
tmpl += `</span>`
w.Write([]byte(`<span class="username" style="float: right;">` + extra_data.Tag + `</span>`))
}
tmpl += `
w.Write([]byte(`
</div>
<div class="rowitem passive">
<a class="username">Add Friend</a>
`
`))
if tmpl_profile_vars.CurrentUser.Is_Super_Mod && !extra_data.Is_Super_Mod {
tmpl += `
`
w.Write([]byte(`
`))
if extra_data.Is_Banned {
tmpl += `<a href="/users/unban/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `" class="username">Unban</a>`
w.Write([]byte(`<a href="/users/unban/` + strconv.Itoa(extra_data.ID) + `" class="username">Unban</a>`))
} else {
tmpl += `<a href="/users/ban/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `" class="username">Ban</a>`
w.Write([]byte(`<a href="/users/ban/` + strconv.Itoa(extra_data.ID) + `" class="username">Ban</a>`))
}
tmpl += `
`
w.Write([]byte(`
`))
}
tmpl += `
<a href="/report/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `?session=`
tmpl += tmpl_profile_vars.CurrentUser.Session
tmpl += `&type=user" class="username report_item">Report</a>
w.Write([]byte(`
<a href="/report/submit/` + strconv.Itoa(extra_data.ID) + `?session=` + tmpl_profile_vars.CurrentUser.Session + `&type=user" class="username report_item">Report</a>
</div>
</div>
<div class="colblock_right">
<div class="rowitem"><a>Comments</a></div>
</div>
<div class="colblock_right" style="overflow: hidden;">
`
`))
if len(tmpl_profile_vars.ItemList) != 0 {
for _, item := range tmpl_profile_vars.ItemList {
tmpl += `
<div class="rowitem passive deletable_block editable_parent" style="`
w.Write([]byte(`
<div class="rowitem passive deletable_block editable_parent" style="`))
if item.(Reply).Avatar != "" {
tmpl += `background-image: url(`
tmpl += item.(Reply).Avatar
tmpl += `), url(/static/white-dot.jpg);background-position: 0px `
w.Write([]byte(`background-image: url(` + item.(Reply).Avatar + `), url(/static/white-dot.jpg);background-position: 0px `))
if item.(Reply).ContentLines <= 5 {
tmpl += `-1`
w.Write([]byte(`-1`))
}
tmpl += `0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;`
tmpl += string(item.(Reply).Css)
w.Write([]byte(`0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;` + string(item.(Reply).Css)))
}
tmpl += `">
<span class="editable_block user_content">`
tmpl += string(item.(Reply).ContentHtml)
tmpl += `</span>
w.Write([]byte(`">
<span class="editable_block user_content">` + string(item.(Reply).ContentHtml) + `</span>
<br /><br />
<a href="/user/`
tmpl += strconv.Itoa(item.(Reply).CreatedBy)
tmpl += `" class="username">`
tmpl += item.(Reply).CreatedByName
tmpl += `</a>
`
<a href="/user/` + strconv.Itoa(item.(Reply).CreatedBy) + `" class="username">` + item.(Reply).CreatedByName + `</a>
`))
if tmpl_profile_vars.CurrentUser.Is_Mod {
tmpl += `<a href="/profile/reply/edit/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `"><button class="username edit_item">Edit</button></a>
<a href="/profile/reply/delete/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `"><button class="username delete_item">Delete</button></a>`
w.Write([]byte(`<a href="/profile/reply/edit/submit/` + strconv.Itoa(item.(Reply).ID) + `"><button class="username edit_item">Edit</button></a>
<a href="/profile/reply/delete/submit/` + strconv.Itoa(item.(Reply).ID) + `"><button class="username delete_item">Delete</button></a>`))
}
tmpl += `
<a href="/report/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `?session=`
tmpl += tmpl_profile_vars.CurrentUser.Session
tmpl += `&type=user-reply"><button class="username report_item">Report</button></a>
`
w.Write([]byte(`
<a href="/report/submit/` + strconv.Itoa(item.(Reply).ID) + `?session=` + tmpl_profile_vars.CurrentUser.Session + `&type=user-reply"><button class="username report_item">Report</button></a>
`))
if item.(Reply).Tag != "" {
tmpl += `<a class="username" style="float: right;">`
tmpl += item.(Reply).Tag
tmpl += `</a>`
w.Write([]byte(`<a class="username" style="float: right;">` + item.(Reply).Tag + `</a>`))
}
tmpl += `
</div>`
w.Write([]byte(`
</div>`))
}
}
tmpl += `
w.Write([]byte(`
</div>
`
`))
if !tmpl_profile_vars.CurrentUser.Is_Banned {
tmpl += `
w.Write([]byte(`
<div class="colblock_right">
<form action="/profile/reply/create/" method="post">
<input name="uid" value='`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' type="hidden" />
<input name="uid" value='` + strconv.Itoa(extra_data.ID) + `' type="hidden" />
<div class="formrow">
<div class="formitem"><textarea name="reply-content" placeholder="Insert reply here"></textarea></div>
</div>
@ -179,13 +133,11 @@ tmpl += `' type="hidden" />
</div>
</form>
</div>
`
`))
}
tmpl += `
`
tmpl += ` <!--<link rel="stylesheet" href="https://use.fontawesome.com/8670aa03ca.css">-->
w.Write([]byte(`
<!--<link rel="stylesheet" href="https://use.fontawesome.com/8670aa03ca.css">-->
</div>
</body>
</html>`
return tmpl
</html>`))
}

View File

@ -1,246 +1,168 @@
package main
import "io"
import "strconv"
import "html/template"
func init() {
ctemplates["topic"] = template_topic
}
func template_topic(tmpl_topic_vars Page) (tmpl string) {
func template_topic(tmpl_topic_vars Page, w io.Writer) {
var extra_data TopicUser = tmpl_topic_vars.Something.(TopicUser)
tmpl += `<!doctype html>
w.Write([]byte(`<!doctype html>
<html lang="en">
<head>
<title>`
tmpl += tmpl_topic_vars.Title
tmpl += `</title>
<title>` + tmpl_topic_vars.Title + `</title>
<link href="/static/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
var session = "`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `";
var session = "` + tmpl_topic_vars.CurrentUser.Session + `";
</script>
<script type="text/javascript" src="/static/global.js"></script>
</head>
<body>
<div class="container">
`
tmpl += `<div class="nav">
<div class="nav">
<ul>
<li class="menu_overview"><a href="/">Overview</a></li>
<li class="menu_forums"><a href="/forums/">Forums</a></li>
<li class="menu_topics"><a href="/">Topics</a></li>
<li class="menu_create_topic"><a href="/topics/create/">Create Topic</a></li>
`
`))
if tmpl_topic_vars.CurrentUser.Loggedin {
tmpl += `
w.Write([]byte(`
<li class="menu_account"><a href="/user/edit/critical/">Account</a></li>
<li class="menu_account"><a href="/user/`
tmpl += strconv.Itoa(tmpl_topic_vars.CurrentUser.ID)
tmpl += `">Profile</a></li>
`
<li class="menu_account"><a href="/user/` + strconv.Itoa(tmpl_topic_vars.CurrentUser.ID) + `">Profile</a></li>
`))
if tmpl_topic_vars.CurrentUser.Is_Super_Mod {
tmpl += `<li class="menu_account"><a href="/panel/forums/">Panel</a></li>`
w.Write([]byte(`<li class="menu_account"><a href="/panel/forums/">Panel</a></li>`))
}
tmpl += `
<li class="menu_logout"><a href="/accounts/logout?session=`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `">Logout</a></li>
`
w.Write([]byte(`
<li class="menu_logout"><a href="/accounts/logout?session=` + tmpl_topic_vars.CurrentUser.Session + `">Logout</a></li>
`))
} else {
tmpl += `
w.Write([]byte(`
<li class="menu_register"><a href="/accounts/create/">Register</a></li>
<li class="menu_login"><a href="/accounts/login/">Login</a></li>
`
`))
}
tmpl += `
w.Write([]byte(`
</ul>
</div>`
tmpl += `
`
</div>
`))
if len(tmpl_topic_vars.NoticeList) != 0 {
for _, item := range tmpl_topic_vars.NoticeList {
tmpl += `<div class="alert">`
tmpl += item
tmpl += `</div>`
w.Write([]byte(`<div class="alert">` + item + `</div>`))
}
}
tmpl += `
w.Write([]byte(`
<div class="rowblock">
<form action='/topic/edit/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' method="post">
<div class="rowitem"`
<form action='/topic/edit/submit/` + strconv.Itoa(extra_data.ID) + `' method="post">
<div class="rowitem"`))
if extra_data.Sticky {
tmpl += ` style="background-color: #FFFFEA;"`
w.Write([]byte(` style="background-color: #FFFFEA;"`))
}
tmpl += `>
<a class='topic_name hide_on_edit'>`
tmpl += extra_data.Title
tmpl += `</a>
<span class='username topic_status_e topic_status_`
tmpl += extra_data.Status
tmpl += ` hide_on_edit' style="font-weight:normal;float: right;">`
tmpl += extra_data.Status
tmpl += `</span>
w.Write([]byte(`>
<a class='topic_name hide_on_edit'>` + extra_data.Title + `</a>
<span class='username topic_status_e topic_status_` + extra_data.Status + ` hide_on_edit' style="font-weight:normal;float: right;">` + extra_data.Status + `</span>
<span class="username" style="border-right: 0;font-weight: normal;float: right;">Status</span>
`
`))
if tmpl_topic_vars.CurrentUser.Is_Mod {
tmpl += `
<a href='/topic/edit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username hide_on_edit open_edit" style="font-weight: normal;margin-left: 6px;">Edit</a>
<a href='/topic/delete/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username" style="font-weight: normal;">Delete</a>
`
w.Write([]byte(`
<a href='/topic/edit/` + strconv.Itoa(extra_data.ID) + `' class="username hide_on_edit open_edit" style="font-weight: normal;margin-left: 6px;">Edit</a>
<a href='/topic/delete/submit/` + strconv.Itoa(extra_data.ID) + `' class="username" style="font-weight: normal;">Delete</a>
`))
if extra_data.Sticky {
tmpl += `<a href='/topic/unstick/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username" style="font-weight: normal;">Unpin</a>`
w.Write([]byte(`<a href='/topic/unstick/submit/` + strconv.Itoa(extra_data.ID) + `' class="username" style="font-weight: normal;">Unpin</a>`))
} else {
tmpl += `<a href='/topic/stick/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username" style="font-weight: normal;">Pin</a>`
w.Write([]byte(`<a href='/topic/stick/submit/` + strconv.Itoa(extra_data.ID) + `' class="username" style="font-weight: normal;">Pin</a>`))
}
tmpl += `
w.Write([]byte(`
<input class='show_on_edit topic_name_input' name="topic_name" value='`
tmpl += extra_data.Title
tmpl += `' type="text" />
<input class='show_on_edit topic_name_input' name="topic_name" value='` + extra_data.Title + `' type="text" />
<select name="topic_status" class='show_on_edit topic_status_input' style='float: right;'>
<option>open</option>
<option>closed</option>
</select>
<button name="topic-button" class="formbutton show_on_edit submit_edit">Update</button>
`
`))
}
tmpl += `
<a href="/report/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `?session=`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `&type=topic" class="username report_item" style="font-weight: normal;">Report</a>
w.Write([]byte(`
<a href="/report/submit/` + strconv.Itoa(extra_data.ID) + `?session=` + tmpl_topic_vars.CurrentUser.Session + `&type=topic" class="username report_item" style="font-weight: normal;">Report</a>
</div>
</form>
</div>
<div class="rowblock">
<div class="rowitem passive editable_parent" style="border-bottom: none;`
<div class="rowitem passive editable_parent" style="border-bottom: none;`))
if extra_data.Avatar != "" {
tmpl += `background-image: url(`
tmpl += extra_data.Avatar
tmpl += `), url(/static/white-dot.jpg);background-position: 0px `
w.Write([]byte(`background-image: url(` + extra_data.Avatar + `), url(/static/white-dot.jpg);background-position: 0px `))
if extra_data.ContentLines <= 5 {
tmpl += `-1`
w.Write([]byte(`-1`))
}
tmpl += `0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;`
tmpl += string(extra_data.Css)
w.Write([]byte(`0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;` + string(extra_data.Css)))
}
tmpl += `">
<span class="hide_on_edit topic_content user_content">`
tmpl += string(tmpl_topic_varsstring(.Something.(TopicUser).Content))
tmpl += `</span>
<textarea name="topic_content" class="show_on_edit topic_content_input">`
tmpl += string(tmpl_topic_varsstring(.Something.(TopicUser).Content))
tmpl += `</textarea>
w.Write([]byte(`">
<span class="hide_on_edit topic_content user_content">` + string(extra_data.Content.(template.HTML)) + `</span>
<textarea name="topic_content" class="show_on_edit topic_content_input">` + string(extra_data.Content.(template.HTML)) + `</textarea>
<br /><br />
<a href="/user/`
tmpl += strconv.Itoa(extra_data.CreatedBy)
tmpl += `" class="username">`
tmpl += extra_data.CreatedByName
tmpl += `</a>
`
<a href="/user/` + strconv.Itoa(extra_data.CreatedBy) + `" class="username">` + extra_data.CreatedByName + `</a>
`))
if extra_data.Tag != "" {
tmpl += `<a class="username" style="float: right;">`
tmpl += extra_data.Tag
tmpl += `</a>`
w.Write([]byte(`<a class="username" style="float: right;">` + extra_data.Tag + `</a>`))
} else {
if extra_data.URLName != "" {
tmpl += `<a href="`
tmpl += extra_data.URL
tmpl += `" class="username" style="color: #505050;float: right;">`
tmpl += extra_data.URLName
tmpl += `</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">`
tmpl += extra_data.URLPrefix
tmpl += `</a>`
w.Write([]byte(`<a href="` + extra_data.URL + `" class="username" style="color: #505050;float: right;">` + extra_data.URLName + `</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">` + extra_data.URLPrefix + `</a>`))
}
}
tmpl += `
w.Write([]byte(`
</div>
</div><br />
<div class="rowblock" style="overflow: hidden;">
`
`))
if len(tmpl_topic_vars.ItemList) != 0 {
for _, item := range tmpl_topic_vars.ItemList {
tmpl += `
<div class="rowitem passive deletable_block editable_parent" style="`
w.Write([]byte(`
<div class="rowitem passive deletable_block editable_parent" style="`))
if item.(Reply).Avatar != "" {
tmpl += `background-image: url(`
tmpl += item.(Reply).Avatar
tmpl += `), url(/static/white-dot.jpg);background-position: 0px `
w.Write([]byte(`background-image: url(` + item.(Reply).Avatar + `), url(/static/white-dot.jpg);background-position: 0px `))
if item.(Reply).ContentLines <= 5 {
tmpl += `-1`
w.Write([]byte(`-1`))
}
tmpl += `0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;`
tmpl += string(item.(Reply).Css)
w.Write([]byte(`0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;` + string(item.(Reply).Css)))
}
tmpl += `">
<span class="editable_block user_content">`
tmpl += string(item.(Reply).ContentHtml)
tmpl += `</span>
w.Write([]byte(`">
<span class="editable_block user_content">` + string(item.(Reply).ContentHtml) + `</span>
<br /><br />
<a href="/user/`
tmpl += strconv.Itoa(item.(Reply).CreatedBy)
tmpl += `" class="username">`
tmpl += item.(Reply).CreatedByName
tmpl += `</a>
`
<a href="/user/` + strconv.Itoa(item.(Reply).CreatedBy) + `" class="username">` + item.(Reply).CreatedByName + `</a>
`))
if tmpl_topic_vars.CurrentUser.Is_Mod {
tmpl += `<a href="/reply/edit/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `"><button class="username edit_item">Edit</button></a>
<a href="/reply/delete/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `"><button class="username delete_item">Delete</button></a>`
w.Write([]byte(`<a href="/reply/edit/submit/` + strconv.Itoa(item.(Reply).ID) + `"><button class="username edit_item">Edit</button></a>
<a href="/reply/delete/submit/` + strconv.Itoa(item.(Reply).ID) + `"><button class="username delete_item">Delete</button></a>`))
}
tmpl += `
<a href="/report/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `?session=`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `&type=reply"><button class="username report_item">Report</button></a>
`
w.Write([]byte(`
<a href="/report/submit/` + strconv.Itoa(item.(Reply).ID) + `?session=` + tmpl_topic_vars.CurrentUser.Session + `&type=reply"><button class="username report_item">Report</button></a>
`))
if item.(Reply).Tag != "" {
tmpl += `<a class="username" style="float: right;">`
tmpl += item.(Reply).Tag
tmpl += `</a>`
w.Write([]byte(`<a class="username" style="float: right;">` + item.(Reply).Tag + `</a>`))
} else {
if item.(Reply).URLName != "" {
tmpl += `<a href="`
tmpl += item.(Reply).URL
tmpl += `" class="username" style="color: #505050;float: right;" rel="nofollow">`
tmpl += item.(Reply).URLName
tmpl += `</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">`
tmpl += item.(Reply).URLPrefix
tmpl += `</a>`
w.Write([]byte(`<a href="` + item.(Reply).URL + `" class="username" style="color: #505050;float: right;" rel="nofollow">` + item.(Reply).URLName + `</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">` + item.(Reply).URLPrefix + `</a>`))
}
}
tmpl += `
</div>`
w.Write([]byte(`
</div>`))
}
}
tmpl += `
w.Write([]byte(`
</div>
`
`))
if !tmpl_topic_vars.CurrentUser.Is_Banned {
tmpl += `
w.Write([]byte(`
<div class="rowblock">
<form action="/reply/create/" method="post">
<input name="tid" value='`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' type="hidden" />
<input name="tid" value='` + strconv.Itoa(extra_data.ID) + `' type="hidden" />
<div class="formrow">
<div class="formitem"><textarea name="reply-content" placeholder="Insert reply here"></textarea></div>
</div>
@ -249,13 +171,11 @@ tmpl += `' type="hidden" />
</div>
</form>
</div>
`
`))
}
tmpl += `
`
tmpl += ` <!--<link rel="stylesheet" href="https://use.fontawesome.com/8670aa03ca.css">-->
w.Write([]byte(`
<!--<link rel="stylesheet" href="https://use.fontawesome.com/8670aa03ca.css">-->
</div>
</body>
</html>`
return tmpl
</html>`))
}

262
template_topic_old.go Normal file
View File

@ -0,0 +1,262 @@
package main
import "strconv"
import "html/template"
//func init() {
//ctemplates["topic2"] = template_topic2
//}
func template_topic3(tmpl_topic_vars Page) (tmpl string) {
var extra_data TopicUser = tmpl_topic_vars.Something.(TopicUser)
tmpl += `<!doctype html>
<html lang="en">
<head>
<title>`
tmpl += tmpl_topic_vars.Title
tmpl += `</title>
<link href="/static/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
var session = "`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `";
</script>
<script type="text/javascript" src="/static/global.js"></script>
</head>
<body>
<div class="container">
`
tmpl += `<div class="nav">
<ul>
<li class="menu_overview"><a href="/">Overview</a></li>
<li class="menu_forums"><a href="/forums/">Forums</a></li>
<li class="menu_topics"><a href="/">Topics</a></li>
<li class="menu_create_topic"><a href="/topics/create/">Create Topic</a></li>
`
if tmpl_topic_vars.CurrentUser.Loggedin {
tmpl += `
<li class="menu_account"><a href="/user/edit/critical/">Account</a></li>
<li class="menu_account"><a href="/user/`
tmpl += strconv.Itoa(tmpl_topic_vars.CurrentUser.ID)
tmpl += `">Profile</a></li>
`
if tmpl_topic_vars.CurrentUser.Is_Super_Mod {
tmpl += `<li class="menu_account"><a href="/panel/forums/">Panel</a></li>`
}
tmpl += `
<li class="menu_logout"><a href="/accounts/logout?session=`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `">Logout</a></li>
`
} else {
tmpl += `
<li class="menu_register"><a href="/accounts/create/">Register</a></li>
<li class="menu_login"><a href="/accounts/login/">Login</a></li>
`
}
tmpl += `
</ul>
</div>`
tmpl += `
`
if len(tmpl_topic_vars.NoticeList) != 0 {
for _, item := range tmpl_topic_vars.NoticeList {
tmpl += `<div class="alert">`
tmpl += item
tmpl += `</div>`
}
}
tmpl += `
<div class="rowblock">
<form action='/topic/edit/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' method="post">
<div class="rowitem"`
if extra_data.Sticky {
tmpl += ` style="background-color: #FFFFEA;"`
}
tmpl += `>
<a class='topic_name hide_on_edit'>`
tmpl += extra_data.Title
tmpl += `</a>
<span class='username topic_status_e topic_status_`
tmpl += extra_data.Status
tmpl += ` hide_on_edit' style="font-weight:normal;float: right;">`
tmpl += extra_data.Status
tmpl += `</span>
<span class="username" style="border-right: 0;font-weight: normal;float: right;">Status</span>
`
if tmpl_topic_vars.CurrentUser.Is_Mod {
tmpl += `
<a href='/topic/edit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username hide_on_edit open_edit" style="font-weight: normal;margin-left: 6px;">Edit</a>
<a href='/topic/delete/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username" style="font-weight: normal;">Delete</a>
`
if extra_data.Sticky {
tmpl += `<a href='/topic/unstick/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username" style="font-weight: normal;">Unpin</a>`
} else {
tmpl += `<a href='/topic/stick/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' class="username" style="font-weight: normal;">Pin</a>`
}
tmpl += `
<input class='show_on_edit topic_name_input' name="topic_name" value='`
tmpl += extra_data.Title
tmpl += `' type="text" />
<select name="topic_status" class='show_on_edit topic_status_input' style='float: right;'>
<option>open</option>
<option>closed</option>
</select>
<button name="topic-button" class="formbutton show_on_edit submit_edit">Update</button>
`
}
tmpl += `
<a href="/report/submit/`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `?session=`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `&type=topic" class="username report_item" style="font-weight: normal;">Report</a>
</div>
</form>
</div>
<div class="rowblock">
<div class="rowitem passive editable_parent" style="border-bottom: none;`
if extra_data.Avatar != "" {
tmpl += `background-image: url(`
tmpl += extra_data.Avatar
tmpl += `), url(/static/white-dot.jpg);background-position: 0px `
if extra_data.ContentLines <= 5 {
tmpl += `-1`
}
tmpl += `0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;`
tmpl += string(extra_data.Css)
}
tmpl += `">
<span class="hide_on_edit topic_content user_content">`
tmpl += string(extra_data.Content.(template.HTML))
tmpl += `</span>
<textarea name="topic_content" class="show_on_edit topic_content_input">`
tmpl += string(extra_data.Content.(template.HTML))
tmpl += `</textarea>
<br /><br />
<a href="/user/`
tmpl += strconv.Itoa(extra_data.CreatedBy)
tmpl += `" class="username">`
tmpl += extra_data.CreatedByName
tmpl += `</a>
`
if extra_data.Tag != "" {
tmpl += `<a class="username" style="float: right;">`
tmpl += extra_data.Tag
tmpl += `</a>`
} else {
if extra_data.URLName != "" {
tmpl += `<a href="`
tmpl += extra_data.URL
tmpl += `" class="username" style="color: #505050;float: right;">`
tmpl += extra_data.URLName
tmpl += `</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">`
tmpl += extra_data.URLPrefix
tmpl += `</a>`
}
}
tmpl += `
</div>
</div><br />
<div class="rowblock" style="overflow: hidden;">
`
if len(tmpl_topic_vars.ItemList) != 0 {
for _, item := range tmpl_topic_vars.ItemList {
tmpl += `
<div class="rowitem passive deletable_block editable_parent" style="`
if item.(Reply).Avatar != "" {
tmpl += `background-image: url(`
tmpl += item.(Reply).Avatar
tmpl += `), url(/static/white-dot.jpg);background-position: 0px `
if item.(Reply).ContentLines <= 5 {
tmpl += `-1`
}
tmpl += `0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;`
tmpl += string(item.(Reply).Css)
}
tmpl += `">
<span class="editable_block user_content">`
tmpl += string(item.(Reply).ContentHtml)
tmpl += `</span>
<br /><br />
<a href="/user/`
tmpl += strconv.Itoa(item.(Reply).CreatedBy)
tmpl += `" class="username">`
tmpl += item.(Reply).CreatedByName
tmpl += `</a>
`
if tmpl_topic_vars.CurrentUser.Is_Mod {
tmpl += `<a href="/reply/edit/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `"><button class="username edit_item">Edit</button></a>
<a href="/reply/delete/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `"><button class="username delete_item">Delete</button></a>`
}
tmpl += `
<a href="/report/submit/`
tmpl += strconv.Itoa(item.(Reply).ID)
tmpl += `?session=`
tmpl += tmpl_topic_vars.CurrentUser.Session
tmpl += `&type=reply"><button class="username report_item">Report</button></a>
`
if item.(Reply).Tag != "" {
tmpl += `<a class="username" style="float: right;">`
tmpl += item.(Reply).Tag
tmpl += `</a>`
} else {
if item.(Reply).URLName != "" {
tmpl += `<a href="`
tmpl += item.(Reply).URL
tmpl += `" class="username" style="color: #505050;float: right;" rel="nofollow">`
tmpl += item.(Reply).URLName
tmpl += `</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">`
tmpl += item.(Reply).URLPrefix
tmpl += `</a>`
}
}
tmpl += `
</div>`
}
}
tmpl += `
</div>
`
if !tmpl_topic_vars.CurrentUser.Is_Banned {
tmpl += `
<div class="rowblock">
<form action="/reply/create/" method="post">
<input name="tid" value='`
tmpl += strconv.Itoa(extra_data.ID)
tmpl += `' type="hidden" />
<div class="formrow">
<div class="formitem"><textarea name="reply-content" placeholder="Insert reply here"></textarea></div>
</div>
<div class="formrow">
<div class="formitem"><button name="reply-button" class="formbutton">Create Reply</button></div>
</div>
</form>
</div>
`
}
tmpl += `
`
tmpl += ` <!--<link rel="stylesheet" href="https://use.fontawesome.com/8670aa03ca.css">-->
</div>
</body>
</html>`
return tmpl
}

262
template_topic_writer.go Normal file
View File

@ -0,0 +1,262 @@
package main
import "io"
import "strconv"
import "html/template"
/*func init() {
ctemplates["topic"] = template_topic
}*/
func template_topic2(tmpl_topic_vars Page, w io.Writer) {
var extra_data TopicUser = tmpl_topic_vars.Something.(TopicUser)
w.Write([]byte(`<!doctype html>
<html lang="en">
<head>
<title>`))
w.Write([]byte(tmpl_topic_vars.Title))
w.Write([]byte(`</title>
<link href="/static/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
var session = "`))
w.Write([]byte(tmpl_topic_vars.CurrentUser.Session))
w.Write([]byte(`";
</script>
<script type="text/javascript" src="/static/global.js"></script>
</head>
<body>
<div class="container">
`))
w.Write([]byte(`<div class="nav">
<ul>
<li class="menu_overview"><a href="/">Overview</a></li>
<li class="menu_forums"><a href="/forums/">Forums</a></li>
<li class="menu_topics"><a href="/">Topics</a></li>
<li class="menu_create_topic"><a href="/topics/create/">Create Topic</a></li>
`))
if tmpl_topic_vars.CurrentUser.Loggedin {
w.Write([]byte(`
<li class="menu_account"><a href="/user/edit/critical/">Account</a></li>
<li class="menu_account"><a href="/user/`))
w.Write([]byte(strconv.Itoa(tmpl_topic_vars.CurrentUser.ID)))
w.Write([]byte(`">Profile</a></li>
`))
if tmpl_topic_vars.CurrentUser.Is_Super_Mod {
w.Write([]byte(`<li class="menu_account"><a href="/panel/forums/">Panel</a></li>`))
}
w.Write([]byte(`
<li class="menu_logout"><a href="/accounts/logout?session=`))
w.Write([]byte(tmpl_topic_vars.CurrentUser.Session))
w.Write([]byte(`">Logout</a></li>
`))
} else {
w.Write([]byte(`
<li class="menu_register"><a href="/accounts/create/">Register</a></li>
<li class="menu_login"><a href="/accounts/login/">Login</a></li>
`))
}
w.Write([]byte(`
</ul>
</div>`))
w.Write([]byte(`
`))
if len(tmpl_topic_vars.NoticeList) != 0 {
for _, item := range tmpl_topic_vars.NoticeList {
w.Write([]byte(`<div class="alert">`))
w.Write([]byte(item))
w.Write([]byte(`</div>`))
}
}
w.Write([]byte(`
<div class="rowblock">
<form action='/topic/edit/submit/`))
w.Write([]byte(strconv.Itoa(extra_data.ID)))
w.Write([]byte(`' method="post">
<div class="rowitem"`))
if extra_data.Sticky {
w.Write([]byte(` style="background-color: #FFFFEA;"`))
}
w.Write([]byte(`>
<a class='topic_name hide_on_edit'>`))
w.Write([]byte(extra_data.Title))
w.Write([]byte(`</a>
<span class='username topic_status_e topic_status_`))
w.Write([]byte(extra_data.Status))
w.Write([]byte(` hide_on_edit' style="font-weight:normal;float: right;">`))
w.Write([]byte(extra_data.Status))
w.Write([]byte(`</span>
<span class="username" style="border-right: 0;font-weight: normal;float: right;">Status</span>
`))
if tmpl_topic_vars.CurrentUser.Is_Mod {
w.Write([]byte(`
<a href='/topic/edit/`))
w.Write([]byte(strconv.Itoa(extra_data.ID)))
w.Write([]byte(`' class="username hide_on_edit open_edit" style="font-weight: normal;margin-left: 6px;">Edit</a>
<a href='/topic/delete/submit/`))
w.Write([]byte(strconv.Itoa(extra_data.ID)))
w.Write([]byte(`' class="username" style="font-weight: normal;">Delete</a>
`))
if extra_data.Sticky {
w.Write([]byte(`<a href='/topic/unstick/submit/`))
w.Write([]byte(strconv.Itoa(extra_data.ID)))
w.Write([]byte(`' class="username" style="font-weight: normal;">Unpin</a>`))
} else {
w.Write([]byte(`<a href='/topic/stick/submit/`))
w.Write([]byte(strconv.Itoa(extra_data.ID)))
w.Write([]byte(`' class="username" style="font-weight: normal;">Pin</a>`))
}
w.Write([]byte(`
<input class='show_on_edit topic_name_input' name="topic_name" value='`))
w.Write([]byte(extra_data.Title))
w.Write([]byte(`' type="text" />
<select name="topic_status" class='show_on_edit topic_status_input' style='float: right;'>
<option>open</option>
<option>closed</option>
</select>
<button name="topic-button" class="formbutton show_on_edit submit_edit">Update</button>
`))
}
w.Write([]byte(`
<a href="/report/submit/`))
w.Write([]byte(strconv.Itoa(extra_data.ID)))
w.Write([]byte(`?session=`))
w.Write([]byte(tmpl_topic_vars.CurrentUser.Session))
w.Write([]byte(`&type=topic" class="username report_item" style="font-weight: normal;">Report</a>
</div>
</form>
</div>
<div class="rowblock">
<div class="rowitem passive editable_parent" style="border-bottom: none;`))
if extra_data.Avatar != "" {
w.Write([]byte(`background-image: url(`))
w.Write([]byte(extra_data.Avatar))
w.Write([]byte(`), url(/static/white-dot.jpg);background-position: 0px `))
if extra_data.ContentLines <= 5 {
w.Write([]byte(`-1`))
}
w.Write([]byte(`0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;`))
w.Write([]byte(string(extra_data.Css)))
}
w.Write([]byte(`">
<span class="hide_on_edit topic_content user_content">`))
w.Write([]byte(string(extra_data.Content.(template.HTML))))
w.Write([]byte(`</span>
<textarea name="topic_content" class="show_on_edit topic_content_input">`))
w.Write([]byte(string(extra_data.Content.(template.HTML))))
w.Write([]byte(`</textarea>
<br /><br />
<a href="/user/`))
w.Write([]byte(strconv.Itoa(extra_data.CreatedBy)))
w.Write([]byte(`" class="username">`))
w.Write([]byte(extra_data.CreatedByName))
w.Write([]byte(`</a>
`))
if extra_data.Tag != "" {
w.Write([]byte(`<a class="username" style="float: right;">`))
w.Write([]byte(extra_data.Tag))
w.Write([]byte(`</a>`))
} else {
if extra_data.URLName != "" {
w.Write([]byte(`<a href="`))
w.Write([]byte(extra_data.URL))
w.Write([]byte(`" class="username" style="color: #505050;float: right;">`))
w.Write([]byte(extra_data.URLName))
w.Write([]byte(`</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">`))
w.Write([]byte(extra_data.URLPrefix))
w.Write([]byte(`</a>`))
}
}
w.Write([]byte(`
</div>
</div><br />
<div class="rowblock" style="overflow: hidden;">
`))
if len(tmpl_topic_vars.ItemList) != 0 {
for _, item := range tmpl_topic_vars.ItemList {
w.Write([]byte(`
<div class="rowitem passive deletable_block editable_parent" style="`))
if item.(Reply).Avatar != "" {
w.Write([]byte(`background-image: url(`))
w.Write([]byte(item.(Reply).Avatar))
w.Write([]byte(`), url(/static/white-dot.jpg);background-position: 0px `))
if item.(Reply).ContentLines <= 5 {
w.Write([]byte(`-1`))
}
w.Write([]byte(`0px;background-repeat: no-repeat, repeat-y;background-size: 128px;padding-left: 136px;`))
w.Write([]byte(string(item.(Reply).Css)))
}
w.Write([]byte(`">
<span class="editable_block user_content">`))
w.Write([]byte(string(item.(Reply).ContentHtml)))
w.Write([]byte(`</span>
<br /><br />
<a href="/user/`))
w.Write([]byte(strconv.Itoa(item.(Reply).CreatedBy)))
w.Write([]byte(`" class="username">`))
w.Write([]byte(item.(Reply).CreatedByName))
w.Write([]byte(`</a>
`))
if tmpl_topic_vars.CurrentUser.Is_Mod {
w.Write([]byte(`<a href="/reply/edit/submit/`))
w.Write([]byte(strconv.Itoa(item.(Reply).ID)))
w.Write([]byte(`"><button class="username edit_item">Edit</button></a>
<a href="/reply/delete/submit/`))
w.Write([]byte(strconv.Itoa(item.(Reply).ID)))
w.Write([]byte(`"><button class="username delete_item">Delete</button></a>`))
}
w.Write([]byte(`
<a href="/report/submit/`))
w.Write([]byte(strconv.Itoa(item.(Reply).ID)))
w.Write([]byte(`?session=`))
w.Write([]byte(tmpl_topic_vars.CurrentUser.Session))
w.Write([]byte(`&type=reply"><button class="username report_item">Report</button></a>
`))
if item.(Reply).Tag != "" {
w.Write([]byte(`<a class="username" style="float: right;">`))
w.Write([]byte(item.(Reply).Tag))
w.Write([]byte(`</a>`))
} else {
if item.(Reply).URLName != "" {
w.Write([]byte(`<a href="`))
w.Write([]byte(item.(Reply).URL))
w.Write([]byte(`" class="username" style="color: #505050;float: right;" rel="nofollow">`))
w.Write([]byte(item.(Reply).URLName))
w.Write([]byte(`</a>
<a class="username" style="color: #505050;float: right;border-right: 0;">`))
w.Write([]byte(item.(Reply).URLPrefix))
w.Write([]byte(`</a>`))
}
}
w.Write([]byte(`
</div>`))
}
}
w.Write([]byte(`
</div>
`))
if !tmpl_topic_vars.CurrentUser.Is_Banned {
w.Write([]byte(`
<div class="rowblock">
<form action="/reply/create/" method="post">
<input name="tid" value='`))
w.Write([]byte(strconv.Itoa(extra_data.ID)))
w.Write([]byte(`' type="hidden" />
<div class="formrow">
<div class="formitem"><textarea name="reply-content" placeholder="Insert reply here"></textarea></div>
</div>
<div class="formrow">
<div class="formitem"><button name="reply-button" class="formbutton">Create Reply</button></div>
</div>
</form>
</div>
`))
}
w.Write([]byte(`
`))
w.Write([]byte(` <!--<link rel="stylesheet" href="https://use.fontawesome.com/8670aa03ca.css">-->
</div>
</body>
</html>`))
}

View File

@ -2,6 +2,7 @@ package main
import "log"
import "fmt"
import "strings"
import "strconv"
import "reflect"
import "path/filepath"
import "io/ioutil"
@ -29,6 +30,9 @@ type CTemplateSet struct
importMap map[string]string
varList map[string]VarItem
localVars map[string]map[string]VarItemReflect
stats map[string]int
pVarList string
pVarPosition int
//tempVars map[string]string
doImports bool
expectsInt interface{}
@ -48,8 +52,12 @@ func (c *CTemplateSet) compile_template(name string, dir string, expects string,
c.funcMap["lt"] = true
c.funcMap["ne"] = true
c.importMap = make(map[string]string)
c.importMap["io"] = "io"
c.importMap["strconv"] = "strconv"
c.varList = varList
//c.pVarList = ""
//c.pVarPosition = 0
c.stats = make(map[string]int)
c.expectsInt = expectsInt
holdreflect := reflect.ValueOf(expectsInt)
@ -108,7 +116,16 @@ func (c *CTemplateSet) compile_template(name string, dir string, expects string,
varString += "var " + varItem.Name + " " + varItem.Type + " = " + varItem.Destination + "\n"
}
out = "package main\n" + importList + "\nfunc init() {\nctemplates[\"" + fname + "\"] = template_" + fname + "\n}\n\nfunc template_" + fname + "(tmpl_" + fname + "_vars " + expects + ") (tmpl string) {\n" + varString + out + "return tmpl\n}\n"
out = "package main\n" + importList + c.pVarList + "\nfunc init() {\nctemplates[\"" + fname + "\"] = template_" + fname + "\n}\n\nfunc template_" + fname + "(tmpl_" + fname + "_vars " + expects + ", w io.Writer) {\n" + varString + out + "}\n"
out = strings.Replace(out,`))
w.Write([]byte(`," + ",-1)
out = strings.Replace(out,"` + `","",-1)
for index, count := range c.stats {
fmt.Println(index + ": " + strconv.Itoa(count))
}
if debug {
fmt.Println("Output!")
fmt.Println(out)
@ -210,7 +227,7 @@ func (c *CTemplateSet) compile_switch(varholder string, holdreflect reflect.Valu
}
return c.compile_subtemplate(varholder, holdreflect, node)
case *parse.TextNode:
return "tmpl += `" + string(node.Text) + "`\n"
return "w.Write([]byte(`" + string(node.Text) + "`))\n"
default:
panic("Unknown Node in main switch")
}
@ -246,8 +263,12 @@ func (c *CTemplateSet) compile_subswitch(varholder string, holdreflect reflect.V
cur = cur.FieldByName(id)
if cur.Kind() == reflect.Interface {
cur = cur.Elem()
if cur.Kind() == reflect.String && cur.Type().Name() != "string" {
varbit = "string(" + varbit + "." + id + ")"
/*if cur.Kind() == reflect.String && cur.Type().Name() != "string" {
varbit = "string(" + varbit + "." + id + ")"*/
//if cur.Kind() == reflect.String && cur.Type().Name() != "string" {
if cur.Type().PkgPath() != "main" {
c.importMap["html/template"] = "html/template"
varbit += "." + id + ".(" + strings.TrimPrefix(cur.Type().PkgPath(),"html/") + "." + cur.Type().Name() + ")"
} else {
varbit += "." + id + ".(" + cur.Type().Name() + ")"
}
@ -283,7 +304,7 @@ func (c *CTemplateSet) compile_subswitch(varholder string, holdreflect reflect.V
}
out, _ = c.compile_if_varsub(n.String(), varholder, template_name, holdreflect)
return "tmpl += " + out + "\n"
return "w.Write([]byte(" + out + "))\n"
case *parse.StringNode:
return n.Quoted
default:
@ -445,30 +466,6 @@ func (c *CTemplateSet) compile_reflectswitch(varholder string, holdreflect refle
fmt.Println(node.Args)
}
return "", outVal
/*case *parse.IdentifierNode:
fmt.Println("Identifier Node: ")
fmt.Println(node)
fmt.Println(node.Args)
ArgLoop:
for pos, id := range node.Args {
fmt.Println(id)
switch id.String() {
case "not":
out += "!"
case "or":
out += " || "
case "and":
out += " && "
case "le":
out += c.compile_if_varsub_n(node.Args[pos + 1].String(), varholder, holdreflect) + " <= " + c.compile_if_varsub_n(node.Args[pos + 2].String(), varholder, holdreflect)
break ArgLoop
default:
fmt.Println("Variable!")
out += c.compile_if_varsub_n(id.String(), varholder, holdreflect)
}
}
return out*/
case *parse.DotNode:
return varholder, holdreflect
case *parse.NilNode:
@ -551,6 +548,14 @@ func (c *CTemplateSet) compile_if_varsub(varname string, varholder string, templ
out = strings.Replace(out, varItem.Destination, varItem.Name, 1)
}
}
_, ok := c.stats[out]
if ok {
c.stats[out]++
} else {
c.stats[out] = 1
}
return out, cur
}
@ -578,23 +583,30 @@ func (c *CTemplateSet) compile_varsub(varname string, val reflect.Value) string
}
}
_, ok := c.stats[varname]
if ok {
c.stats[varname]++
} else {
c.stats[varname] = 1
}
if val.Kind() == reflect.Interface {
val = val.Elem()
}
switch val.Kind() {
case reflect.Int:
return "tmpl += strconv.Itoa(" + varname + ")\n"
return "w.Write([]byte(strconv.Itoa(" + varname + ")))\n"
case reflect.Bool:
return "if " + varname + " {\ntmpl += \"true\"} else {\ntmpl += \"false\"\n}\n"
return "if " + varname + " {\nw.Write([]byte(\"true\"))} else {\nw.Write([]byte(\"false\"))\n}\n"
case reflect.String:
if val.Type().Name() != "string" {
return "tmpl += string(" + varname + ")\n"
if val.Type().Name() != "string" && !strings.HasPrefix(varname,"string(") {
return "w.Write([]byte(string(" + varname + ")))\n"
} else {
return "tmpl += " + varname + "\n"
return "w.Write([]byte(" + varname + "))\n"
}
case reflect.Int64:
return "tmpl += strconv.FormatInt(" + varname + ", 10)"
return "w.Write([]byte(strconv.FormatInt(" + varname + ", 10)))"
default:
fmt.Println("Unknown Kind: ")
fmt.Println(val.Kind())