1

If I have written jinja2 variables in javascript, for example

var array = [{{count}}...

and it works, will it work even if I move the code to a separate js file? Is there anything else I need to know about this practice?

2 Answers 2

3

You can certainly create a Jinja2 template that contains Javascript with Jinja2 variables, render that into a JavaScript file, and serve it to your users. Jinja2 doesn't care what kind of file you are rendering.

An important consideration is that you are changing a static file to a dynamic file. A typical Javascript file is static but you are now making it dynamic which puts additional load on your servers.

A typical solution is to use static JavaScript but render JavaScript data into your HTML page that the JavaScript file can access.

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

Comments

1

I came across this looking for the same kind of solution, and it was pointed out to me somewhere else that the data attribute in HTML is a good solution here as well.

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.