I want to put some template tag for configuration in js static file, and it will get included by in html file. How do I pre-compile the file ? is there any way like ruby 's js.erb file?
Asked
Active
Viewed 418 times
0
-
Why do you want to do that? – Raydel Miranda Dec 20 '13 at 20:10
-
I have some javascript file the source dir is different based on different environment – Wen Dec 24 '13 at 19:54
2 Answers
0
Well, nothing stops you to just do that, maybe like this:
page.html
template:
<script>
{% include "settings.js" %}
</script>
settings.js
template:
var SETTINGS = {
'MEDIA_URL': '{% get_media_url_example %}',
...
};
If you don't want an inline JS code, but a separate file, you can use Django Compressor, which can generate compressed static files from inline JavaScript. Example usage from documentation:
{% load compress %}
{% compress js %}
<script src="/static/js/one.js"></script>
<script>obj.value = "value";</script>
{% endcompress %}

HankMoody
- 3,077
- 1
- 17
- 38
0
You can render anything:
>>> from django.template import Context, Template
>>> t = Template("My name is {{ person.first_name }}.")
>>> d = {"person": {"first_name": "Joe", "last_name": "Johnson"}}
>>> t.render(Context(d))
"My name is Joe."
https://docs.djangoproject.com/en/1.6/ref/templates/api/#rendering-a-context

allcaps
- 10,945
- 1
- 33
- 54