4 ms·
This is great. It seems strange to me that logicless templates who have taken it too far have gained so much popularity in js templating. For someone consideri
by ludwigvan 14y ago
This is great. It seems strange to me that logicless templates who have taken it too far have gained so much popularity in js templating.
For someone considering handlebars, here is what I have found really constraining, note that most of them are by design:
- No builtin equality check between two variables. So, you can do:
`{{#if my_boolean}}`, but cannot do
`{{#if my_variable == 2 }}`
You have two options here:
- Resort to implementing an ifequal helper (and other trivial helpers like gt, lt etc.)
- Prepare everything before handing it to the template, so before passing the json to the template, add another field like, equal_to_two to check for equality with 2.
- Partials lack access to parent.
Say you are iterating over answers of a question, so you do:
{{#each answers }}
{{ text }} - {{ user }}
{{/each}}
At some point, you will want to add some information in the parent scope, so you do this:
{{#each answers }}
{{ answer.text }} - {{ answer.user }} -- in response to {{../user }}
{{/each}}
Now, you may want to register this snippet as a partial:
{{ answer.text }} - {{ answer.user }} -- in response to {{../user }}
So that you can do:
{{#each answers }}
{{> answer_partial }}
{{/each}}
But answer_partial cannot access the parent scope (question item); so you resort to another custom helper "include".
- Lack of keyword and delimiter compatibility with Django/Jinja2; so you need to replace each of your {% if %}'s to {{if}} and so on.
- It is hard to tell what the current context is referring to at times, so, it is helpful to use an additional debug helper (again not builtin) that just logs the current context. Block helpers change the context, except when they don't.
The only reservation I have with nunjucks is that it faces the danger that it might be abandoned like other efforts; other than that, it seems it will address all the painpoints I am experiencing with handlebars, but speaking with jlongster on twitter, he seems determined to keep working on it.
- underwires 14y agoYeah, the equality check thing kills me. I have a page that needs <selects> for choosing a time of day, and so I set a bunch of vars like isHour1, isHour2 and isMin0, inMin5, etc. A total pain when what I want is {{if hour == 1}} http://jsbin.com/uvuhip/1/edit http://jsbin.com/uvuhip/1/edit ugly...
- ludwigvan 14y agoIs that mustache, or handlebars? If handlebars, maybe you could try writing a helper as I mentioned.