Yeah, using a template variable in a variable name is just asking for trouble.

In general you want the interface between two components to have as little surface area as reasonably possible so it's easier to understand, change, test, and debug individual components and reason about how they interact.

I would actually like to see us eventually disallow inline javascript in top-level templates and instead require a dictionary containing all the  javascript variables needed by a page to be passed as a context variable which would then be output as a single object in base_template, and all javascript would be forced to be included javascript files that explicitly reference the context variables they use.