

Pure python html templating using with statements  - casbon
https://gist.github.com/1461441

======
ljlolel
Weby Templates: [http://www.jperla.com/blog/post/weby-templates-are-easier-
fa...](http://www.jperla.com/blog/post/weby-templates-are-easier-faster-and-
more-flexible)

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...](http://www.jperla.com/blog/post/weby-templates-are-easier-faster-and-
more-flexible)

~~~
casbon
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.

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

~~~
ljlolel
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.

------
simonw
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)

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

~~~
dodothelast
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...](https://github.com/dodo/node-
dynamictemplate/blob/a46fb6ed18847af7309f66f4cf66abd08517736b/src/compile.coffee#L285-L307)

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

------
phzbOx
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())

~~~
toyg
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".

~~~
d0mine
the gist contains example using lxml:

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

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

------
bokchoi
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...](http://davidbau.com/archives/2007/03/11/python_string_functions.html)

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

------
jgalvez
Also: <http://jonasgalvez.com.br/Software/XMLWitch.html>

