468,532 Members | 1,904 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

HTML generation vs PSP vs Templating Engines

Hello everybody,

I am in the process of writing my very first web application in Python,
and I need a way to
generate dynamic HTML pages with data from a database. I have to say I
am overwhelmed
by the plethora of different frameworks, templating engines, HTML
generation tools etc that
exist. After some thought I decided to leave the various frameworks
aside for the
time being and use mod_python.publisher along with some means of
generating HTML on
the fly.
Could someone that has used all the different ways mentioned above for
dynamic HTML
content, suggest what the pros and cons of the different methods are?

Thank you very much in advance

Panos

Nov 22 '05 #1
18 4693
pk**********@gmail.com wrote:
Hello everybody,

I am in the process of writing my very first web application in Python,
and I need a way to
generate dynamic HTML pages with data from a database. (snip) After some thought I decided to leave the various frameworks
aside for the
time being and use mod_python.publisher


I know this is not exactly an answer to your question, but have you
tried Django ?
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 22 '05 #2
pk**********@gmail.com wrote:
Hello everybody,

I am in the process of writing my very first web application in Python,
and I need a way to
generate dynamic HTML pages with data from a database. (snip) After some thought I decided to leave the various frameworks
aside for the
time being and use mod_python.publisher


I know this is not exactly an answer to your question, but have you
tried Django ?
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 22 '05 #3
> After some thought I decided to leave the various frameworks
aside for the time being and use mod_python.publisher along with some
means of generating HTML on the fly.


I kind of like KID templates the most, you can easyly work with them in any
HTML authoring software, they are easy to use (prety much pythonic).
--
damjan
Nov 22 '05 #4
> After some thought I decided to leave the various frameworks
aside for the time being and use mod_python.publisher along with some
means of generating HTML on the fly.


I kind of like KID templates the most, you can easyly work with them in any
HTML authoring software, they are easy to use (prety much pythonic).
--
damjan
Nov 22 '05 #5
I just do the following:

I store the form data as a pickeled dictionary. Then I create my
HTML form with something like this:

