1

I have a python/flask application that I'm creating. I am also using javascript on some parts of it. The current setup I have is that when I render the template, I pass along any additional javascript that needs to be on the page.

@app.route('/index')
def index():
    return render_template('index.html',
    more_js = '/static/js/example.js',
    more_js_script = 'example_js_to_execute();')

It ends up on the page that more_js is the src of a script tag and more_js_script is executed in a script tag. Is there a better way to do this? There's also times I need to pass a parameter to the more_js_script that is a string, but because of the formatting

more_js_script = 'example_js_to_execute('parameter');

will end up like

<script>example_js_to_execute(&#39;parameter&#39;);</script>

on the actual page and not execute correctly. Do you have any tips on how to restructure what I'm doing or execute what I have with parameters?

1
  • Is there some reason not to just include the JS inline in the template itself, or just include a separate JS file in the index.html? Commented May 22, 2014 at 16:15

1 Answer 1

1

Make more_js_script not to be automatic escaping.

http://jinja.pocoo.org/docs/templates/#working-with-automatic-escaping

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.