Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/chris-martin/django-jsdata-tag

Django template tag for safely passing JSON into the Javascript context.
https://github.com/chris-martin/django-jsdata-tag

Last synced: 26 days ago
JSON representation

Django template tag for safely passing JSON into the Javascript context.

Awesome Lists containing this project

README

        

# Django `jsdata` tag

## Example

Let the Django template context contain the value `status = { 'user': 'Alice' }`.

This line in the template:

```
{{ status | jsdata:'initialStatus' }}
```

produces this HTML:

```html

{
"a": "Alice"
}

var script = document.getElementById('initialStatus-json');
var div = document.createElement('div');
div.innerHTML = script.innerHTML;
var text = div.textContent || div.innerText;
window['initialStatus'] = JSON.parse(text);

```

Which is functionally equivalent to:

```js
window.initialStatus = { 'user': 'Alice' };
```