views:

75

answers:

2

I'm writing a Google app engine app and obviously the default web app framework is a subset of Django. As such I'm using it's templating engine.

My question is if I have say the following code:

    template_values = {
   'first':first,
   'second':second,
  }

  path = os.path.join(os.path.dirname(__file__), 'index.html')

  self.response.out.write(template.render(path, template_values))

And I want to reference the template value first in an externally included javascript file from index.html, do I just continue with the {{ first }} usage I'd go with in index.html itself or do I need to tell the framework about example.js somehow too so that it knows to replace the reference there?

+1  A: 

Disclaimer: my knowledge of app engine is limited so this answer may not serve your purpose.

In Django if you need to be able to pass variables from a view to a JS file you will have to ensure that the file is parsed by the template engine. In other words the file has to be served by Django. This means for e.g. that if you have a JS file in a media directory that is served by say, Nginx, you will not be able to use template variables in it.

I would expect the same to apply for app engine. But then see the disclaimer. I might be totally wrong :P

Manoj Govindan
suspected this - how is this accomplished in code? just add it to the path variable?
rutherford
I know the answer for Django - serve this JS file using a thin wrapper over `direct_to_template`. However I don't know how to do it in app engine.
Manoj Govindan
I've looked at the Django method. I'm not a big fan of frameworks to be honest and as it looks like I'd need to adopt the full Django lib to do this I guess I'll just pass any dynamic variables in as function arguments.
rutherford
+2  A: 

Inserting the value into the javascript is probably a bad idea; wouldn't it make more sense for the script to be static and to have it grab the data either out of the DOM (assuming it's part of the HTML page you're rendering) or get the necessary data from the server using an AJAX call?

Wooble
open to suggestions why this is better? Ajax calls would be an unnecessary overhead imo but I kind of like the idea of DOM insertion. Bit more natural giving the data a place in the page I guess?
rutherford