Summary:
How should I reference static files in a handlebars-part in a django template? I can use handlebars if I use verbatim
tags, but then I can't use django's static
tag.
Details
While converting an app to Django, I came across a part that uses handelbars.js
for rendering ajax-call-results. Via, amongst others, "Handlebars.js in Django templates" I found out about the {% verbatim %}
tag.
A simple piece of handlebars works fine with this. But I also have a part where images are dynamically shown based on the result, which looks something like this:
<img src="path/{{ result }}.png">
Now while this works fine if I set the path manually, I believe in Django it is good practice to reference your static files like so:
<img src="{% static 'path/file.png' %}">
Just getting a static_url
constant isn't advised, see for instance this blog
So unless someone has a real compelling reason to fix it otherwise, I believe it is best to use the {% static %}
method.
The naive solution would be to combine the 2 techniques, and literally spray the template with verbatim/endverbatim. Apart from the fact that this looks ugly, illegible and seems like a bad idea from the start, it also doesn't work.
{% verbatim %}
<!-- handlebars -->
{% endverbatim %}
<img src="{% static 'path{% verbatim %}{{ result }}{% endverbatim %}' %}">
{% verbatim %}
<!-- handlebars -->
{% endverbatim %}
This ends in tears, as the result is
TemplateSyntaxError at /
Could not parse the remainder: ''path{%' from ''path{%'
It might be possible to generate the correct static url on the backend side, and render that. But the backend shouldn't be aware of what image we want to show in the template.
Only solution might be to do an extra call to the backend with the 'relative' string (e.g. path/result.png
) to the backend, and ask for the correct static link? This isn't that hard, but requires an extra call, which shouldn't be the case.
So how do I correctly reference these static files ?