How to use template in js files?

223 views
Skip to first unread message

Raven

unread,
Mar 18, 2013, 10:24:40 AM3/18/13
to python-...@googlegroups.com
Hi all

I am confused by the following situation:

I have the index.html writing
<script type="text/javascript" src="{{ static_url('main.js')}}"
</script> in head node.

And in main.js, I want to use some images such as ico.png.

How to decide the path to the ico.png using template?

I tried "{{ static_url('ico.png')}}", where static dir was defined as
static_url. But it was not work.

While I tried "static/ico.png", it worked.

Is there anything that I have missed.

I would appreciate for any replay.

-Raven

aliane abdelouahab

unread,
Mar 18, 2013, 2:44:55 PM3/18/13
to Tornado Web Server
javascript is the part the the browser will execute, so i guess all
paths have been transormed when they reached the browser, so i guess
the {{ static_url('ico.png')}} has become something like /path/to/
ico.png , so check in your javascript code, maybe he calls the
picture from a directory that is not defined (a parent directory for
example).

Russ Weeks

unread,
Mar 18, 2013, 3:52:19 PM3/18/13
to python-...@googlegroups.com
Hi, Raven,

You're specifying a static url for main.js, so it's not treated as a template.  main.js will be served to the client with no variable substitution.

If you want to use Tornado's template syntax in main.js, register a handler for eg. '/main.js' and render the template file with your desired value for the ico.png url.  Then, in your index.html, make sure that the url to main.js points to the same url you registered, instead of a static url.

More info about rendering templates here: http://www.tornadoweb.org/en/stable/overview.html

-Russ





-Raven

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Reply all
Reply to author
Forward
0 new messages