If I\'d like to generate JavaScript code, and not just HTML, through Jinja2, am I stuck with keeping the JS code inline, or is there a way for me to reference the script?
You can add a route that generates the JS.
@app.route('/script.js')
def script():
return render_template('script.js', color='pink')
And in script.js, this should be in the same folder as your other templates:
function myEnterFunction() {
element = document.getElementById("demo");
element.style.backgroundColor = "{{ color }}";
}
And in your layout.html:
<script src="{{url_for('script')}}"></script>
Jinja2 will parse any files containing it's syntax.