Attention Internet Explorer users: This site won't look like ass if you use a better browser. My site, my rules.

Seasoning Templates

I’m a food geek. I spend my free time contemplating recipes. I bore my friends by talking about the Maillard reaction and annoy them by saying things like, “did you know that cheesecakes aren’t actually cakes at all?” I wonder about the techniques for replacing chemical with organic leavening and vice versa. I watch the Food Network, and Alton Brown is my hero.

I cook a lot, of course, and I’ve gotten pretty good at it. However, there are still a couple of things that give me problems. One area in particular is especially vexing to me:

Salt.

See, I’m a sodium fiend. I adore salty food. I’ve got half a dozen kinds of salt [1] in my kitchen right now. I add extra salt to nearly everything I eat, and that’s the problem: if I salt foods until they taste “right” to me, most other people will think that my food’s far too salty. I’ve had to learn through trial and error what other people’s tolerance for salt is; my own taste buds are essentially broken.

It’s nearly impossible to get right. There’s a whole range of tastes when it comes to salt: my mom likes her food nearly salt-free; my wife has a pretty average taste for salt; I am (as previously discussed) way over on flat side of the bell curve.

Cooking for myself is easy but what to do when cooking for a crowd? If I aim to satisfy my own taste, my guests will wonder how I managed to spill my saltshaker without noticing. I can’t just leave salt out; a certain amount of salt has to be cooked in from the beginning (that 1/2 teaspoon of salt in chocolate chip cookies is vital and couldn’t just be sprinkled on to taste — ugh).

No, the smart thing to do is to aim for the big bulgy area right in the middle of that bell curve. If I get it right, I’ll match the average taste, which means I’ll be able to please the greatest number of people. Sure, my mom will still think my food tastes a bit too oceany, and I’ll need to add some extra salt to satisfy my craving, but the majority of people who eat my food will be happy.


Designing a template language is a lot like seasoning a dish; there’s a whole range of tastes out there. Options range from a language that allows variable substitution and nothing else all the way up to templates that embed a full-featured programming language.

Like seasoning a dish, we aim Django’s template language towards the average. That of course means that a some will find it bland and some will spit it out in disgust.

My advice to those people is to learn how to cook.

[1]Table, kosher, pickling, sea, rock, and popcorn, if you must know.

Update:

It’s about to be seven: Bacon Salt

Comments

Jeremy Dunck

July 19th, 2007

10:46 a.m.

A small bit of context: not everyone's bell curve is the same. You're generally not cooking for a random selection of people.

I regularly advise programmer-only Django projecters to use whatever template language they want, pointing out that the default is good, but has some limitations.

I think the template-is-safe approach is good in lots of contexts; designer-created and shared-host are good examples.

As long as it's an informed decision, they have only themselves to blame if they get it wrong and realize it later.

The problem is just the power of defaults-- but it's still better than shipping with nothing and saying "choose one of these 20, newbie!"

Graham

July 19th, 2007

11:23 a.m.

The slight distaste I find in Django templates is the difficulty in removing some duplication, the (maybe naive) seasoning I would apply would be parameterised includes and extending the for syntax to allow iteration over in template defined lists. There again nothings stopping me writing some new templatetags...

so

foo yes
foo no
foo maybe

becomes

{% for name in ["yes". "no", "maybe"] %}
{% include("foo.txt", {"name": name} %}}
{% endfor %}

Jay Rendon

July 19th, 2007

12:08 p.m.

I tried your chocolate chip and it is awesome! You are a the best! The recipe that I got sucks! Its hard to follow and I cannot make the dough fluffy.

I tried your chocolate chip and you must put more chocolate on it or nobody will eat it. I will say it again, nobody will eat it. Got that?

I tried your chocolate chip and it is pretty good. Can you forward me the recipe? Here is my current recipe I think you may like it.

...just common reactions i hear when someone bakes a cookie

Matt Croydon

July 19th, 2007

12:10 p.m.

Jacob,

This discussion is completely lacking a carpentry analogy.

You're slipping.

Jeff Croft

July 19th, 2007

12:23 p.m.

Let me first say: I totally agree with this post. No one template language is going to fit all, and the beauty of Django is that it's modular enough to swap tempalte languages in and out easily. With that having been said...

As someone who has used the Django template language as much or more than probably anyone on this planet (I worked at the Journal-World for a year and a half, and spent eight hours a day in Django templates), I'm always a little baffled when people suggest that it doesn't have enough "power". I'm an experienced design and HTML author, and I often want to do pretty advanced this, but I almost never come across a presentational stiatuion that the Django tempalte language can't handle. And, when I do, it's always as simple as writing (or having someone who's a better programer than me write!) a short template tag.

