lets-go:5.1 templates
This commit is contained in:
parent
569f2fc0a9
commit
65c86f86d2
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
//"html/template"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@ -63,7 +63,26 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%+v", snippet)
|
||||
files := []string{
|
||||
"./ui/html/base.tmpl",
|
||||
"./ui/html/partials/nav.tmpl",
|
||||
"./ui/html/pages/view.tmpl",
|
||||
}
|
||||
|
||||
ts, err := template.ParseFiles(files...)
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
data := templateData{
|
||||
Snippet: snippet,
|
||||
}
|
||||
|
||||
err = ts.ExecuteTemplate(w, "base", data)
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
// snippetCreate ...
|
||||
|
9
snippetbox/cmd/web/templates.go
Normal file
9
snippetbox/cmd/web/templates.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"snippetbox.chaosfem.tw/internal/models"
|
||||
)
|
||||
|
||||
type templateData struct {
|
||||
Snippet models.Snippet
|
||||
}
|
16
snippetbox/ui/html/pages/view.tmpl
Normal file
16
snippetbox/ui/html/pages/view.tmpl
Normal file
@ -0,0 +1,16 @@
|
||||
{{define "title"}}Snippet #{{.Snippet.ID}}{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
<div class="snippet">
|
||||
<div class="metadata">
|
||||
<strong>{{.Snippet.Title}}</strong>
|
||||
<span>#{{.Snippet.ID}}</span>
|
||||
</div>
|
||||
|
||||
<pre><code>{{.Snippet.Content}}</code></pre>
|
||||
<div class="metadata">
|
||||
<time>Created: {{.Snippet.Created}}</time>
|
||||
<time>Expires: {{.Snippet.Expires}}</time>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
Loading…
Reference in New Issue
Block a user