cgi, reusing html. common problem?

  • Thread starter John M. Gabriele
  • Start date
J

John M. Gabriele

I'm putting together a small site using Python and cgi.

(I'm pretty new to this, but I've worked a little with
JSP/servlets/Java before.)

Almost all pages on the site will share some common (and
static) html, however, they'll also have dynamic aspects.
I'm guessing that the common way to build sites like this
is to have every page (which contains active content) be
generated by a cgi script, but also have some text files
hanging around containing incomplete html fragments which
you read and paste-in as-needed (I'm thinking:
header.html.txt, footer.html.txt, and so on).

Is that how it's usually done? If not, what *is* the
usual way of handling this?

Thanks,
---John
 
D

Diez B. Roggisch

John said:
I'm putting together a small site using Python and cgi.

(I'm pretty new to this, but I've worked a little with
JSP/servlets/Java before.)

Almost all pages on the site will share some common (and
static) html, however, they'll also have dynamic aspects.
I'm guessing that the common way to build sites like this
is to have every page (which contains active content) be
generated by a cgi script, but also have some text files
hanging around containing incomplete html fragments which
you read and paste-in as-needed (I'm thinking:
header.html.txt, footer.html.txt, and so on).

Is that how it's usually done? If not, what *is* the
usual way of handling this?

The basic idea is correct - but there are sooo many other people that
had the same problem, and thus they creted web-framworks like e.g.
CherryPy or Django or... and then there is ZOPE. Search this group for
webframeworks, and you might get more answers than you wanted :)

Diez
 
F

Fuzzyman

I'm putting together a small site using Python and cgi.

(I'm pretty new to this, but I've worked a little with
JSP/servlets/Java before.)

Almost all pages on the site will share some common (and
static) html, however, they'll also have dynamic aspects.
I'm guessing that the common way to build sites like this
is to have every page (which contains active content) be
generated by a cgi script, but also have some text files
hanging around containing incomplete html fragments which
you read and paste-in as-needed (I'm thinking:
header.html.txt, footer.html.txt, and so on).

Is that how it's usually done? If not, what *is* the
usual way of handling this?

Having a template and inserting dynamic values into it is very common.

You'll have more luck looking for 'python templating systems'.

I use a module called 'embedded code' - which is part of firedrop by
Hans Nowak. See http://www.voidspace.org.uk/python/firedrop2/

Popular templating engines include Cheetah and TAL.

You can also roll your own basic one using the string method
``replace``.

I'm pretty sure their is an entry on the Python.org WIKI about
templating.

All the best,

Fuzzy
http://www.voidspace.org.uk/python
 
?

=?ISO-8859-1?Q?Walter_D=F6rwald?=

John said:
I'm putting together a small site using Python and cgi.

(I'm pretty new to this, but I've worked a little with
JSP/servlets/Java before.)

Almost all pages on the site will share some common (and
static) html, however, they'll also have dynamic aspects.
I'm guessing that the common way to build sites like this
is to have every page (which contains active content) be
generated by a cgi script, but also have some text files
hanging around containing incomplete html fragments which
you read and paste-in as-needed (I'm thinking:
header.html.txt, footer.html.txt, and so on).

Is that how it's usually done? If not, what *is* the
usual way of handling this?

I don't know if it's the *usual* way, but you could give XIST a try
(http://www.livinglogic.de/Python/xist). It was developed for exactly
this purpose: You implement reusable HTML fragments in Python and you
can use any kind of embedded dynamic language (PHP and JSP are supported
out of the box).

Bye,
Walter Dörwald
 
J

John M. Gabriele

The basic idea is correct - but there are sooo many other people that
had the same problem, and thus they creted web-framworks like e.g.
CherryPy or Django or... and then there is ZOPE. Search this group for
webframeworks, and you might get more answers than you wanted :)

Diez


Thanks Diez. Glad to hear I'm on the right track. :)

From poking around, it looks to me like these Python web
frameworks are to Python as JSP is to Java.

I really don't want to use a "templating language" (a la
JSP) -- I was hoping to just stick with straight Python
and then also html + css. Though I've heard good things
about CherryPy.

Looks like mod_python also comes with it's own solution too:
http://www.onlamp.com/pub/a/python/2004/02/26/python_server_pages.html
 
J

