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

Struct field names in templates / contexts #319

Open
ivoras opened this issue Dec 27, 2022 · 0 comments · May be fixed by #329
Open

Struct field names in templates / contexts #319

ivoras opened this issue Dec 27, 2022 · 0 comments · May be fixed by #329

Comments

@ivoras
Copy link

ivoras commented Dec 27, 2022

Assuming this struct:

type S struct {
  Name string
  Number int
}

The way to use an instance of the structure is to add it to a Context:

ctx := Context{
  "mystruct": S
}

And the way to use it in a template is to access the struct members using the Go convention: variables starting with an uppercase letter are exported:

Hello {{ mystruct.Name }}. This is number {{ mystruct.Number }}.

Which works ok, but can be a bit ugly when switching from lowercase names to uppercase. Why not introduce a Go struct tag which renames fields for use in Pongo2? Something like this:

type S struct {
  Name string `pongo2:"name"`
  Number int  `pongo2:"a_number"`
}

This would allow more flexibility and consistency in templates by writing:

Hello {{ mystruct.name }}. This is number {{ mystruct.a_number }}.
@boatrainlsz boatrainlsz linked a pull request Feb 2, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants