Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Templating with Javascript or Django?

I'm building a Django web application which has a lot of html generated on the fly by ajax requests. Right now I use Django's templating language to build up html and then pass this new HTML as a string in JSON object which is then injected into the page with jQuery.

This works fairly well, but with Javascript being so fast in modern browsers and with so many javascript template libraries being made I'm wondering if I should push everything clientside.

So my question is: Given the that my average "page" with all requests to and from it has to compile around ~300 templates (each of about 15 or so lines with 5 or so substitutions) out into HTML during its lifetime is there significant performance advantage to doing templating in the browser?

In addition can anybody reccomend a 'fast' Javascript templating library? I've heard good things about underscore.js, mustache.js and jQuery template.

like image 427
Stephen Diehl Avatar asked Nov 19 '10 22:11

Stephen Diehl


People also ask

Can Django template be used in JavaScript?

Server-side rendered templates HTML templates are rendered on the server, this is the traditional approach. Django templates with javascript added throughout your templates. Leverages all of Django's built in features. Great for small teams, allows for quick prototyping.

What templating language does Django use?

Django Template Language or DTL is a text-based Template language that provides a bridge between scripts like HTML, CSS, JS, etc. and programming languages like python. DTL is specifically built for developers to embed Django logic codes into HTML template files.

How integrate JavaScript with Django?

path/to/your/file. js is a path relative to the static folder. You should use django-compressor to minify your files on production environment. As long as the routes you defined in the Django app are the same that in the Java app for the AJAX calls, you shouldn't have to change the content of your javascript file.

Do you need a front end framework with Django?

Django has no bearing on which frontend you should use. That being said, I like React, I like Vue. I like a bunch of frontend frameworks. They're both easy to use.


2 Answers

The (massive) advantage of sticking with Django templates is that you only need to use one templating language, which retains the same capabilities regardless of the page you wish to generate. If you find that you're having performance issues then you should consider looking into caching template fragments.

like image 138
Ignacio Vazquez-Abrams Avatar answered Oct 20 '22 01:10

Ignacio Vazquez-Abrams


I don't think an hybrid client and server templating architecture is bad. As long as you code a template only in one of the environment.

Every time you generate a page server side, an amount of processing time and some network bandwidth is consumed. This is what you pay if you use hosted servers.
While the user's browser is awaiting idle on a generally idle computer for the response.

If you send the template on the client(HTML + JS), they can be cached, for the session or even days, if the user do not delete them.
This reduces the network traffic to deliver the same content various times. As the data are generally smaller than their equivalent rendered HTML.

As you point today's Javascript engines are really fast, as well as the computer they run it. Each time you sent the rendering work to the client, you save some processing time for your server and deliver faster the data.

We're at the other extreme side, as we run all on the client and that's why we created PURE for an ultra-fast client rendering. Our app looks very fast as a result of this decentralisation.

like image 3
Mic Avatar answered Oct 19 '22 23:10

Mic