ponzu-cms / ponzu

Headless CMS with automatic JSON API. Featuring auto-HTTPS from Let's Encrypt, HTTP/2 Server Push, and flexible server framework written in Go.
https://docs.ponzu-cms.org
BSD 3-Clause "New" or "Revised" License
5.7k stars 384 forks source link

Resolve references in string() method #224

Closed 4e6963 closed 6 years ago

4e6963 commented 6 years ago

I tried to show parent content's data in string(). But it doesn't resolve the URL

func (d *Document) String() string { return d.Title + " from " + d.Company } Shows this:

Document1 from /api/content?type=Company&id=1

Is there a way to do this?

nilslice commented 6 years ago

Yes, but currently you need to do a bit of work. Either use the content API to get the resource via HTTP, or parse the URI to get the type and ID and get the value directly from the database using the system/DB package.

I plan to provide a method in the db package to resolve the reference URI more easily, but am short on time recently.

4e6963 commented 6 years ago

Thank you for the fast response.

This solution works for me. I hope I will find time in the next weeks, to do a pull request with a general solution.

// No error handling
func (d *Document) String() string {
    companyQuery := d.Company
    url, _ := url.Parse(companyQuery)
    urlQuery := url.Query()
    query := urlQuery.Get("type") + ":" + urlQuery.Get("id")
    bytes, _ := db.Content(query)
    company := Company{}
    json.Unmarshal(bytes, &company)
    return d.Title + " from " + company.Name
}
nilslice commented 6 years ago

Great! I'll look forward to it. If I get a chance to start, I'll be sure to ping you here so we don't overlap work. Thanks :)