You are not logged in.

#1 2012-06-05 06:12:04

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

[Solved] Python CGI - how to "include" like in PHP

Hi.

When I code PHP I like to use this template system. A short instance would consist of 2 files: "index.php" and "template.php".

template.php:

<?php

echo "<h1>$header</h1>";
echo "<p>$text</p>";

?>

index.php:

<?php

$header = "this is a header";
$text = "this is a text";

include "template.php";

?>

So I can use one template for lots of pages - same layout but different vars.

I cannot find how to do this in Python CGI.

page.cgi:

#!/usr/bin/python

import os, cgi

x = "This is the page text."
eval(open('template.cgi').read())

template.cgi

print("Content-type: text/html\n")
print(x)

This gives me error 500. I am not surprised, this mail.python.org page says:

> I can't figure out how one would approach this in Python (i'm using
> mod_python). There's no equivalent to the "include" function from
> PHP.

Correct, the concept doesn't exist because you are working
with vanilla CGI rather than server scripting.

But maybe there is a way to make something similar?

Last edited by Mr. Alex (2012-06-05 17:52:42)

Offline

#2 2012-06-05 09:59:05

Cdh
Member
Registered: 2009-02-03
Posts: 1,098

Re: [Solved] Python CGI - how to "include" like in PHP

Well, first a meta question: Do you need it to be vanilla CGI + python? There are (micro)frameworks like bottle that do templates and webstuff for you more easily.


฿ 18PRsqbZCrwPUrVnJe1BZvza7bwSDbpxZz

Offline

#3 2012-06-05 12:45:58

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

Re: [Solved] Python CGI - how to "include" like in PHP

Yes, I prefer vanilla CGI. I just don't get those frameworks.

Offline

#4 2012-06-05 14:11:45

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [Solved] Python CGI - how to "include" like in PHP

Template (index.html.tpl)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Main Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        h1 {{
            margin: auto;
        }}
    </style>
</head>
<body>
    <h1>Admin</h1>
    <ul>
        <li><a href="{}">Monthly Report</a></li>
        <li><a href="{}">Inventory Zip Files</a></li>
        <li><a href="{}">Order Reports</a> -- Last 7 days</li>
        <li><a href="{}">Notes/TODO/Information</a></li>
        <li><a href="{}">EDI Orders</a></li>
    </ul>
</body>
</html>

index.py :

#!/usr/bin/env python2
def main():
    template = "index.html.tpl"
    pages = ("monthly", "zips", "reports", "vimwiki", "edi.py")
    sitename = "/"
    pages = ["{}{}".format(sitename, pages) for i in pages]
    tpl = "".join(open(template,'r').readlines())
    print 'Content-type: text/html\n\n' + tpl.format(*pages)

if __name__ == '__main__':
    main()

Maybe not the best way...but it works for me smile Notice the double {{ }} in the template where you want literal brackets, not placeholders.

Scott

Offline

#5 2012-06-05 14:16:05

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

Re: [Solved] Python CGI - how to "include" like in PHP

Wow, that was not obvious. big_smile Thanks.

Offline

#6 2012-06-05 14:24:45

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: [Solved] Python CGI - how to "include" like in PHP

Instead of using indexed variables, you can use a dictionary:

...
<span>Hello, my name is {name}.</span>
...
...
environment = {"name": "The Name"}
print 'Content-type: text/html\n\n' + tpl.format(**environment)

Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

#7 2012-06-05 17:28:52

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [Solved] Python CGI - how to "include" like in PHP

@Steblien: thanks! Good idea.

@Mr. Alex: nope, not obvious except in hindsight...it took awhile for me to figure out too! Don't forget to mark your post [Solved] smile

Scott

Offline

Board footer

Powered by FluxBB