Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serving parts of a page / creating an html document #13

Closed
theirf opened this issue Apr 27, 2014 · 1 comment
Closed

serving parts of a page / creating an html document #13

theirf opened this issue Apr 27, 2014 · 1 comment

Comments

@theirf
Copy link

theirf commented Apr 27, 2014

Is it possible to serve parts of html and build a page using goji?

For example if I wanted to serve the of results.html, write , and then in a for loop write contents of the body of that page, and then . Or will I have to use standard Golang features like:

func viewHandler(w http.ResponseWriter, r *http.Request) {
    title := r.URL.Path[len("/view/"):]
    p, _ := loadPage(title)
    fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
@elithrar
Copy link
Contributor

@theirf Use Go's html/template package — an example of which is further down the page from where you copied that code ;)

i.e.

// Shorthand
type M map[string]interface{}

func viewHandler(c web.C, w http.ResponseWriter, r *http.Request) {
    title := c.URLParams["title"]
    p, err := loadPage(title)
    if err != nil {
        ...
    }
    // do other things

    template.ExecuteTemplate(w, "results.html", M{
            "title": title,
            "results": results,
            "pagination": true,
         }
    }

results.html

    {{range .Results }}
       <h1>{{ Result.Name }}</h1>
       <p>{{ Result.Body }}</p>
     {{ end }}

@zenazn zenazn closed this as completed Apr 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants