Live data from Hacker News

Idea: using code rather than templates to generate HTML

github.com

1–10 of 74 posts

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

#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.

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

#3
One thing: use the "with" keyword so that you don't have to do td.close(), etc every time. It's cleaner and reduces bugs.

Also, I agree. I'm using templates that are just python functions in my new Python web framework, Webify (http://github.com/jperla/webify/tree/master).

I honestly don't think I've made it clean enough though (there are a lot of yield's there). Nevertheless, I think I'm on the right track.

The only thing I'm not sure about is that the Django people seem to think that allowing designers to edit code the html directly should be allowed, and that they need a simple secure straightforward html template language. Pure Python obviously avoids that. I also kind of like the straightjacket of Python templates.

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

#6
I like the idea, but it's not exactly new (at least not in the Ruby community).

The most known is probably Markaby, written by _why two years ago: http://redhanded.hobix.com/inspect/markabyForRails.html

Nokogiri also ships with a similar library, and Tagz is another one.

I've actually written such a library myself which uses ParseTree/RubyParser to parse the code and generates plain Ruby. Of course, nobody is ever going to use it, but it was a load of fun and it's super fast.

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

#8
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.

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 tiniest templating system is more flexible.

(Sorry; pet peeve. ;))

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

#9
Answer to "why not just use code to generate HTML?": to facilitate communication between the programmer and the designer. If you work with designers who create the HTML for you, it is much easier for them to deal with back-and-forth tweaks and edits using a template. But if you are acting as both the programmer and the designer on a project, then yes it's possible that coding the HTML might be better.
Post reply on HN