HTMLout="""<HTML><BODY>......
.......
<INPUT NAME="field1" value=%(field1)s>
<INPUT NAME="field2" value=%(field2)s>
<INPUT NAME="field3" value=%(field3)s>'''

where the field1, field2 etc era the fields on my form.

Then finally:

print HTMLout % dict

where dict has all the values that I previously pickled.
You have to do some additional trickery for <SELECT> type fields
and checkboxes and so on but its really not difficult.

Nov 22 '05 #6
I just do the following:

I store the form data as a pickeled dictionary. Then I create my
HTML form with something like this:

HTMLout="""<HTML><BODY>......
.......
<INPUT NAME="field1" value=%(field1)s>
<INPUT NAME="field2" value=%(field2)s>
<INPUT NAME="field3" value=%(field3)s>'''

where the field1, field2 etc era the fields on my form.

Then finally:

print HTMLout % dict

where dict has all the values that I previously pickled.
You have to do some additional trickery for <SELECT> type fields
and checkboxes and so on but its really not difficult.

Nov 22 '05 #7
With Karrigell (http://karrigell.sf.net/), all you need to know is
Python and HTML.
No templates, no python-like or special languages, only pure and simple
python.

You can embedd python into html or, if it better suits your programming
tyle, you can embed html into python. Why don't you give it a try?

Nov 22 '05 #8
With Karrigell (http://karrigell.sf.net/), all you need to know is
Python and HTML.
No templates, no python-like or special languages, only pure and simple
python.

You can embedd python into html or, if it better suits your programming
tyle, you can embed html into python. Why don't you give it a try?

Nov 22 '05 #9
has
pk**********@gmail.com wrote:
Could someone that has used all the different ways mentioned above for
dynamic HTML
content, suggest what the pros and cons of the different methods are?


Not used them all - as you say, there's a plethora of options - but to
give you a general idea of the territory...

First choice you have is between HTML generators vs. HTML templating
systems:

- HTML generators create all markup from scratch. Useful when producing
arbitrary markup/layout whose entire structure must be determined
programmatically, e.g. applying paragraph and character styling to a
big chunk of text, creating arbitrary HTML form layouts based on other
input. Examples: htmlgen (crusty, HTML 3.1-era), Nevow's Stan engine.

- HTML templates insert individual items of data into a mostly static
block of HTML markup written in advance by e.g. a graphic designer.
Useful when creating documents that are fairly regular in structure -
most fall into this category - as it's much easier to create the
repetitious parts using standard HTML editing tools than write code to
produce it all programmatically.

Sometimes you might combine the two methods, using an HTML generator to
create sections of markup from arbitrary input which is then inserted
into a full-page template to produce the finished HTML document.
Assuming a templating-based solution is the appropriate choice for you
(which it most likely is), there are three general approaches to choose
from:

1. Systems that embed markup in code. This is a fairly small category.
It's fairly programmer-friendly since you've direct access to all
language features, but hostile to web designers as you have to pull
your HTML markup apart and insert it into program code to produce the
final template, making it a pain to modify that markup later. Examples:
ad-hoc solutions using Python's string interpolation, the Quixote
framework's built-in templating support, Karrigell (though it supports
some aspects of approach 2 as well).

2. Systems that embed code in markup. This is the most common category
with a fair amount of variety and capabilities to choose from. Lots of
obviously PHP/ASP-inspired designs. Two sub-categories: systems that
embed standard Python code, e.g. PSP, and systems that embed a custom
language, e.g. Cheetah. Some provide no restrictions on what you can
get up to within embedded code, others restrict functionality to
enforce a strict separation between presentation logic and model logic.
Embedding styles also vary: some mix code statements and markup
directly (e.g. Cheetah), some embed code statements in special <%...%>
style tags (e.g. PSP), some hide all code within HTML tag attributes
(e.g. TAL, Kid), providing varying degrees of designer-friendliness as
a result.

3. DOM-style systems. This is a more recent arrival and a smaller
category. These systems completely separate markup from presentation
logic. Selected HTML elements are flagged using specific named tag
attributes (e.g. id="somename") or simple compiler directives, e.g.
(nevow:render="somename"); the template is then compiled into a simple
templating-oriented (i.e. not w3c) DOM, allowing these elements to be
manipulated programmatically by standard Python scripts. Very designer
and developer friendly, although less experienced programmers may find
the higher level of abstraction involved a bit offputting. Examples:
PyMeld, HTMLTemplate [1], Nevow.Render.
For a fairly extensive list of available systems, see
<http://wiki.python.org/moin/WebProgramming>. Unfortunately they're not
well categorised there, but I can't think of a better, up-to-date list
off the top of my head and most will provide decent overviews so it
shouldn't take too long to take a quick look at each.

Note that some templating engines are embedded into larger web
programming frameworks and may or may not be usable independently.
Conversely, some web frameworks may be coupled to a specific templating
system, while others allow you to choose your own.

HTH

has

--
[1] Disclaimer: I wrote HTMLTemplate. Also, shameless plug:
<http://freespace.virgin.net/hamish.sanderson/htmltemplate.html>

Nov 22 '05 #10
has
pk**********@gmail.com wrote:
Could someone that has used all the different ways mentioned above for
dynamic HTML
content, suggest what the pros and cons of the different methods are?


Not used them all - as you say, there's a plethora of options - but to
give you a general idea of the territory...

First choice you have is between HTML generators vs. HTML templating
systems:

- HTML generators create all markup from scratch. Useful when producing
arbitrary markup/layout whose entire structure must be determined
programmatically, e.g. applying paragraph and character styling to a
big chunk of text, creating arbitrary HTML form layouts based on other
input. Examples: htmlgen (crusty, HTML 3.1-era), Nevow's Stan engine.

- HTML templates insert individual items of data into a mostly static
block of HTML markup written in advance by e.g. a graphic designer.
Useful when creating documents that are fairly regular in structure -
most fall into this category - as it's much easier to create the
repetitious parts using standard HTML editing tools than write code to
produce it all programmatically.

Sometimes you might combine the two methods, using an HTML generator to
create sections of markup from arbitrary input which is then inserted
into a full-page template to produce the finished HTML document.
Assuming a templating-based solution is the appropriate choice for you
(which it most likely is), there are three general approaches to choose
from:

1. Systems that embed markup in code. This is a fairly small category.
It's fairly programmer-friendly since you've direct access to all
language features, but hostile to web designers as you have to pull
your HTML markup apart and insert it into program code to produce the
final template, making it a pain to modify that markup later. Examples:
ad-hoc solutions using Python's string interpolation, the Quixote
framework's built-in templating support, Karrigell (though it supports
some aspects of approach 2 as well).

2. Systems that embed code in markup. This is the most common category
with a fair amount of variety and capabilities to choose from. Lots of
obviously PHP/ASP-inspired designs. Two sub-categories: systems that
embed standard Python code, e.g. PSP, and systems that embed a custom
language, e.g. Cheetah. Some provide no restrictions on what you can
get up to within embedded code, others restrict functionality to
enforce a strict separation between presentation logic and model logic.
Embedding styles also vary: some mix code statements and markup
directly (e.g. Cheetah), some embed code statements in special <%...%>
style tags (e.g. PSP), some hide all code within HTML tag attributes
(e.g. TAL, Kid), providing varying degrees of designer-friendliness as
a result.

3. DOM-style systems. This is a more recent arrival and a smaller
category. These systems completely separate markup from presentation
logic. Selected HTML elements are flagged using specific named tag
attributes (e.g. id="somename") or simple compiler directives, e.g.
(nevow:render="somename"); the template is then compiled into a simple
templating-oriented (i.e. not w3c) DOM, allowing these elements to be
manipulated programmatically by standard Python scripts. Very designer
and developer friendly, although less experienced programmers may find
the higher level of abstraction involved a bit offputting. Examples:
PyMeld, HTMLTemplate [1], Nevow.Render.
For a fairly extensive list of available systems, see
<http://wiki.python.org/moin/WebProgramming>. Unfortunately they're not
well categorised there, but I can't think of a better, up-to-date list
off the top of my head and most will provide decent overviews so it
shouldn't take too long to take a quick look at each.

Note that some templating engines are embedded into larger web
programming frameworks and may or may not be usable independently.
Conversely, some web frameworks may be coupled to a specific templating
system, while others allow you to choose your own.

HTH

has

--
[1] Disclaimer: I wrote HTMLTemplate. Also, shameless plug:
<http://freespace.virgin.net/hamish.sanderson/htmltemplate.html>

Nov 22 '05 #11
> No templates, no python-like or special languages, only pure and simple python.
You can embedd python into html or, if it better suits your programming
style, you can embed html into python. Why don't you give it a try?


I dislike embedding code or html in each other, apart from the
'impurity' of mixing code and user interface it makes them inseparable.

Using templates means that the code can work with different templates,
and this should be seamless, it also means that different code can be
used with the templates, for example if different languages are used.

The main advantage, for me, is that different outputs formats can be
created without changing the code. If the user wants a set of data in a
table then the html template is used, if they want a csv file of the
data, that is just a different template name. A printed report: same
code just a different template name. XML, simple text, postscript,
EDIFACT file, all done with same code, different template. Just arrange
for the name of the template file to be a parameter on the URL and the
various outputs can be selected by the user.

I did, however, write my own templating module, they are quite easy to
do.

Nov 22 '05 #12
> No templates, no python-like or special languages, only pure and simple python.
You can embedd python into html or, if it better suits your programming
style, you can embed html into python. Why don't you give it a try?


I dislike embedding code or html in each other, apart from the
'impurity' of mixing code and user interface it makes them inseparable.

Using templates means that the code can work with different templates,
and this should be seamless, it also means that different code can be
used with the templates, for example if different languages are used.

The main advantage, for me, is that different outputs formats can be
created without changing the code. If the user wants a set of data in a
table then the html template is used, if they want a csv file of the
data, that is just a different template name. A printed report: same
code just a different template name. XML, simple text, postscript,
EDIFACT file, all done with same code, different template. Just arrange
for the name of the template file to be a parameter on the URL and the
various outputs can be selected by the user.

I did, however, write my own templating module, they are quite easy to
do.

Nov 22 '05 #13
I meant that it is not strictly necessary to use templates in
Karrigell, although you can use Cheetah if you want.
I'm not used to templates mainly because I'm familiar with the way PHP
works and, for simple dynamic sites like those I work on, this is the
simpliest approach.
Another reason is that I'm a little bit lazy for learning yet another
language, be it a python-like one or a template one.

Also, I feel that Python is easy enough for having to resort to another
language.
I usually create my pages with Dreamweaver or any other visual tool,
and once the design is ready, I add a few python lines to it which,
combined with modules that are separated from the html code, work
wonders on my sites.

Nov 22 '05 #14
I meant that it is not strictly necessary to use templates in
Karrigell, although you can use Cheetah if you want.
I'm not used to templates mainly because I'm familiar with the way PHP
works and, for simple dynamic sites like those I work on, this is the
simpliest approach.
Another reason is that I'm a little bit lazy for learning yet another
language, be it a python-like one or a template one.

Also, I feel that Python is easy enough for having to resort to another
language.
I usually create my pages with Dreamweaver or any other visual tool,
and once the design is ready, I add a few python lines to it which,
combined with modules that are separated from the html code, work
wonders on my sites.

Nov 22 '05 #15
has
ri****@Azonic.co.nz wrote:
I dislike embedding code or html in each other, apart from the
'impurity' of mixing code and user interface it makes them inseparable.

Using templates means that the code can work with different templates,
and this should be seamless, it also means that different code can be
used with the templates, for example if different languages are used.


This seems to contradict your statement that you dislike 'embedding
code or html in each other', since the scenarios you describe still
involve embedding presentation logic in markup. (The only templating
systems that *completely* separate logic from markup are the DOM-style
ones.)

I assume what you really meant is that you don't like embedding *model*
logic in markup, which is generally good design practice. However,
templating systems that use Python for presentation logic (Karrigell,
PSP, Nevow, HTMLTemplate, etc) certainly don't force you to put model
logic into the template; you are quite entitled to keep that separate
as per MVC. They just don't *enforce* the model logic/presentation
logic separation as some authoritarian custom language-based systems
do.

HTH

Nov 22 '05 #16
has
ri****@Azonic.co.nz wrote:
I dislike embedding code or html in each other, apart from the
'impurity' of mixing code and user interface it makes them inseparable.

Using templates means that the code can work with different templates,
and this should be seamless, it also means that different code can be
used with the templates, for example if different languages are used.


This seems to contradict your statement that you dislike 'embedding
code or html in each other', since the scenarios you describe still
involve embedding presentation logic in markup. (The only templating
systems that *completely* separate logic from markup are the DOM-style
ones.)

I assume what you really meant is that you don't like embedding *model*
logic in markup, which is generally good design practice. However,
templating systems that use Python for presentation logic (Karrigell,
PSP, Nevow, HTMLTemplate, etc) certainly don't force you to put model
logic into the template; you are quite entitled to keep that separate
as per MVC. They just don't *enforce* the model logic/presentation
logic separation as some authoritarian custom language-based systems
do.

HTH

Nov 22 '05 #17
>> Using templates means that the code can work with different templates,
and this should be seamless, it also means that different code can be
used with the templates, for example if different languages are used.
This seems to contradict your statement that you dislike 'embedding
code or html in each other', since the scenarios you describe still
involve embedding presentation logic in markup. (The only templating
systems that *completely* separate logic from markup are the DOM-style
ones.)


Perhaps that is why I implemented my own mechanisms for templating. My
templates contain no logic at all and can be used from my Python
programs, Cobol or C equally well.

Contrarywise, my Python programs choose at runtime the required
template (depending on configuration, user request or other) and then
the same program code will output HTML, XML, EDIFACT, CSV, printed
report or other dependant entirely on the content of the template file.

Nov 22 '05 #18
>> Using templates means that the code can work with different templates,
and this should be seamless, it also means that different code can be
used with the templates, for example if different languages are used.
This seems to contradict your statement that you dislike 'embedding
code or html in each other', since the scenarios you describe still
involve embedding presentation logic in markup. (The only templating
systems that *completely* separate logic from markup are the DOM-style
ones.)


Perhaps that is why I implemented my own mechanisms for templating. My
templates contain no logic at all and can be used from my Python
programs, Cobol or C equally well.

Contrarywise, my Python programs choose at runtime the required
template (depending on configuration, user request or other) and then
the same program code will output HTML, XML, EDIFACT, CSV, printed
report or other dependant entirely on the content of the template file.

Nov 22 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Michele Simionato | last post: by
14 posts views Thread by Philippe C. Martin | last post: by
8 posts views Thread by John M. Gabriele | last post: by
reply views Thread by pkassianidis | last post: by
10 posts views Thread by Sullivan WxPyQtKinter | last post: by
15 posts views Thread by lxyone | last post: by
5 posts views Thread by jmDesktop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.