473,513 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

templates

For some of the web programming I've done in Python, I've used
htmltmpl. I had some experience with it in Perl, and found a Python
version.

http://htmltmpl.sourceforge.net/

I like that there's nearly a complete separation between the
presentation and the code. This is great when one person is designing
the pages and another is writing the code to drive those pages.
However, that's usually not the case for me. I'm usually doing both,
and I'm thinking there's got to be something possibly better out there.
I'm curious about other templating systems. I wouldn't be opposed to a
little bit of code in the template. I just don't want to go to the
other extreme of something like PHP or ASP. i.e. all code in the
template.

What web templating systems do you use and why?

Mike
(Note: I posted this on the tutor mailing list week, but I didn't get
any replies, so I thought I'd try here.)

Jan 30 '06 #1
14 1597
projecktzero a écrit :
For some of the web programming I've done in Python, I've used
htmltmpl. I had some experience with it in Perl, and found a Python
version.

http://htmltmpl.sourceforge.net/

I like that there's nearly a complete separation between the
presentation and the code. This is great when one person is designing
the pages and another is writing the code to drive those pages.
However, that's usually not the case for me. I'm usually doing both,
and I'm thinking there's got to be something possibly better out there.
May be... Depending on your definition of "better" !-)
I'm curious about other templating systems.
Then here are two starting points:
http://www.webwareforpython.org/Papers/Templates/
http://www.skreak.com/wp/
I wouldn't be opposed to a
little bit of code in the template. I just don't want to go to the
other extreme of something like PHP or ASP. i.e. all code in the
template.
You can do PHP without messing up logic and presentation.
What web templating systems do you use and why?


Mostly ZopePageTemplate, and actually playing with Myghty too.

Jan 30 '06 #2
projecktzero wrote:
I like that there's nearly a complete separation between the
presentation and the code. This is great when one person is designing
the pages and another is writing the code to drive those pages.
...
What web templating systems do you use and why?


A plethora of such Python web templating systems exists.

If you really care about separation between code and layout, try
attribute languages like TAL or Kid.

TAL is used by Zope, but there is also a nice standalone implementation
called SimpleTAL.

Kid is used by TurboGears and can also be used with Webware for Python.
I think Kid is a very clever and innovative solution.

http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL
http://www.owlfish.com/software/simpleTAL/
http://kid.lesscode.org
http://www.turbogears.org
http://www.webwareforpython.org/KidK...sersGuide.html

-- Christoph
Jan 31 '06 #3
projecktzero wrote:
For some of the web programming I've done in Python, I've used
htmltmpl. I had some experience with it in Perl, and found a Python
version.
What web templating systems do you use and why?


BTW, there are also a couple of other very clever concepts for creating
web pages from Python. It must not always be templating systems. E.g.

XIST: http://www.livinglogic.de/Python/xist/
Nevow: http://divmod.org/projects/nevow

-- Christoph
Jan 31 '06 #4
Christoph Zwerschke wrote:
It must not always be templating systems. E.g.

Nevow: http://divmod.org/projects/nevow


Just saw that Newvow provides templates and a tag attribute language as
well, not only the "Stan" part which is more like XIST.

