Added templating

This commit is contained in:
Gregory Ballantine 2022-12-07 21:24:11 -05:00
parent a7da12d028
commit 6ebf79e0e0
7 changed files with 74 additions and 4 deletions

17
app/web/routes.go Normal file
View File

@ -0,0 +1,17 @@
package web
import (
"net/http"
"github.com/flamego/flamego"
"github.com/flamego/template"
)
func RegisterRoutes(f *flamego.Flame) {
// index route - landing page for the user
f.Get("/", func(t template.Template, data template.Data) {
t.HTML(http.StatusOK, "index")
})
// item list route - lists all of the items in the database
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/alecthomas/participle/v2 v2.0.0-beta.5 // indirect github.com/alecthomas/participle/v2 v2.0.0-beta.5 // indirect
github.com/fatih/color v1.13.0 // indirect github.com/fatih/color v1.13.0 // indirect
github.com/flamego/flamego v1.7.0 // indirect github.com/flamego/flamego v1.7.0 // indirect
github.com/flamego/template v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect

2
go.sum
View File

@ -4,6 +4,8 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/flamego/flamego v1.7.0 h1:c1Lu16PBAZKkpsjHw42vwotdoQnMMpUi60ITP41W12w= github.com/flamego/flamego v1.7.0 h1:c1Lu16PBAZKkpsjHw42vwotdoQnMMpUi60ITP41W12w=
github.com/flamego/flamego v1.7.0/go.mod h1:dnVMBJyHKaxjcqRVN93taSK+YB/9p+Op1GdLIuA1hFQ= github.com/flamego/flamego v1.7.0/go.mod h1:dnVMBJyHKaxjcqRVN93taSK+YB/9p+Op1GdLIuA1hFQ=
github.com/flamego/template v1.1.0 h1:iYtCzY3TeYpsoQiGApFXw2qycKdMzimz2gkO/SlcksM=
github.com/flamego/template v1.1.0/go.mod h1:bgnmEXNumarhQIUzFgn18CDG6u8cM6X09c7UOTwZcxM=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=

View File

@ -2,12 +2,26 @@ package main
import ( import (
"github.com/flamego/flamego" "github.com/flamego/flamego"
"github.com/flamego/template"
"git.metaunix.net/metaunix/raven/app/web"
) )
func main() { func main() {
f := flamego.Classic() f := flamego.New()
f.Get("/", func() string {
return "Hello, Flamego!" // Initialize template engine
}) f.Use(template.Templater(template.Options{
Directory: "views",
}))
f.Use(flamego.Static(
flamego.StaticOptions{
Directory: "public",
},
))
web.RegisterRoutes(f)
f.Run() f.Run()
} }

7
views/index.html Normal file
View File

@ -0,0 +1,7 @@
{{ template "layout" . }}
{{ define "title" }}Dashboard{{ end }}
{{ define "content" }}
<p>This is a test.</p>
{{ end }}

16
views/layout/layout.html Normal file
View File

@ -0,0 +1,16 @@
{{ define "layout" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ block "title" . }}{{ end }} | Raven</title>
</head>
<body>
{{ template "navbar" . }}
{{ block "content" . }}{{ end }}
</body>
</html>
{{ end }}

13
views/layout/navbar.html Normal file
View File

@ -0,0 +1,13 @@
{{ define "navbar" }}
<nav id="main-nav">
<h3>Raven</h3>
<ul>
<li><a href="/">Dashboard</a></li>
<li><a href="/item/list">Items</a></li>
<li><a href="/license/list">Licenses</a></li>
</ul>
</nav>
{{ end }}