Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client-Side (JavaScript) Django/Jinja-like template inheritance

I'm looking for a way to do template inheritance to a group of .html files I have.

Let's say I have a base.html file which has the common HTML for all pages of my website, i.e. header, footer, etc. . Each page, including the main (index) page, needs to inherit from this template HTML file.

Now, this is doable on the server-side using Django's Jinja template langauge. But this is not a good solution for me. My HTML pages are stored remotely and I have no control over the server storing them. This must be done client-side, with or without JavaScript.

Somebody asked about this before, and the only half-baked solution I found was to use Mustache's partials, which doesn't really do the job.

Does anybody know of a way to do this on the client-side?

like image 734
Ory Band Avatar asked Aug 10 '11 17:08

Ory Band


People also ask

Is Jinja server side or client side?

Jinja2 is a server side templating tool. It allows you to build static pages very easily. If all you want is a simple website with static pages then use Jinja.

How do I inherit a template in Django?

extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.

Can I use Jinja with Django?

Having both Django's template engine and Jinja2 allows you to use both flexibly so for example you can still have the admin panel provided by Django and any other plugins you might want to use, that use Django templates. Be sure to read Jinja2 documentation also to learn more about how to use it.

Is Django client side rendering?

Django can do either, but it is designed to render on the server side in its default configuration, not the client side.


2 Answers

I just released this for the same purpose: https://github.com/ericclemmons/jinja.js

like image 66
Eric Avatar answered Sep 16 '22 19:09

Eric


1. Yes, with Dustjs you can "have a template inherit the bulk of its content from a common base template".

Check out the Dustjs docs, here: Dustjs, the "Blocks and Inline Partials" section.

(I posted an answer to a related question, here: https://stackoverflow.com/a/12432034/694469 )


2. There's also some work in progress on adding template inheritance to Handlebars, see another answer of mine to "the other version" of this question: https://stackoverflow.com/a/12432490/694469.

It links to this GitHub issue: https://github.com/wycats/handlebars.js/issues/208, and this blog post: Template Inheritance for Handlebars

like image 33
KajMagnus Avatar answered Sep 19 '22 19:09

KajMagnus