23 points · by joshu · 17y ago · 66 comments
It seems like people tend to prefer templates, however I'm not yet convinced. I think there are good arguments for language-generated code, especially with a language that is powerful enough. HSP in Haskell provides some interesting possibilities here (http://www.cs.chalmers.se/~d00nibro/hsp/thesis.pdf). Text.XHtml is another Haskell library that does this (http://www.haskell.org/ghc/dist/current/docs/libraries/xhtml...).
Though, at least looking at the Python implementation it seems very clean and simple compared to others I've seen (though you're importing pprint twice), but then again I'm pretty opposed to the idea in the first place so I haven't really browsed the available options.
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.
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 quickly, it would make good business sense to push them through the transition.
HTML is a designers tool, and generating it is basically a programmer working by what they perceive as an annoyance by shooting someone else in the foot. It also smacks a bit of "I see no value in how this is done so I'm going to generate it from code", which I think is ignorant, but that's my opinion.
I also don't think it's as maintainable, but I have no evidence to back this up (but I really don't want to be the guy who has to go decipher the code that generates a website two years later, I'd even rather deal with ColdFusion).
I thought the modern idea was that CSS is the designer's tool, and HTML should only be used for marking up content in the most semantically "correct" way (which has nothing to do with design).
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.
"I am Avi Bryant, your argument is invalid."
Replacing templates per se? Much harder sell...
Seaside is for building web applications, not web sites. I wrote about this difference sometime back at http://motionobj.com/blog/smalltalk-needs-a-framework-like-r....
But the approach of using code to generate HTML should be applicable for web applications, it just so happens that Seaside is not suitable, state-heavy for one.
The most common argument against code-generated HTML is this makes it hard to work with designers who work with HTML. But as a developer, how often do you change/tweak the HTML after a designer pass you a comp, either in HTML or a PSD? I always do, and I suspect it is the same for almost everyone else. Be it using templates or code-generation, the designer is not going to care, nor is he going to know how to structure the page so that elements can be shared or values substituted effectively. And if he tweaks the CSS, he can't change the HTML without working with the developer anyway, since that will break your code. So it's not like using templates makes the problem go away.
http://docs.plt-scheme.org/continue/index.html#(part._.Rende...
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.
(Sorry; pet peeve. ;))
Developers should write HTML for the same reason that business people should learn to edit HTML--it's the best canonical format for all stakeholders.
Templates can be incredibly simple and require no further attention, but even slightly more complex ones like Smarty work really well, in my experience.
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 <span class="yellow"><em>Yellow text!</em></span> to my template.
Using your generator, I now have to figure out a whole new way of doing that.
The second point follows: Any first year web monkey can learn to add spans and em's. It takes a bit more effort to understand python. While this might not matter to someone who understands both, it offers much less flexibility to the already available alternative.
Django's template implementation is very, very good. It fits quite well into your average web shop and even fails silently.
I like your desire to scratch your own itch, but I felt I should answer your question.
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).
Not calling it yellow I think is the more important piece. In fact class="sctmtdt" seems best to me.
* (Stupid Client Told Me To Do This) :-)
<span class="client_wants_yellow_client_gets_yellow">Yellow!</span>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.
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.
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.
from nagare.namespaces import xhtml
l = ['the', 'mandatory', 'hello', 'world']
h = xhtml.Renderer()
with h.div(id='content'):
with h.ul:
h << [h.li(element) for element in l]
print h.root.write_htmlstring()
Or a (X)HTML template can have nodes annotated with a 'meld:id=<id>'
attribut. They are then retreived and manipulated in Python.In both cases, one of the main goal is not to reinvent a new syntax / templating language but to use normal Python with all its power. Also the generated (X)HTML is always well-formed.
and others, as pointed out in Guido van Rossum's 1999 "Using Python for CGI programming"
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.
http://search.cpan.org/~jrockway/Template-Refine-0.02/
I like the second one better, for some reason.
["head", ["title", "HeRF Demo"]],
["body",
["div", {"id": "content"},
["h1", "HeRF - The HTML Rendering Framework"],
["hr"],
This seems almost as painful as writing out html.Also, you may not have noticed that even though it can handle the data in tuples, if you write it in list form (as in your comment), it's trivial for a program to add extra items enclosed in a particular tag. So if I have:
x = ["ul"]
I can just append elements to x to fill out the list: for i in range(10): x.append(["li", "Item %s" % i])
However, I don't think that it's _quite_ as painful as writing HTML... You don't have to worry about closing tags, for instance. That said, I really wish Python could deal with barewords in data structures, because that would really clean it up otherwise.(html, (body, (h1, "header one text") ) )
To me it's certainly not as painful as writing out html. I'm still working on it - hope to have a demo soon but its not a high priority, more something to sketch in the notebook when bored.
I have wondered if anyone would be interested in a templating syntax like this and got some response from the python mailing list -enough response to determine it's worth pursuing.
Its for personal projects so the designer seperation isn't a concern - I perfer to be able to generate it by code.
I've been thinking "templating system" for the last week none-stop. After seeing that project I just could not stop laughing. Yea why not code...
O well back to templating :)