Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Pure python html templating using with statements (gist.github.com)
83 points by casbon on Dec 11, 2011 | hide | past | favorite | 20 comments


Weby Templates: http://www.jperla.com/blog/post/weby-templates-are-easier-fa...

Weby Templates are used in production on websites that have garnered over 500,000 uniques a month. They are incredibly easy to work with. They are the "templates for lazy people". They have 3 main benefits:

* easier * faster * more flexible

* The main codebase is implemented in 100 source lines of code.

* Extensive libraries only add just a couple hundred other lines of code.

* It is simple.

* It ensures that all tags have closing tags.

* It is compiled and fast.

* It is unicode compliant and safe.

* You can be fully fluent within 5 minutes since it is just Python and a transparent library.

http://www.jperla.com/blog/post/weby-templates-are-easier-fa...


I am confused how these are "faster" than other templating languages, such as Mako. Don't all of the better templating languages "compile to bytecode"? It would seem that the large number of function calls that would be involved per tag would massively decrease the performance vs. something that was able to treat most of the content as opaque.


Thanks, I didn't find that when searching. Very similar idea, but a slightly more involved syntax. I see it uses string building rather than a lib like lxml which probably makes it faster.


This was the thread that I needed: http://hackerne.ws/item?id=1605909


Yea, my early iterations a few years ago looked more like your library does today. I learned a lot about building good APIs through experimentation.


I wouldn't want to generate HTML with this, but it could be very useful for generating guaranteed-to-be-valid XML.

(That said, aside from Atom feeds I haven't had the need to output XML rather than JSON in a very long time)


Could probably use contextlib to simplify the implementation a bit: http://docs.python.org/library/contextlib.html. Indeed HTML generation is the first example.


My view is probably warped because I helped design the PTL "language" for Python. However, it's been my experience if you write well factored code (i.e. Don't-Repeat-Yourself, helper functions/macros), then templates that use the HTML by default mode end up looking extremely ugly. Essentially you end up with mostly Python logic with a little HTML markup mixed in.

It's not friendly to UI designers who like to load it into something like Dreamweaver or translate their static design into the template. I haven't thought of any good solution for that though. Zope TAL seemed like a valiant attempt but I'm sure how successful it was.


i solved that by problem in my library https://github.com/dodo/node-dynamictemplate with the ui designers by masking their static html on my functions, which are only a subset of the part in the static html which you want to cover.

this way using a html mockup from a designer is like using an 3D from a 3D model artist, because you only load the data in, selecting what you want and putting a animatinon mesh / functionality into it.

tldr-part: look at my wip code → https://github.com/dodo/node-dynamictemplate/blob/a46fb6ed18...


TAL is one of the most impressive things to come out of Zope, after using it for years I feel dirty using a templating system allows inline programming logic. Chameleon templates implement Zope's TAL for use outside of Zope: http://chameleon.repoze.org/


This is very interesting, I had the exact idea for the longest time but never got time to fully implement it fully, here is my simple version of it https://github.com/Doboy/Dopy Do.py was the file with the implementation and basic.py is an example input. I am extremely interested in where this project goes and would like to get involved.


Here is a simple idea:

  html(
    head(
      title('This is the title')),
    body(
      div(id='header', body=...)))
We could also create a shortcut for div ID and classes.. for instance a keyword capitalized is an ID, or something starting with _ could be a class.

It could then be possible to make "custom" fields:

html( headers(), left_side(), content(), footer())


I'm pretty sure this has been done. I remember seeing something extremely similar while going through several of the umpteen python web frameworks and thinking "well, it's just html with parentheses rather than angled bracket and an additional leaky abstraction on top".


the gist contains example using lxml:

  E.html(
    E.head(...),
    E.body(...))


I've been playing with a similar syntax: https://github.com/maplambda/py-htmlout/blob/master/htmlgen.... I need to provide better examples, tests and form handling. I'm finding it nice to work with.


Looks good. My own module of this sort auto-escapes HTML special characters in strings fed to it, by default -- it looks like yours doesn't do this, at least at first glance. Is that right?

OTOH I think I like your syntax better.


Thanks, that would be a good approach, mine is currently very barebones! just trying to get the syntax right and get a feel for using it in actual projects - hope to put up a real sample soon.


Yes another interesting python templating library, templet.py:

http://davidbau.com/downloads/templet.py

And some notes about the implementation:

http://davidbau.com/archives/2007/03/11/python_string_functi...


The Nagare framework also does something similar: http://www.nagare.org/trac/wiki/PresentationTier





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: