By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

cgi, reusing html. common problem?

P: n/a
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

--
--- if contacting via email, remove zees ---
Sep 1 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
John M. Gabriele wrote:
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
Sep 1 '05 #2

P: n/a
On Thu, 01 Sep 2005 03:10:07 -0400, "John M. Gabriele"
<jo************@yahooz.com> wrote:
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

Thanks,
---John

Sep 1 '05 #3

P: n/a
John M. Gabriele wrote:
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
Sep 1 '05 #4

P: n/a
On Thu, 01 Sep 2005 09:20:51 +0200, Diez B. Roggisch wrote:
John M. Gabriele wrote:
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

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/2...ver_pages.html

--
--- if contacting via email, remove zees ---
Sep 1 '05 #5

P: n/a
On Thu, 01 Sep 2005 13:12:14 +0100, Fuzzyman wrote:
On Thu, 01 Sep 2005 03:10:07 -0400, "John M. Gabriele"
<jo************@yahooz.com> wrote:
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``.

Thanks for the reply Fuzzy.

I'm going to try rolling my own. I found this *great* article:
http://www.devshed.com/index2.php?op...ge=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


All the best,

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

Thanks,
---John


--
--- if contacting via email, remove zees ---
Sep 2 '05 #6

P: n/a
On Thu, 01 Sep 2005 19:10:14 +0200, Walter Dörwald wrote:
John M. Gabriele wrote:
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


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

--
--- if contacting via email, remove zees ---
Sep 2 '05 #7

P: n/a
John M. Gabriele wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 2 '05 #8

P: n/a
On Thu, 01 Sep 2005 20:57:56 -0500, Steve Holden wrote:
John M. Gabriele wrote:
[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

--
--- if contacting via email, remove zees ---
Sep 2 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.