Adding new pages - let's add search page
Author | Topic |
---|---|
mysh
|
Posted on
Before we start adding anything to the frontend side of things we need to create a migration. Adding and registering the new routeLet's add In <a href="/search">search</a> To register a new route, we need to edit // Search publicSubRouter.HandleFunc("/search", nil).Methods(http.MethodGet) Creating and displaying the templateLet's create // /web/handler.search.go package handler import ( "net/http" ) func (h *Handler) searchShow(w http.ResponseWriter, r *http.Request) { query := r.URL.Query().Get("q") thing, _ := h.storage.Search(query) v := NewView(w, r, "search") // We don't have that yet! v.Set("sql", thing) v.Set("q", query) v.Render() } And let's create a template for it - We need to add 2 sections: - - --- {{ define "title" }} Search {{ end }} {{ define "content" }} <h2>Search</h2> {{ if .q }} <p>{{ len .sql }} results found.</p> {{ else }} <p>Enter your search query down below.</p> {{ end }} <form action="/search" method="get" class="search-form"> <label for="q" style="display: none">Search</label> <input type="text" value="{{ .q }}" name="q" id="q" placeholder="..."> <button type="submit">search</button> </form> {{ if .sql }} {{ range $index, $element := .sql }} <div class="search-result"> <span>[{{ $element.OriginTable }}]</span> <a href="/{{ $element.OriginTable }}/{{ $element.ID }}">{{ $element.HighlightedTitle }}</a> {{ if ne $element.HighlightedContent "" }} <div class="content"> {{ $element.HighlightedContent }}... </div> {{ end }} </div> {{ end }} {{ end }} {{ end }} And done! Run Last edited on |