I think a lot of it comes from people not really knowing the template language inside and out and not knowing all of the little tricks and tips. I feel like the tempalte language could be better-documented (I know, I know -- I'm more than welcome to submit some docs). As a simple example: Graham's suggestion for prametertizes includes is uncessary:

{% with "yes" as name %}
{% include "foo.html" %}
{% endwith %}

(His suggestion for in-template defined lists is something that the language definitely doesn't support. I could see it as a reasonable addition, but I also think the use cases for it are very few and far between -- I've never come across a time when I wanted that, myself.)

My experience with Django templates can be defined as such: If you can't do it in the template language, it probably doesn't belong in the template. I think this is true 90% of the time. Most of the time when there are things I think I should be able to do in the template language, a second-thinking makes me realize it really should be done in the view. The other 10% of the time can usually be accomadated with a template tag.

Really, the only thing I've ever found myself wanting on a regular basis is comparison support. i.e.:

{% if some_integer lte 6 %} whatever {% endif %}
{% if some_list|length gt 10 %} whatever {% endif %}
{% if some_integer = some_other_integer %} whatever {% endif %}

This has been implemented by James Bennett in the template_utils package, though (albeit not with me preferred syntax). :)

Doug Napoleone

July 19th, 2007

3:08 p.m.

obligatory:

I am taking all this with a grain of salt.

Jeff Triplett

July 19th, 2007

3:47 p.m.

Great analogy. Strangely enough everyone's taste buds for food and template languages are different and it comes from their context too as Jeremy pointed out. You are so right that it's all about taste.

Django's template system was the first system that I used that allowed common ground between coders and designers without getting in the way. Coders tend to gravitate towards template languages that are mini-programming languages while designers complain about not having enough control or not having enough control over elements that are generated through custom widgets, etc. Django's template system manages to solve this problem without limiting either group of people.

It's not a perfect system but it doesn't have to be. You can write template tags to extend and to make it work your way. Personally, I think it helps a team have more respect for what the other group does when you're dealing with multiple grounds of coders and designers.

{% extends "best_chicken_recipe" %}
{% load salt %}
{{ recipe.display|salt:"half" }}

Sorry, couldn't resist on that last part...

Bill de hOra

July 19th, 2007

4:15 p.m.

"No, the smart thing to do is to aim for the big bulgy area right in the middle of that bell curve"

Here's the thing: no one amount of salt is going to work for everyone. But always put in not enough salt, for four reasons. First you can keep adding salt as you cook, but taking it out is much too hard. Second, just not enough salt encourages you to eat the next forkful. Third, your guests can always adjust the salt after you serve up. Fourth, other cooks will big you up; less salt indicates craft.

Django templates - someone should figure out what formal language/logic class it falls into. Because it's not quite enough, and that's exactly the right amount of expressive power for presentation logic.

PeterMHoward

July 19th, 2007

5:46 p.m.

Jeff Croft was probably the guy I read the most of getting into Django, so I completely view the template language from his POV - if you can't do it in a template, it probably doesn't belong there... That is one of the things I love about Django (having previously written all-in-one-file CF and PHP), and now I'll always spend a little more time finding a way to get the logic out of the template.

And re Salt: I've a really low tolerance for salt, and almost never cook with it, though I know I go overboard on the spice sometimes (ie, when cooking for others, I really should put less in and let ppl add things if they want it)... And I read somewhere that you can actually train your taste-buds back, they're not completely busted. It involves lowering your intake of salt dramatically (cold turkey really) and trying complementary flavours (bitters) - after a while (varies per person, so I don't know how long it'd take) you can pick up the nuances in other flavours.

-p

Paul Kenjora

July 19th, 2007

10:39 p.m.

I read somwhere that a person's craving for salt is determined during pregnancy by the mother's consumption of salt. Higher salt intake during pregnancy means you'll like it more.

So if we extend the ananlogy... yeah it still holds true. Your mother used too much or not enough. You may want to talk to Freud or his analogous doppleganger.

Jeff Croft

July 20th, 2007

12:29 a.m.

I'm pretty sure my mother never used a template language.

Ryan Berg

July 20th, 2007

1:49 p.m.

Much like Jeff, once I got used to the template language (I've been using it for two and a half years now) the only functionality I've craved more than once or twice are numeric comparisons. Somehow now that James has given us this functionality, I haven't yet needed it. Go figure.

I've grown to love its simplicity.

Oh, an {% elseif %} tag would clean up overly-nested logic, but I see the argument for not including it, and keeping the number of tags to a minimum.

Adam Spooner

July 20th, 2007

9:37 p.m.

"and Alton Brown is my hero"

I just gained an entirely new level of respect for you. Alton Brown is the best of both worlds, food and geek.

Jill Kaplan (aka your mother)

July 28th, 2007

5:35 p.m.

Thanks for telling the world I have salt-free taste buds. Now how about a phone call?

Jeremy Dunck

July 28th, 2007

10:59 p.m.

Zing!

Moxy

Aug. 4th, 2007

8:30 p.m.

Cool, I really like your site :D I know this post is not about Django but: I Love Django!!!

Regards, Moxy

Gordon Ramsay

Aug. 6th, 2007

8:32 a.m.

"My advice to those people is to learn how to cook."

Is this a subtle attempt to try and promote Hell's Kitchen? http://en.wikipedia.org/wiki/Hell%27s_Kitchen_%28US_TV_series%29

Your 2¢

Comment