Skip to content

Implement 'tojson' filter #142

Closed
Closed
@flexferrum

Description

@flexferrum

Implement 'tojson' filter according to the jinja2 specification:

Dumps a structure to JSON so that it’s safe to use in <script> tags. It accepts the same arguments and returns a JSON string. Note that this is available in templates through the |tojson filter which will also mark the result as safe. Due to how this function escapes certain characters this is safe even if used outside of <script> tags.

The following characters are escaped in strings:

  • <
  • >
  • &
  • '

This makes it safe to embed such strings in any place in HTML with the notable exception of double quoted attributes. In that case single quote your attributes or HTML escape it in addition.

The indent parameter can be used to enable pretty printing. Set it to the number of spaces that the structures should be indented with.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions