Installation
Start with a blank Go project and install magic with the following command.go get github.com/Instantan/magic
Installation
Start with a blank Go project and install magic with the following command.go get github.com/Instantan/magic
Web server
In the main.go file, create a simple web server. The only magic specific thing here is the magic.CompressedComponentHTTPHandler(index) wich takes a component and returns a http.Handler. You can use any std-library compatible router/server/mux you want.Note that the following code wont compile on it's own.
main.gopackage main
import (
"log"
"net/http"
"github.com/Instantan/magic"
)
func main() {
magic.HotReload() // include this line if you want to hot reload your application on save
mux := http.NewServeMux()
mux.Handle("/", magic.CompressedComponentHTTPHandler(index))
log.Print("Listening to http://localhost:8070")
if err := http.ListenAndServe(":8070", mux); err != nil {
log.Fatal(err)
}
}
First view
Create a new file index.go. In that file you will create the first view. Views in magic are simple http templates. The templates are like mustache templates logicless. It's recommended that the views are created on the top level, because the view creation is somewhat expensive.Note that the following code wont compile on it's own.
index.gopackage main
import (
"github.com/Instantan/magic"
)
var indexView = magic.View(`
<!DOCTYPE html>
<html>
<head>
<title>Learn</title>
</head>
<body>
{{ body }}
</body>
</html>
`)
Simple component
To render a view you first need to create a component. The first argument of the component takes always a magic.Socket. The second argument is a generic one. It can take any type you want and is used to pass down data to the component. This example uses the type magic.Empty wich is just a empty struct.index.gopackage main
import (
"github.com/Instantan/magic"
)
var indexView = magic.View(`
<!DOCTYPE html>
<html>
<head>
<title>Learn</title>
</head>
<body>
{{ body }}
</body>
</html>
`)
var index = magic.Component(func(s magic.Socket, _ magic.Empty) magic.AppliedView {
magic.Assign(s, "body", "Hello World")
return indexView(s)
})
Make it live
So far there is nothing much magical about this. You can statically render websites based on composeable components, but thats it.index.gopackage main
import (
"github.com/Instantan/magic"
)
var indexView = magic.View(`
<!DOCTYPE html>
<html>
<head>
<title>Learn</title>
</head>
<body>
{{ body }}
</body>
</html>
`)
var index = magic.Component(func(s magic.Socket, _ magic.Empty) magic.AppliedView {
magic.Assign(s, "body", "Hello World")
if s.Live() {
t := time.NewTicker(time.Second)
go func() {
for r := range t.C {
magic.Assign(s, "body", r.String())
}
}()
s.HandleEvent(func(e string, d magic.EventData) {
switch e {
case magic.UnmountEvent:
t.Stop()
}
})
}
return indexView(s)
})
Events
Send events like onclick from the client to the server and handle it in your component.Static nodes
Sometimes you dont want to live render some nodes. For example when you manipulate divs with JavaScript. If thats the case you can use the HTML Attribute magic:static="a". Only when the value equals the live rendered value the node gets ignored.JavaScript
-Hooks
-Deferred assigns
-Deferred components
-Temporal data patches
-Handlers
-