Skip to content

Struct field names in templates / contexts #319

Description

@ivoras

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 }}.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions