Alasdair Nicol: Ponies and moustaches, or Templates in Django

DANIELE PROCIDA: Thank you very much. {Applause} next stop Alasdair Nicol on ponies and moustaches which are in fact technical terms in Django. {Applause}.

ALASDAIR NICOL: Good afternoon and I am going to talk about ponies and moustaches the more silver templates. I’m Alasdair Nicol a developer at Memset hosting concern south-east England. Started using Django in 2009. Like Adrienne who had the slot this afternoon the first time I used Django tutorial it didn’t work at all and I came back a bit later and it worked the second time so it’s good advice to try again if it doesn’t work first.

In 2009 Django 1.0 came out, we’ve seen a few releases since then and in April 1, Django 1.8 came out. It’s a long-term support release so if you upgrade to it you get security releases for quite sometime and some of the highlights API for the model meta class, there are lots of goodies if you use the postgres database and the thing I’m interested in talking about support for multiple template engines.

So, for any visitors who come on to the open day and don’t know what a template engine is I’ll start simply. A template is just a text file with a special - and the template engine can take advantage of special syntax in the template so in this case we’ve got “hello my name is” then 2 variables name and subject. Then if we have a context so the variables, name and subject, then a template engine can render a template and merge the context into the template. So even if you haven’t used Django before you might have used mail merge in office or you might have seen an email where you got some these current in braces which means someone has messed up somewhere.

Before Django 1.8 if you wanted to - your choice of template engines there was a single choice of template angles Django template language. This is perhaps a bit surprising there was only one choice - not surprising there was only one choice when you get other parts of the Django stack there has been lots of choices for some time. Mark this morning was saying that with Django you have a choice of operating systems, choice of servers you employ Django with then what I’m looking at is within Django there is different layers and you have been able to have different choices before so when you come to Django and are choosing database you had lots of choices in database. Postgres SQL very popular with distributors in Django, my SQL which is very ubiquitous what we use, SQ light which is built into python which means you can get up and running very quickly, oracle and there is third party databases you can use as well.

Similarly sessions the cookies or the other way you store data about the user accessing your web-site, with Django you can store sessions in the database, use signed cookies or store on files though probably not a great idea to do that.

Finally one more example part of the Django stack swappable components caching where you take a page or part of your page and save it in memory so to speed up your web-site. There is mem cached support built into Django and third party support for Reddis or you can do caching in other ways as well.

Templates in Django - before Django 1.8 there was only one template language you could use. There were occasionally proposals Django should replace the Django template language with something like jinja. I wouldn’t have liked that because we have hundreds of templates that work and if I had to change them to something else it wouldn’t be fun.

It was a crowd funded campaign launched by Americ Augustin, it’s one of a few Django features which has used Crowd funding and indigo {inaudible} seems to be working well. Any way the 3 planks of the campaign or the 3 goals were to keep support for the existing Django template language add support for jinja 2 and API so other people could swap in any template language they wanted.

So for the rest of this talk I’m going to have an example web page which I’ll show you in a second and go through the 3 bullet points to see how we implement with Django template language with jinja 2 and another engine.

Sorry this is so basic, my skills are round the back end rather than front end development but since Djangocon here is a table how we moved from 2009 in Prague to 2015 here today in Cardiff.

Django is model template view or also other frameworks use module view controller so before we get to the template we have a model which describes the database layout in Python code. Got a very simple conference model. Going to store the location and country as Charfields {inaudible} country would be better. Years into the field. The view is where you describe what data you want to display on the page resident Python. A very simple view which grabs conferences from the database then renders the template. And finally here is the template to - or section of that template in the Django template language. So, we’ve got conference year, conference location, conference country, the double angle braces or moustaches, including variables and then the other couple of things this template has is we’ve got a forward, that’s using a template tag where you’ve got the angle of brace and percentage and we’ve also - we’re using the cycle tag so that we can display {inaudible} even as odd not shown very well on the slide but you can imagine there is {inaudible} extracts going through that column.

So, moving on to jinja 2 which in Django 1.8 there is support for out of the box, jinja 2 syntax is very similar to Django’s, very fast. Used by flask, a micro framework in Python and I have been exploring Ansible recently and it uses jinja 2 for its template as well. Enabling jinja 2 in 1.8 is easy. There is a new templates list and by default it will come up with one entry, the Django template engine and you just need to add the second dictionary to enable jinja 2. Then when you write your jinja templates you put them in a directory with jinja 2 and when you use the short cuts Django will take place of everything else. So here is our template in jinja 2 which works very, very similar to our original template in Django template language. I think the only difference in this template is that instead of using a cycle template tag we’ve moved our cycle instead and jinja allows you to cull functions within the language which Django doesn’t. I think that’s a philosophical language, {inaudible} thinks you should keep language out of templates and jinja allows more logic into templates.

Now on to the third part - I had a go at seeing what the API was - see what the API was like to add support for another templating language and I chose moustache so I could use the title I talked about. The other reason I thought moustache was interesting was that there are so many languages that have support for moustache and we’ve had couple of talks today where people talked about rest APIs and perhaps the need for doing templating on the server side isn’t as much as there used to be but maybe there is a future where you’re doing templating on the server side and on the browser in which case something like moustache where support for Java script and...(inaudible)...is potentially interesting.

