[Tutor] Automated Breadcrumb -- help for a PHP user

Daniel Ehrenberg littledanehren at yahoo.com
Tue Nov 25 22:15:33 EST 2003


Chris Heisel wrote:
> Hi,
> 
> I'm a n00b to python coming from PHP land.
> 
> I'd like to create an automated breadcrumb script
> that would inspect the 
> URL and create an appropriate bread crumb navigation
> set.
> 
> In PHP I'd so something like
> function makeBread($url) {
>      $path_parts = explode('/', $url);
>      echo('<p class="trail">');
>      foreach ($path_parts as $key => $value) {
>          $where_at = $where_at + $value;
>          echo('<a
> href="$where_at">'.$where_at.'</a>');
>      }
> }
> 
If I understand what that does correctly, it's
probably something like this:

def makeBread(url):
    path_parts = url.split('/')
    output = '<p class="trail">'
    where_at = ''
    for value in path_parts:
        where_at += value
        output += '<a href="%s">%s</a>' % (where_at,
where_at)
    return output

> I'd put this function in an external file include()
> it and make a call with:
> <?php
> makeBread($PHP_SELF)
> ?>
> (Disclaimer: this code is really ugly its just an
> example...)
> 
> I'd like to do this in Python and make it a cgi-bin
> file so I could have 
> something like this:
> <!--#include virtual="/cgi-bin/bread.py" -->
> 
> But I'm not sure where to start. How can I pass the
> URL of the page 
> making the include call to the script?
> 
> One its there I'm sure I can muddle through some
> flow control structures 
> and what not to massage the URL into the various
> breadcrumb pieces...
> 
> Any help would be greatly appreciated.
> 
> Thanks in advance,
> 
> Chris

It looks kind of like you're trying to write PHP in
Python. There's nothing wrong with PHP, but it is not
that popular to try to emulate other languages. (If
you still want to, try the replcode module at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52217,
but it doesn't handle CGI automatically.) For advanced
website templating, you should try CherryPy, or Zope's
DTML. DTML does a good job of seperating style and
coding, and CherryPy  is very good object system and
is easy to use. If you still want to use your weird
comment syntax, a regexp would probably be necessary,
but I don't know anything further.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/



More information about the Tutor mailing list