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.67k stars 387 forks source link

how to: reference referenced field in fmt.sprintf #316

Closed CharlesLabri closed 4 years ago

CharlesLabri commented 4 years ago

Trying to reference the title of a referenced select in the sprintf title. All im getting is the API URL.

example:

func (m *MaintenanceLog) MarshalEditor() ([]byte, error) {
    view, err := editor.Form(m,
        editor.Field{
            View: reference.Select("book", m, map[string]string{
                "label": "book",
            },
                "book",
                `{{ .nickname }} `,
            ),
        },
)
func (m *MaintenanceLog) String() string {
    return fmt.Sprintf("book_nickname: %s", m.book)
}

string i get is: "/api/content?type=book&id=1"

How can i reference the {{.nickname}} in the sprintf?

nilslice commented 4 years ago

Hi @CharlesLabri -

Assuming Nickname is a field of the book, you would need to do a follow-on GET request to the content API path found in m.Book.

you can do this with your own HTTP client and construct the GET request, or you can use some helpers in the go-client for ponzu. You'd call ParseReferenceURI using the m.Book value, and then make the call to the content API using the Content method. It might be overkill to pull in this additional module, but it might simplify things for you.

nilslice commented 4 years ago

The functionality in the `{{ .nickname }}, call within the MarshalEditor method does this behind the scenes for you, within the CMS code -- it is a bit of hidden magic, so I apologize if it threw you off a bit!

CharlesLabri commented 4 years ago

thanks!