0

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?

Wen
  • 401
  • 5
  • 13

2 Answers2

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