Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading external script with jinja2 template directive

I'm very new to jinja2 and the use of templates in general so I was wondering if there's an easy way to load an external javascript. I was thinking of using:

{% block javascript %}     <script src="myscript.js"></script> {% endblock %} 

But I can't help to ask:

Is there a way of loading this script directly from within a template directive?

like image 460
kirbuchi Avatar asked Aug 05 '10 06:08

kirbuchi


People also ask

Can you use Jinja2 in Javascript?

JsJinja lets you use your Jinja2 templates in Javascript. It compile the Jinja2 templates to Javascript with no restrictions. The js can be generated via command line jsjinja <template file> or through the {% jsjinja %} tag in the templates.

What is the difference between Jinja and Jinja2?

Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.

Does Ansible use Jinja2 template?

Ansible uses Jinja2 templating to enable dynamic expressions and access to variables and facts. You can use templating with the template module.

Does Jinja2 work with Python 3?

Jinja2 works with Python 2.6. x, 2.7. x and >= 3.3. If you are using Python 3.2 you can use an older release of Jinja2 (2.6) as support for Python 3.2 was dropped in Jinja2 version 2.7.


2 Answers

You have two choices here -- the first is the way you did it -- simply add the appropriate markup into a template (or a block if you want to be able to override it in templates which extend your first template.)

The second way is to use Jinja2's include function:

{% block javascript %}     <script type="text/javascript">         {% include "myscript.js" %}     </script>     <!-- The contents of myscript.js will be loaded inside the script tag --> {% endblock %} 

The advantage of using include is that Jinja2 will process your javascript before including it -- which means you can have variables in your javascript that change depending on the state of your program.

The disadvantage of using include in this manner is the same -- your .js file will be run through Jinja2 before being sent out -- if you are not using dynamic content you will just be processing the file unnecessarily for every request -- and if you are using a javascript templating library with Jinja2 syntax then trouble is likely.

like image 173
Sean Vieira Avatar answered Sep 20 '22 15:09

Sean Vieira


This question is quite old, but there is another way of doing it that might be interesting as well. I found it while working with Jinja2 and flask.

I used the url_for() and it works fine:

{% block javascript %}     <script src="{{ url_for('static',filename='myscript.js') }}"></script> {% endblock %} 

And I have my myscript.js in my static folder. Specified in Jinja2 environment, or by default in flask.

like image 36
Sylhare Avatar answered Sep 21 '22 15:09

Sylhare