Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/chris-martin/django-jsdata-tag
- Owner: chris-martin
- License: mit
- Created: 2014-06-09T08:48:37.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-06-09T09:10:39.000Z (over 10 years ago)
- Last Synced: 2023-03-11T07:58:46.915Z (over 1 year ago)
- Size: 152 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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' };
```