Live data from Hacker News

Idea: using code rather than templates to generate HTML

github.com

21–30 of 74 posts

Re: Idea: using code rather than templates to generate HTML

#21
post #2

I've been sitting on this for a while and finally pushed it out, having never really had the chance to give the idea the work it deserves. Anyway, the idea is simple: templates are a mess. They have goofy language variants, etc. Your editor can't color them. Why not just use code to generate the HTML? Still needs: Context-sensitive escaping, css generation, javascript inclusion.

I don't want too sound negative, but there are a number of reasons this is a less than feasible idea. First it's overkill. The reason we have a www at all is because HTML and its variants are dead simple. If the client decides that six key sentences should be yellow and emphasized, I go into my template and add a Yellow text! to my template. Using your generator, I now have to figure out a whole new way of doing that…

Er, or you could do Yellow Text!. But that's not even the best. What if he wants to change the color later? Special Text!.

CSS classes should be used sparingly and by purpose: they should describe the function of the element, and not the look of the element.

But that's just semantics ;) (har, har).

ASP.net uses code generated html for a lot of things: server controls, some user controls, and so forth. It can be a pain to override them if the markup they generate is crap (and from my experience, the built-in server controls and many of the 3rd party controls generate crap).

Re: Idea: using code rather than templates to generate HTML

#23

Earlier quoted context omitted.

I don't want too sound negative, but there are a number of reasons this is a less than feasible idea. First it's overkill. The reason we have a www at all is because HTML and its variants are dead simple. If the client decides that six key sentences should be yellow and emphasized, I go into my template and add a Yellow text! to my template. Using your generator, I now have to figure out a whole new way of doing that…

If you ever name a class yellow, you've entirely missed the point of CSS.

Maybe it should be:

  Yellow!

Re: Idea: using code rather than templates to generate HTML

#24

Earlier quoted context omitted.

The reason why not is that eventually you will want to hand the site to a designer and not have to do a half-hour of work on it every coupla weeks for the rest of its life. Making things easier for the designer reduces your total work; forcing the designer to work inside the HTML your tool produces will either limit the nifty things she can do, or require you to come back and touch this code again and again. Even the…

Indeed, the most effective team will be at least minimally cross-disciplined. I think the desire to generate HTML via code is just a case of unfamiliarity. I've been writing code since 1986 and HTML since 1994, and I have to say that there is no advantage to generating via HTML per se (frameworks like Seaside notwithstanding). If you have a decent text editor it can color and edit templating languages just fine. Deve…

What if you want to repeat a similar chunk of HTML in a loop? What if you want variables? Functions? ? The world has spent too much time trying to hammer programming language constructs into XML-based languages and templating systems. There simply has to be a better, way, but no one has successfully come up with one that satisfies everybody yet.

Re: Idea: using code rather than templates to generate HTML

#25
post #14

Not to rain on your parade, but this is a pretty ancient idea. I don't have links handy but I remember doing this with PHP (amusingly enough) around the turn of the century, and I'm pretty sure Perl has had something in place for eons. There are also a number of Python options if you google around. Though, at least looking at the Python implementation it seems very clean and simple compared to others I've seen (thoug…

For a project 1.5 years ago, I generated all my html via markaby from _why. It really kept my html very very clean, but the downside was that the designer guy didn't understand what the heck was going on so I haven't considered using it since.

Re: Idea: using code rather than templates to generate HTML

#27
I've implemented this in our Lua web application, and it makes writing HTML from code much better... although we still have templates. The API solves all sorts of problems, from unquoted attributes to unclosed tags, and all around makes life easier. I stole most of it straight from Seaside, of course.

Re: Idea: using code rather than templates to generate HTML

#28
Advantages: Automatic tag closing. Near elimination of XSS opportunities (once you write proper escaping in the first place). Ability to take advantage of all normal code constructs without having to explicitly import them into the template language directly, which allows for some nice abstracting.

Disadvantages: Performance; it is intrinsically slower to process things on a tag-by-tag basis than to just blindly shovel out bytes from a template. Optimization may be able to get this to the point you don't care, but you're starting out behind. The classic "designers can't handle this directly" argument cited several other times. There are other reasons to think that maybe templates shouldn't be in a Turing-complete language (though I'd argue once you are there it might as well be a nice Turing-complete language and not a hacked-together one).

Personally, I'm not a huge fan of the "dumb designers" argument because any designer that dumb is probably lobbing in XSS vulnerabilities everywhere, but that depends on your framework. I also think that if your language starts out looking just like a slightly-respelled HTML that it's hardly any different than current template languages, which aren't actually HTML either. But most people seem to stop at the "designers" argument and consider it to trump all, no matter what the other advantages may be.

Personally, I lean in favor of using a code-based system because in my value system, "preventing XSS and other injection vulnerabilities" rates above everything else, even "designers can modify my code" and building HTML with code makes this much easier to enforce, but... evidence is pretty strong I'm unusual in this view.

Re: Idea: using code rather than templates to generate HTML

#29
If you have to communicate with designers, you have to use templates. And templates are only a mess when people insist in inserting $random_programming_language code in them.

If you have a language designed for templates from the ground up, like, for instance, Zope Page Templates, you can have designers editing valid HTML that's also a valid template, comfortably in their editors of choice.

Re: Idea: using code rather than templates to generate HTML

#30
post #25
post #14

Not to rain on your parade, but this is a pretty ancient idea. I don't have links handy but I remember doing this with PHP (amusingly enough) around the turn of the century, and I'm pretty sure Perl has had something in place for eons. There are also a number of Python options if you google around. Though, at least looking at the Python implementation it seems very clean and simple compared to others I've seen (thoug…

For a project 1.5 years ago, I generated all my html via markaby from _why. It really kept my html very very clean, but the downside was that the designer guy didn't understand what the heck was going on so I haven't considered using it since.

you've hit on why html (or css or javascript) generators will never catch on. they do not fit within the traditional workflow.

you cannot take a delivered html document and convert it into whatever html-generator you use. the designer will have no clue how to use your (hand) converted file in dreamweaver to make small changes. in the end, it just makes life harder for everyone and costs more money.

Post reply on HN