Hacker News new | ask | show | jobs
by erlehmann_ 3266 days ago
Templates cause bugs. The solitary appropriate solution is an unparser – a component that walks an AST and serializes it.

Making sure the result conforms to the grammar of the output language without any unparser would always involve a parser.

> As soon as I'm looking at more than one programming or markup language in the same file, I'm looking at spaghetti code.

Iain Dooley, December 2011

http://www.workingsoftware.com.au/page/Your_templating_engin...

2 comments

This article has some good points but the reasons we add more logic to templates are:

- just pre-generating everything outside of the template can be very efficients. Especially if you language can't make everything lazy or if you have several representations for the same dataset.

- designers want a bit more freedom that just printing x. Having to go back to the dev team everytime you need a little tweak is terrible

- all templates are not HTML

- it's way easier and faster to prototype

- rendering caching != data caching

- everything is not about display. Linking and injecting resources are a big deal, and putting that outside of the template is a huge pain.

- conditional template inheritance ? includes in loop ?

- stuff like wordpress have entire business based on the fact you can switch templates on the fly without touching the blog code base or without the wp team to know what you are going to need inside the template in advance.

Yes, all of those are reasons. None of those reasons apply if users wwant something understandable, maintainable and secure.

Separation of concerns is a thing that can help designers, no?

> you simply include everything in the markup that might need to be there, and the programmer removes whatever is not necessary

I realize you may not be cosigning on everything in the article you're quoting, but this is the author's first suggestion to an alternative to template languages.

I work on an SPA that was built like this. The index.html is over 10k lines long. It contains almost every single piece of the UI. Templating libraries and languages aren't perfect but they offer a better separation of concerns than just "dump it all in one file and write some imperative dom fiddling code to add different states"

Note that I made a different proposal above the quote.

I think SPAs are either fundamentally dishonest engineering, in the same way that a microwave wrapped in artificial wood veneer is (and a stainless steel microwave is not) or should result in such a template. If you really think that this is too much, IMO you should not make an SPA in the first place.

> I think SPAs are either fundamentally dishonest engineering, in the same way that a microwave wrapped in artificial wood veneer is (and a stainless steel microwave is not) or should result in such a template.

Could you rephrase or expound on this metaphor? I have no idea what you mean by "fundamentally dishonest engineering" or what that has to do with SPAs and templates.