Elanat continues its commitment to supporting WebForms Core technology across all programming languages. WebForms Core is now available to everyone on the GO programming language. Elanat has updated the WebForms class for GO with the latest version of WebFormsjs, 1.6. The WebForms class on the server and the WebFormsJS library on the client constitute the WebForms Core technology.
WebForms Core technology is a revolution in the web that will change the way you think about web development. It can handle web elements from the server in both online and offline modes.
A few things you should know about WebForms Core:
- WebForms Core does not require any other JavaScript libraries to run.
- Form handling is completely faithful to HTML and sends data via the XmlHttpRequest object, just like HTML sends data.
- It can process both SSR and CSR and can handle tags, giving you the freedom to combine the best of both worlds.
The following video shows the functionality of WebForms Core technology in submitting form data and server response.
Download Links
You can download the WebForms class for the Go programming language from the link below.
To download the latest version of WebFormsJS, visit the following link.
Example: Using WebForms Core in GO
This example shows how to create a simple web page using WebForms Core in GO that can change the theme from day to night and from night to day, and also dynamically adjust the font size.
The following view page contains 4 buttons: one to increase the font size, one to decrease the font size, one to set the font size to the default size, and one to change the theme from day to night and vice versa. The following view works completely offline. Here, by calling cache in the WebForms class, the data is requested from the server only once and is cached for subsequent times and requested from the browser cache. Please note that in this technology, by using the StartIndex method and setting the cache-time in the server response header, we can request the server response only once instead of 4 times (for more information, you can read the "Multiple responses in action controls" section in this link)
Note: In WebForms Core technology, the state in which the client does not need the server data to perform an event is called offline mode.
package main
import (
"fmt"
"my-web-app/webforms"
"net/http"
)
func main() {
http.Handle("/script/", http.StripPrefix("/script/", http.FileServer(http.Dir("script"))))
http.HandleFunc("/", handleForm)
http.ListenAndServe(":8080", nil)
}
func handleForm(w http.ResponseWriter, r *http.Request) {
form := new(webforms.WebForms)
event := new(webforms.HtmlEvent)
if r.Method == http.MethodGet {
index := r.URL.Query().Get("index")
theme := r.URL.Query().Get("theme")
if index != "" {
if index == "i" {
form.IncreaseFontSize("<body>", 1)
}
if index == "d" {
form.DecreaseFontSize("<body>", 1)
}
if index == "t" {
form.SetFontSizeInt("<body>", 16)
}
form.SetCacheNoTime()
fmt.Fprint(w, form.Response())
return
}
if theme != "" {
if theme == "day" {
form.DeleteAttribute("btn_Theme", event.OnClick())
form.SetGetEvent("btn_Theme", event.OnClick(), "?theme=night")
form.SetText("btn_Theme", "Night")
form.SetTextColor("<body>", "black")
form.SetBackgroundColor("<body>", "white")
}
if theme == "night" {
form.DeleteAttribute("btn_Theme", event.OnClick())
form.SetGetEvent("btn_Theme", event.OnClick(), "?theme=day")
form.SetText("btn_Theme", "Day")
form.SetTextColor("<body>", "white")
form.SetBackgroundColor("<body>", "black")
}
form.SetCacheNoTime()
fmt.Fprint(w, form.Response())
return
}
// If 'index' query parameter is not present, set up the events
form.SetFontSizeInt("<body>", 16)
form.SetGetEvent("btn_Increase", event.OnClick(), "?index=i")
form.SetGetEvent("btn_Decrease", event.OnClick(), "?index=d")
form.SetGetEvent("btn_Default", event.OnClick(), "?index=t")
form.SetGetEvent("btn_Theme", event.OnClick(), "?theme=night")
fmt.Fprint(w, `<!DOCTYPE html>
<html>
<head>
<title>Using WebForms Core</title>
<script type="text/javascript" src="/script/web-forms.js"></script>
</head>
<body>
<button id="btn_Theme">Night</button>
<button id="btn_Increase">+</button> <button id="btn_Default">Default</button> <button id="btn_Decrease">-</button>
<h1>WebForms Core Technology in Go</h1>
<p><b>CodeBehind Framework</b> is a modern full-stack framework developed by Elanat in 2023...</p>
<p><b>Elanat CMS</b> is a content management system also developed by Elanat...</p>
<p><b>WebForms Core</b> is a revolutionary technology for modifying HTML tags from the server...</p>
<br>
`)
// Export the WebForms tags
fmt.Fprint(w, form.ExportToWebFormsTag(""))
fmt.Fprint(w, `
</body>
</html>`)
return
}
// Handle other methods if needed
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
The GIF image below shows how the above code works.
Initialization
In this example, the webforms.go class is imported at the beginning of the code. This class is stored in a directory named "webforms" in the project path. Then a new instance of the WebForms class and the HtmlEvent helper class are created.
import (
"fmt"
"my-web-app/webforms"
"net/http"
)
...
form := new(webforms.WebForms)
event := new(webforms.HtmlEvent)
In the code below, the font size of the body tag is first set to 16, and then the click event of the font size change button and the theme change button are set to a query string.
...
form.SetFontSizeInt("<body>", 16)
form.SetGetEvent("btn_Increase", event.OnClick(), "?index=i")
form.SetGetEvent("btn_Decrease", event.OnClick(), "?index=d")
form.SetGetEvent("btn_Default", event.OnClick(), "?index=t")
form.SetGetEvent("btn_Theme", event.OnClick(), "?theme=night")
...
fmt.Fprint(w, form.ExportToWebFormsTag(""))
...
Change font size
To apply the font size change, we check the queries we added earlier to the click event and then call the SetCacheNoTime
method to cache the response. Finally, by calling the Response
method, we send the action control response to the client.
if index != "" {
if index == "i" {
form.IncreaseFontSize("<body>", 1)
}
if index == "d" {
form.DecreaseFontSize("<body>", 1)
}
if index == "t" {
form.SetFontSizeInt("<body>", 16)
}
form.SetCacheNoTime()
fmt.Fprint(w, form.Response())
return
}
Change theme
To apply the theme change, we also check the button click event query for the theme. If the query value is equal to day, the query for the button click event sets the theme to night and whitens the page theme; if the query value is equal to night, the query for the button click event sets the theme to day and blackens the page theme. In this section, the SetCacheNoTime
method is also called. Finally, by calling the Response
method, the action control response is sent to the client.
Note: Using the
SetCacheNoTime
method causes the data to be requested from the server only once. You can also use theSetCache
method and specify the cache duration in seconds.
if theme != "" {
if theme == "day" {
form.DeleteAttribute("btn_Theme", event.OnClick())
form.SetGetEvent("btn_Theme", event.OnClick(), "?theme=night")
form.SetText("btn_Theme", "Night")
form.SetTextColor("<body>", "black")
form.SetBackgroundColor("<body>", "white")
}
if theme == "night" {
form.DeleteAttribute("btn_Theme", event.OnClick())
form.SetGetEvent("btn_Theme", event.OnClick(), "?theme=day")
form.SetText("btn_Theme", "Day")
form.SetTextColor("<body>", "white")
form.SetBackgroundColor("<body>", "black")
}
form.SetCacheNoTime()
fmt.Fprint(w, form.Response())
Note: Please note that when the HTML page is requested for the first time, if you are using the WebForms class, use the
ExportToWebFormsTag
method. For secondary requests, use theResponse
method.
You can get the sample code in link below.
http://elanat.net/content/32/WebForms Class in GO Update to WebFormsJS 1.6.html
Conclusion
Integrating WebForms Core into your Go projects, offers a powerful way to manage web elements directly from your backend codebase without relying on extensive front-end libraries. By leveraging this technology, developers can simplify their workflow while maintaining full control over dynamic content updates across various platforms.
Top comments (0)