Python embedded like PHP

Andrew McNamara andrewm at object-craft.com.au
Tue Mar 12 21:54:46 EST 2002


>Are there any projects out there that allow you to embed python in HTML
>like PHP?  I like the PHP-style of embedding the bits of program in HTML,
>but I'd like to use python...
>Something like:
>
><html>
><?python
>	print "<ul>"
>	for reptile in ["Crocodile", "Python", "Iguana", "Tortoise"]:
>		print "<li> %s </li>" % reptile
>	print "</ul>"
>?>
></html>

You may want to consider Albatross:

    http://www.object-craft.com.au/projects/albatross

Albatross encourages (but doesn't enforce) the separation of presentation
and logic. You might do something like this in Albatross:

    <ul>
    <al-for iter="reptile" expr='["Crocodile", "Python", "Iguana", "Tortoise"]'>
      <li><al-value expr="reptile.value()" whitespace></li>
    </al-for>
    </ul>

An example Albatross CGI application that demonstrates this is:

    #!/usr/bin/python

    import albatross

    ctx = albatross.SimpleContext('.')
    ctx.locals.reptiles = ["Crocodile", "Python", "Iguana", "Tortoise"]
    template = ctx.load_template("reptiles.html")
    template.to_html(ctx)

    print "Content-Type: text/html"
    print
    ctx.flush_content()

And "reptiles.html" would contain: 

    <ul>
      <al-for iter="reptile" expr="reptiles">
        <li><al-value expr="reptile.value()" whitespace></li>
      </al-for>
    </ul> 

Albatross also gives you an complete templating language, and client and
server side sessions. The extensive manual is available online as HTML at:

    http://www.object-craft.com.au/projects/albatross/albatross/

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/




More information about the Python-list mailing list