Browse Source

Front-end templates

Vova Tkach 6 years ago
parent
commit
82a87b0259

+ 50 - 1
frontend.go

@@ -1,9 +1,58 @@
 package main
 
 import (
+	// "log"
+	"html/template"
+
 	"golang-fave/engine/wrapper"
 )
 
+type TmplMenuItem struct {
+	Name string
+	Link string
+}
+
+type TmplData struct {
+	PathSysIcoFav       string
+	PathSysCssBootstrap string
+	PathSysJsJquery     string
+	PathSysJsPopper     string
+	PathSysJsBootstrap  string
+
+	MetaTitle       string
+	MetaKeywords    string
+	MetaDescription string
+	MenuItems       []TmplMenuItem
+	SomeHtml        template.HTML
+}
+
 func handleFrontEnd(e *wrapper.Wrapper) bool {
-	return false
+	tmpl, err := template.ParseFiles(
+		e.DirVhostHome+"/template"+"/index.html",
+		e.DirVhostHome+"/template"+"/header.html",
+		e.DirVhostHome+"/template"+"/footer.html",
+	)
+	if err != nil {
+		// log.Printf(err.Error())
+		return false
+	}
+
+	tmpl.Execute(*e.W, TmplData{
+		PathSysIcoFav:       e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/fave.ico",
+		PathSysCssBootstrap: e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.css",
+		PathSysJsJquery:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/jquery.js",
+		PathSysJsPopper:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/popper.js",
+		PathSysJsBootstrap:  e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.js",
+
+		MetaTitle:       "Meta Title",
+		MetaKeywords:    "Meta Keywords",
+		MetaDescription: "Meta Description",
+
+		MenuItems: []TmplMenuItem{
+			{Name: "Menu Item 1", Link: "/#1"},
+			{Name: "Menu Item 2", Link: "/#2"},
+		},
+		SomeHtml: template.HTML("<div class=\"some-class\">DIV</div>"),
+	})
+	return true
 }

+ 7 - 0
hosts/localhost/template/footer.html

@@ -0,0 +1,7 @@
+		<!-- Optional JavaScript -->
+		<!-- jQuery first, then Popper.js, then Bootstrap JS -->
+		<script src="{{$.PathSysJsJquery}}"></script>
+		<script src="{{$.PathSysJsPopper}}"></script>
+		<script src="{{$.PathSysJsBootstrap}}"></script>
+	</body>
+</html>

+ 19 - 0
hosts/localhost/template/header.html

@@ -0,0 +1,19 @@
+<!doctype html>
+<html lang="en">
+	<head>
+		<!-- Required meta tags -->
+		<meta charset="utf-8">
+		<meta name="theme-color" content="#205081" />
+		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
+
+		<!-- Bootstrap CSS -->
+		<link rel="stylesheet" href="{{$.PathSysCssBootstrap}}">
+
+		<title>{{$.MetaTitle}}</title>
+
+		<meta name="keywords" content="{{$.MetaKeywords}}" />
+		<meta name="description" content="{{$.MetaDescription}}" />
+
+		<link rel="shortcut icon" href="{{$.PathSysIcoFav}}" type="image/x-icon" />
+	</head>
+	<body>

+ 22 - 15
hosts/localhost/template/index.html

@@ -1,15 +1,22 @@
-<!DOCTYPE html>
-<html lang="en">
-	<head>
-		<meta charset="utf-8" />
-		<meta name="theme-color" content="#205081" />
-		<title>Index template</title>
-		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
-		<meta name="viewport" content="width=device-width, initial-scale=0.8, maximum-scale=0.8" />
-		<link rel="shortcut icon" href="/assets/sys/fave.ico" type="image/x-icon" />
-		<link rel="stylesheet" type="text/css" media="all" href="/assets/sys/styles.css" />
-	</head>
-	<body>
-		Index template
-	</body>
-</html>
+{{template "header.html" .}}
+		<div class="container">
+			<h1>Hello, world!</h1>
+			<h2>{{$.MetaTitle}}</h2>
+			<div>
+				Index template<br />
+				{{$.PathSysIcoFav}}
+			</div>
+			<div>
+				<ul>
+					{{range $.MenuItems}}
+						<li>
+							<a href="{{.Link}}">{{.Name}} - {{$.MetaTitle}}</a>
+						</li>
+					{{end}}
+				</ul>
+			</div>
+			<div class="html-test">
+				{{$.SomeHtml}}
+			</div>
+		</div>
+{{template "footer.html" .}}