Yes in Python there is a module pystache which {inaudible} I used.

Here is my template engine. You need a template object which knows how to render itself and most of that was taken care of using pystache then you subclass {inaudible} base engine. You set what you want to keep {inaudible} moustache directory and then we implement 2 methods, one which will render string and the other render template style from directory and render it moustache template engine.

Here is a template written in moustache. Again using double angular braces for variables. Looking through conferences looks a bit different. There is not a 4 conference and conferences syntax like we had before. And the one thing you might notice is that I couldn’t work out how to - I couldn’t find an equivalent for the cycle tag in Django template language although you couldn’t see it on the slide any way so never matter and you could do it with CSS selecting even {inaudible} any way.

So, we’ve got the 3 layers, the model, the template and the view, and in order to get the view to work with moustache had to change it slightly and add dictionaries instead of conference who ran objects to whatever was rendering the template so I cheated slightly but I think that I have shown them that you can for the most part keep your models and views as they are and swap in whatever template language you want.

So, I am going to declare success. My pony has grown a moustache. If you want any further reading, I am not a {inaudible} don’t worry. The weekly updates from Amaric about decisions he made and process are interesting it’s Django open source and it’s great you can see the collaboration decisions and?? Getting contributed. There is a new process called Django DEPs similar to Python DEPs and I think this multiple template engine is the only DEP so far that’s been accepted but it’s interesting again to see a new way of how decisions about the future of Django are made in public and by the community. The Django design philosophy page on the web-site says a little bit about why Django thinks there should be less - about some of the decisions made about Django template language, I touched on that briefly when I said there is not as much logic in the Django templates as the jinja templates and there is 2 scoops of Django which is a really great book and the new edition has a chapter on jinja and some good hits if you were going to be seriously considering using it in production rather than just a fine exercise.

So, yes that’s my slot. Thank you very much. {Applause}.

DANIELE PROCIDA: Thank you very much. Not least because your helpless claw back lost time. A coffee break waiting for us. Thank you very much. I already saw a couple of hands moving in the audience so who would like to go first with a question? OK I’ll start. So, why might one not want to go down this route of exploring additional templates? Why not just stick with the completely built in out the box Django template language that everyone else uses that you know has been widely tested and will do pretty much every tutorial documentation at work? {Laughter}.

ALASDAIR NICOL: I think that’s a very good point and I personally work anyway - I don’t think we’re going to be switching any time soon. If you had a performance reason you might want to switch to jinja although many people say there is other areas like caching or database where you get more performance rather than templates any way so I like the Django template language, I’m not advocating we switch to something else but I think it’s really good we formalise the API and given people the ability to swap it out if they want to and I think there is some of the even by making the API more generic it’s improved the way the Django template language itself interfaces to me to some extent -

DANIELE PROCIDA: It makes me nervous when I feel there is an implication I might be expected to learn something new.

ALASDAIR NICOL: I agree like a couple of talks today suggested that the future is REST APIs but actually it is very nice that with the Django template language at the moment you can get your tutorial or your log in the Django girls tutorial up and running and you don’t need to learn yet another front end single page app.

NEW SPEAKER: By reaching engine {inaudible} template packs?

ALASDAIR NICOL: Yes I think that is probably the biggest - so the templates act yes I think you’ll need to re-write right the templates act. The filters - I think say if you use jinja, I think you can register filters - a filter is just a python {inaudible} takes a value and one other and returns another value. I think in jinja 2 you can put filters but tabs if you switch to jinja that involves quite a bit of re-write.

NEW SPEAKER: Is there currently the ability to choose a template to run in {inaudible} time so if you wanted to render may be an Ajax view slightly differently with a different engine {inaudible}.

ALASDAIR NICOL: Yes so the example that I used was you do - templates by configuration so you stick your jinja templates in jinja directorate and Django will look for them but when you use rendering in Django 1.8 it’s got engine key word so you can have - I have got example template that can do most of this stuff and the way I’ve shown the view 3 different times is with a get parameter which the engine equals Django or engine equals moustache gets passed through but yes at one time you can choose engine.

DANIELE PROCIDA: Very quick one.

NEW SPEAKER: If I understand correctly the reason the logic is taken away out of Django template languages is so you get better design principles. If you follow jinja is that taken down a darker path? {Laughter}.

ALASDAIR NICOL: Ah ... so I think perhaps you’d be in danger of going down that dark path because one thing jinja has that I would like Django template language which is to be able to look up and attribute by variable which you can’t do, you can use it basically it looks like dictionary look up syntax in jinja which you can’t do in Django I think that would be nice in Django, I don’t think the world would implode if we did that. I think the Django philosophy of trying to keep logic out of templates I side with it to an extent.

DANIELE PROCIDA: Smashing. Thank you very much. {Applause} we have a coffee break now, so go and have some refreshments. We’ll come back, try and be here for in 25 minutes time so at 10 past.

(Break)