News for hackers. Stuff that matters.
Re:Pure python html templating using with statements by ljlolel on Sunday December 11, 2011 @01:15PM (#3340580)
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...

Re:Pure python html templating using with statements by saurik on Sunday December 11, 2011 @11:20PM (#3342239)
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.
Re:Pure python html templating using with statements by casbon on Sunday December 11, 2011 @01:24PM (#3340608)
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.
Re:Pure python html templating using with statements by casbon on Sunday December 11, 2011 @01:43PM (#3340660)
This was the thread that I needed: http://hackerne.ws/item?id=1605909
Re:Pure python html templating using with statements by ljlolel on Sunday December 11, 2011 @02:20PM (#3340769)
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.
Re:Pure python html templating using with statements by simonw on Sunday December 11, 2011 @02:02PM (#3340716)
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)

Re:Pure python html templating using with statements by nas on Sunday December 11, 2011 @01:05PM (#3340547)
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.

Re:Pure python html templating using with statements by dodothelast on Sunday December 11, 2011 @03:11PM (#3340914)
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...

Re:Pure python html templating using with statements by trip42 on Sunday December 11, 2011 @10:14PM (#3342086)
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/
Re:Pure python html templating using with statements by yangyang on Sunday December 11, 2011 @04:10PM (#3341105)
Could probably use contextlib to simplify the implementation a bit: http://docs.python.org/library/contextlib.html. Indeed HTML generation is the first example.
Re:Pure python html templating using with statements by phzbOx on Sunday December 11, 2011 @02:54PM (#3340879)
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())

Re:Pure python html templating using with statements by toyg on Sunday December 11, 2011 @03:35PM (#3340993)
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".
Re:Pure python html templating using with statements by d0mine on Sunday December 11, 2011 @10:05PM (#3342064)
the gist contains example using lxml:

  E.html(
    E.head(...),
    E.body(...))
Re:Pure python html templating using with statements by catch404 on Sunday December 11, 2011 @06:03PM (#3341424)
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.
Re:Pure python html templating using with statements by abecedarius on Monday December 12, 2011 @12:21AM (#3342414)
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.

Re:Pure python html templating using with statements by catch404 on Monday December 12, 2011 @03:37AM (#3342844)
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.
Re:Pure python html templating using with statements by Doboy on Sunday December 11, 2011 @11:06PM (#3342204)
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.
Re:Pure python html templating using with statements by bokchoi on Sunday December 11, 2011 @01:38PM (#3340644)
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...

Re:Pure python html templating using with statements by draegtun on Sunday December 11, 2011 @03:06PM (#3340906)
The Nagare framework also does something similar: http://www.nagare.org/trac/wiki/PresentationTier
Re:Pure python html templating using with statements by jgalvez on Sunday December 11, 2011 @04:59PM (#3341244)