John M. Gabriele

Having a template and inserting dynamic values into it is very common.

You'll have more luck looking for 'python templating systems'.

I use a module called 'embedded code' - which is part of firedrop by
Hans Nowak. See http://www.voidspace.org.uk/python/firedrop2/

Popular templating engines include Cheetah and TAL.

You can also roll your own basic one using the string method
``replace``.


Thanks for the reply Fuzzy.

I'm going to try rolling my own. I found this *great* article:
http://www.devshed.com/index2.php?option=content&task=view&id=198&pop=1&page=0&hide_js=1

and it shows pretty much exactly what I think I want: separate
html files containing fragments of a complete page, then some
python code to read in the html fragment, and replace your
generated code with some placeholder.

I'm pretty sure their is an entry on the Python.org WIKI about
templating.

Whoops. I ended up hitting this page first:
http://wiki.python.org/moin/CgiScripts
and now I'm sticking with it. :)

I like CGI. I want to keep things as simple as possible. :)

---John
 
J

John M. Gabriele

I don't know if it's the *usual* way, but you could give XIST a try
(http://www.livinglogic.de/Python/xist). It was developed for exactly
this purpose: You implement reusable HTML fragments in Python and you
can use any kind of embedded dynamic language (PHP and JSP are supported
out of the box).

Bye,
Walter Dörwald

Thanks Walt. :) Though, it seems simpler to me to just stick with some
plain vanilla static html, and pull that in to my cgi scripts as
necessary.

---John
 
S

Steve Holden

John said:
I'm putting together a small site using Python and cgi.

(I'm pretty new to this, but I've worked a little with
JSP/servlets/Java before.)

Almost all pages on the site will share some common (and
static) html, however, they'll also have dynamic aspects.
I'm guessing that the common way to build sites like this
is to have every page (which contains active content) be
generated by a cgi script, but also have some text files
hanging around containing incomplete html fragments which
you read and paste-in as-needed (I'm thinking:
header.html.txt, footer.html.txt, and so on).

Is that how it's usually done? If not, what *is* the
usual way of handling this?
There are a million ways to solve this particular problem, despite
Python's "TSBOAPOOOWTDI" (see "import this") philosophy (because the
philosophy is addressing primitive programming rather than application
frameworks).

You could do something as simple as writing a module "webPage" that
defines a function page(path, content) that does something along the
lines of:

def page(path, content):
return """\
<html>
<title>If you want titles, add another argument</title>
</head>
<body>
<this would be code to build a nav bar, omitting a hypertext link for
the path given as an argument - I'm just ignoring it in this example>
%s
</body>
</html>
""" % content

Then in your generation routines you can build up content in the
traditional way by generating individual fragments of HTML and appending
them to a list. So you start with

content = []

then for every fragment you generate you do

content.append(fragment)

and finally your content is generated with something like

content = webPage.page("siteroot/subdir/page1.html", "".join(content))

If you don't care that a page contains a navbar link to itself (a sign
of web immaturity, but by no means inexcusable) then you don't even need
to pass the page's path into the function.

Hope this gives you a few ideas. This problem has been considered by
many people, but clearly no outstanding solution has yet been devised,
otherwise we'd all be using it.

regards
Steve
 
J

John M. Gabriele

John said:
[snip]

Is that how it's usually done? If not, what *is* the
usual way of handling this?
There are a million ways to solve this particular problem, despite
Python's "TSBOAPOOOWTDI" (see "import this") philosophy (because the
philosophy is addressing primitive programming rather than application
frameworks).

Yes. :)
You could do something as simple as writing a module "webPage" that
defines a function page(path, content) that does something along the
lines of:

def page(path, content):
return """\
<html>
<title>If you want titles, add another argument</title>
</head>
<body>
<this would be code to build a nav bar, omitting a hypertext link for
the path given as an argument - I'm just ignoring it in this example>
%s
</body>
</html>
""" % content

Ah yes, I see. If the content is completely static, you
can use a text file (html fragment) and just read it and
paste it in, but for fragments that are *mostly* static,
I really like that idea to use a module as you suggest.

[snip]

Hope this gives you a few ideas. This problem has been considered by
many people, but clearly no outstanding solution has yet been devised,
otherwise we'd all be using it.

regards
Steve

Thanks Steve!

---John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top