-- Christoph
Jan 31 '06 #5
I have used PyMeld (http://www.entrian.com/PyMeld/) which is one of
very few that gives a 100% separation of code and presentation, in fact
PyMeld is not strictly speaking a template system at all.
I have also used Cheetah. (http://www.cheetahtemplate.org/) However for
a recent project (http://muti.co.za) I ended up using Python's built in
%s templating engine. The reason for this was performance. The %s
native system is extremely fast. In my somewhat informal benchmarks
PyMeld was two orders of magnitude and Cheetah one order of magnitude
slower than Python native templates.
I don't mean to criticze these two systems, they are both excellent,
and I probably could have spent some time trying to figure out why they
were so much slower but in the end it was quicker for me to go with
something I knew was super fast. I did try compiling the Cheetah
templates which provided a small performace boost but it was still much
slower than native templates. (I also tried the new $ based native
template system in Python 2.4 but it doesnt have the flexible
formatting support that the % one has)

Jan 31 '06 #6
thakadu schrieb:
I have used PyMeld (http://www.entrian.com/PyMeld/) which is one of
very few that gives a 100% separation of code and presentation, in fact
PyMeld is not strictly speaking a template system at all.


Yes, it is more like XIST that I mentioned in another post.
The reason why this is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML. If you are only filling in a few words, then
native templates will be surely much more effective. But if you are
messing around with the structure of your page, inserting and adding
elements, then PyMeld will be probably a better way.

Here are two wiki pages listing template engines:

http://wiki.python.org/moin/WebProgramming
http://pythonwiki.pocoo.org/Template_Engines (German)

-- Christoph
Jan 31 '06 #7
Christoph Zwerschke wrote:
thakadu schrieb:
I have used PyMeld (http://www.entrian.com/PyMeld/) which is one of
very few that gives a 100% separation of code and presentation, in fact
PyMeld is not strictly speaking a template system at all.


Yes, it is more like XIST that I mentioned in another post.
The reason why this is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML. If you are only filling in a few words, then
native templates will be surely much more effective. But if you are
messing around with the structure of your page, inserting and adding
elements, then PyMeld will be probably a better way.


Unless I'm misremembering, PyMeld is special amongst the "total
decoupling of code and presentation" crowd in that it does *not* convert
the whole page to objects in memory, but instead performs its operations
using regular-expression substitution, and serializing back to HTML is a
trivial operation since it's already serialized.

I'm sure the extra overhead versus simple % substitution is noticeable,
but I have to say I was surprised to hear that it was measured at two
orders of magnitude slower. I thought better performance (versus the
"manipulate in-memory as DOM" approach) was one of its advantages.

-Peter

Jan 31 '06 #8

[Christoph]
The reason why [PyMeld] is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML.
[Peter] Unless I'm misremembering, PyMeld is special amongst the "total
decoupling of code and presentation" crowd in that it does *not* convert
the whole page to objects in memory, but instead performs its operations
using regular-expression substitution, and serializing back to HTML is a
trivial operation since it's already serialized.


Peter is right for PyMeld, Christoph is right for PyMeldLite (which is a
variant of PyMeld used by Spambayes, and only works with valid XHTML).

The performance of PyMeld therefore depends on how much work you make it do -
if you insert a couple of values into a page, it's very fast. If you do lots
and lots of operations on the whole of a large page, it can be very slow
because it's doing complex regular expression operations on a large string.
(It works this way because two of its design goals were to only touch the
parts of the page you ask it to touch, and to work with arbitrary, possibly
invalid, HTML.)

Incidentally, I'm changing PyMeld's license from Sleepycat to BSD, making it
free for use in closed source projects. (I'll get around to updating the
website RSN 8-)

--
Richie Hindle
ri****@entrian.com
Jan 31 '06 #9
has

Christoph Zwerschke wrote:
thakadu schrieb:
I have used PyMeld (http://www.entrian.com/PyMeld/) which is one of
very few that gives a 100% separation of code and presentation, in fact
PyMeld is not strictly speaking a template system at all.
Yes, it is more like XIST that I mentioned in another post.
The reason why this is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML.


PyMeld's not a good example to judge DOM-style templating by: the main
reason it's a poor performer is its implementation is very inefficient.
There's a couple of third-party redesigns kicking about the web if you
want to search for them; I've not tried them myself but I imagine
performance is one of the issues they address.

It is true that DOM-style templating engines will still tend to be a
bit slower at rendering than a macro-style templating engine even when
all else is equal, but that's a price you always pay for choosing a
dynamic over a static approach.

For example, I once did some quick-n-dirty comparisons for my own
HTMLTemplate engine [1] which is fairly well optimised pure Python
code, and reckon its performance on typical insertion and iteration
tasks is maybe 1/2 - 1/3 of Cheetah's. (IIRC, PyMeld ran up to 100x
slower in those particular tests due to its naive algorithms.)

OTOH, you have to remember that HTML rendering is only one small
component in the overall application, so you really have to consider
this against the performance of the rest of the application to know if
a 2-3x difference in rendering speed makes a significant difference or
not. Chances are the biggest bottlenecks will lie elsewhere. Besides,
if pedal-to-the-metal performance is your primary concern, you should
probably be writing everything in C++ anyway.

If you are only filling in a few words, then
native templates will be surely much more effective. But if you are
messing around with the structure of your page, inserting and adding
elements, then PyMeld will be probably a better way.


I dunno; DOM-style engines work fine even for simple jobs, while the
big macro-style engines can do a lot of fancy stuff too. I think the
major difference is that the DOM-style engines do things much more
simply. For example, HTMLTemplate's codebase, API and documentation are
maybe a tenth the size of Cheetah's. Rather than trying to provide
every possible feature users might ever want themselves (c.f.
Greenspun's Tenth Rule), they keep out of the way as much as possible
and let the original language provide for the user's needs.

HTH

--

[1] http://freespace.virgin.net/hamish.s...ltemplate.html

Jan 31 '06 #10
I would like to give a few more specifics about my "benchmarking". The
web page had about 10 simple fields and a table of roughly 30 table
rows. The method of generation the table rows was exactly the same as
the example in the PyMeld documentation ie you create the Meld, you
make a copy of a prototypr row instance, you generate your new rows,
which may have replaceable fields inside them, and finally you replace
the single prototype row with all the new rows you have generated. The
same test using the same data with Cheetah and native templates
resulted in (on an oldish 600Mhz box):
PyMeld: +-300ms
Cheetah: +-30ms
Native %s: +- 5ms
On newer hardware obviously the times will be very different but this
particular application has to be able to scale to very large tables.
I did not try PyMeldLite because the HTML I am using is exactlty that:
HTML and not XHTML. Again I am not criticising PyMeld, I love its
simplicity and clean api and the code is easily understandable so I
will probably take a look at it again at some time. Also its great that
it works with any snippet of HTML code it does not even have to be
valid HTML and I get a lot of invalid HTML from the designers.
Regarding Cheetah, I was using Cheetah 1.0. There is a 2.0 Release
Candidate out but I didnt want to be using anything in RC status for a
production site.

Jan 31 '06 #11

[thakadu]
The method of generation the table rows was exactly the same as
the example in the PyMeld documentation


Did you try using toFormatString() to speed it up? See
http://www.entrian.com/PyMeld/doco.html

--
Richie Hindle
ri****@entrian.com
Jan 31 '06 #12
has
thakadu wrote:
I did not try PyMeldLite because the HTML I am using is exactlty that:
HTML and not XHTML.
FWIW, HTMLTemplate is pretty lax and not restricted to XHTML. The only
XML-ish requirement is that elements need to be properly closed if
they're to be used as template nodes, e.g. <p node="con:desc">...</p>
and <img node="con:foto" />, not <p node="con:desc">... and <img
node="con:foto">. Otherwise, it should cope with pretty much any old
markup as long as HTMLParser can make sense of it.
and I get a lot of invalid HTML from the designers


You might consider throwing HTMLTidy <http://tidy.sourceforge.net/> at
it.

Jan 31 '06 #13
Yes I looked at that but I did not benchmark it. Basically it seems to
convert the Meld or part of a Meld into a %s template in any case and I
already knew that %s performace was very good. So if I had used PyMeld
combined with %s then sure it would be much faster but I wanted to
benchmark a pure PyMeld approach against a pure %s approach.

You are right thought that this could significantly speed things up if
one was prepared to use a dual approach (less clean but I would still
use it if it was convenient).

Jan 31 '06 #14
I haven't got around to trying HTMLTemplate yet but it is on my list of
things to do. It would be great to see how it compares in perfomance
and simplicity to PyMeld and other DOM approaches.

Jan 31 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2550
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
2516
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
2145
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
12
1947
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
16
16202
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
1615
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
25
3291
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
2598
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
4453
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
1695
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.