Live data from Hacker News

Idea: using code rather than templates to generate HTML

github.com

51–60 of 74 posts

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

#51
post #19

Earlier quoted context omitted.

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

I think it's safe to assume that the class name was chosen for demonstration purposes only.

Bad examples don't help to prove a point.

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

#53
post #24

Earlier quoted context omitted.

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.

Certainly there could be a better way, but HTML and CSS are what we have (and frankly they work pretty well considering that the web comprises mostly documents). Emitting HTML from a programming language can be useful, but it doesn't solve the fundamental inelegance of nested markup. Solutions like this are just indirecting the problem, adding another layer of complexity without a meaningful abstraction.

My project, phpSprockets (http://code.google.com/p/phpsprockets/) does nested markup by working as a node system. After all, if you're going to generate HTML in code you might as well do it in an object oriented way.

$table = new Sprocket( 'table' );

$tr = $table->tr();

$tr->td( "OMG I'm in a sprocket" );

I've done similar things using XElement in C#, though using PHP's magic methods makes for very short readable code.

I agree that templates are handy for separation of concerns though.

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

#54
This is a good idea, but not the right approach. The key is to keep all the "design stuff" in HTML, so a designer can deal with it, and then express all the code as code. XSLT works that way, as does:

http://search.cpan.org/~jrockway/Template-Refine-0.02/

I like the second one better, for some reason.

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

#56
post #43
post #30

Earlier quoted context omitted.

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.

Your argument assumes workflows never change. If that were true, we would still be using whatever workflows our forebears used for every task. Since that is clearly untrue, workflows must be able to change. However, given that business are formed to turn a profit, the standard workflow will remain unchanged until it is profitable to do otherwise. If a markup-generating DSL was simple enough for designers to pick up q…

> If a markup-generating DSL was simple enough for designers to pick up quickly, it would make good business sense to push them through the transition.

Designers being able to do it isn't the problem, justifying why designers should need to is the problem.

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

#59
Didn't expect there to be this much of a response this quickly. Some responses:

1) I want to do projects alone, without a designer in the loop. Why should I use a template when it's wrong for me? This lets me go faster. Therefore the workflow argument is useful, since this is hacker news and not designer news.

2) It's not a new idea. I have seen a bunch of similar libraries, although they tend to use datastructures, more than code directly. I think the small novel bit is that the object mutators return the underlying objects for further mutation (read the code; it's scary)

3) It's not a DSL. It is the language itself. The template programming languages are awful; as soon as you want to do something that they don't define, you are in hell. And the ones with embedded code are a nightmare to look at and format (delicious was in HTML::Mason.)

Markaby is pretty close to what I wanted to do. The other ones I saw for python were not so much.

4) I haven't seen anywhere the notion of context-aware encoding. Unfortunately, I managed to nuke that bit of source while cleaning up and renaming things before sending it to github.

5) I apologize for my code. I am still new to python.

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

#60
post #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 on…

I'll look at "with" -- I'm still finding my legs on python.
Post reply on HN