Using Mako Template in Javascript
Question: I have a dashboard I am working on, using `Python` `cherrypy` framework and
`Mako` template language.

I had an `html` file for each of dashboard pages.

There, I used `Mako` to pass some data to `html` and inline `Javascript`.

For example, to display the names of some processes when I only had the list
of ids, I passed a `Python` `dict` that maps ids to their corresponding names,
and then used the dict in `${}` tags.

However, as I am now moving these `Javascript` codes into a separate file with
`.js` extension, I found out simply putting the same `Mako` code blocks in the
`Javascript` code does not work.

Is there any way I could use `Mako` template language in an external `.js`
file that is imported in an `html` file?

Is it considered a bad practice and should I pass all these data using
`XMLHTTPRequest`s when I am passing them to `Javascript`?


Answer: Ajax is the "right" way to do this.

In order to insert the values into your separate Javascript file dynamically,
it can no longer be served as a static file. Beyond that, it adds an extra
layer of problems with security and maintainability as you have to deal with
string escaping, possible script injection, and having Mako syntax in your
Javascript. Not to mention losing the ability to host your `.js` files on a
CDN or server configured for static files.

