Browse Source

Display template parsing error

Vova Tkach 6 years ago
parent
commit
81e49d95e7

+ 25 - 0
engine/wrapper/resources/templates/page.tmplerror.go

@@ -0,0 +1,25 @@
+package templates
+
+var PageTmplError = []byte(`<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<meta charset="utf-8" />
+		<meta name="theme-color" content="#205081" />
+		<title>Template Error</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>
+		<div class="wrapper">
+			<div class="logo">
+				<div class="svg">
+					<img src="/assets/sys/logo.svg" width="150" height="150" />
+				</div>
+			</div>
+			<h1>Template Error</h1>
+			<h2>%s</h2>
+		</div>
+	</body>
+</html>`)

+ 7 - 0
engine/wrapper/static.go

@@ -1,6 +1,7 @@
 package wrapper
 
 import (
+	"fmt"
 	"net/http"
 	"os"
 
@@ -105,3 +106,9 @@ func (e *Wrapper) printPage404() {
 	(*e.W).Header().Set("Content-Type", "text/html")
 	(*e.W).Write(Templates.PageError404)
 }
+
+func (e *Wrapper) PrintTmplPageError(err error) {
+	(*e.W).WriteHeader(http.StatusInternalServerError)
+	(*e.W).Header().Set("Content-Type", "text/html")
+	(*e.W).Write([]byte(fmt.Sprintf(string(Templates.PageTmplError), err.Error())))
+}

+ 2 - 3
frontend.go

@@ -1,7 +1,6 @@
 package main
 
 import (
-	// "log"
 	"html/template"
 
 	"golang-fave/engine/wrapper"
@@ -33,8 +32,8 @@ func handleFrontEnd(e *wrapper.Wrapper) bool {
 		e.DirVhostHome+"/template"+"/footer.html",
 	)
 	if err != nil {
-		// log.Printf(err.Error())
-		return false
+		e.PrintTmplPageError(err)
+		return true
 	}
 
 	tmpl.Execute(*e.W, TmplData{

+ 20 - 20
hosts/localhost/template/index.html

@@ -1,22 +1,22 @@
 {{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>
+<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" .}}