8. Using OOP to program a website

One the the most powerful feature of CherryPy is that you can really use an object oriented approach to "program" your website.

When you look at a complex website, you'll realize that some parts have a lot in common:

In both cases, OOP provides an elegant solution to the problem and minimizes the amount of code that is required to implement the solution.

To show you how this can be done, we'll create a website that comes in two versions: the English version and the French version. Not only do the text and labels change, but also the colors and the way modules are displayed.

Enter the following code for the website:

#######################
CherryClass Airline abstract:
#######################
function:
    def localize(self, stri):
        return self.dictionnary.get(stri, stri)
mask:
    def header(self):
        <html><body>
            <center>
                <H1 py-eval="self.localize('Welcome to CherryPy airline')"></H1>
                <div py-if="self==airlineFrench">
                    <a py-attr="request.base+'/airlineEnglish/index'" href="">
                        Click here for English version
                    </a>
                </div><div py-else>
                    <a py-attr="request.base+'/airlineFrench/index'" href="">
                        Cliquez ici pour la version française
                    </a>
                </div>
                <br><br><br><br>
    def footer(self):
            </center>
        </body></html>
    def squareWithText(self, title, text):
        <table border=0 cellspacing=0 cellpadding=1 width=200><tr>
            <td py-attr="self.borderColor" bgColor="">
                <table border=0 cellspacing=0 cellpadding=5><tr>
                    <td py-attr="self.insideColor" bgColor=""
                        align=center width=198 py-eval="'<b>%s</b><br><br>%s'%(title,text)">
                    </td>
                </tr></table>
            </td>
        </tr></table>
view:
    def bookAFlight(self):
        page=self.header()
        page+=self.squareWithText(self.localize('Booking a flight'),
            self.localize('To book a flight, think about where you want to go, and you should dream about it tonight'))
        page+=self.footer()
        return page


#######################
CherryClass AirlineFrench(Airline):
#######################
variable:
    insideColor='#FFFF99'
    borderColor='#FF6666'
    dictionnary={
        'Welcome to CherryPy airline': 'Bienvenue chez CherryPy airline',
        'Booking a flight': 'Réserver un vol',
        'To book a flight, think about where you want to go, and you should dream about it tonight':
            'Pour réserver un vol, pensez très fort à la destination, et vous devriez en rêver cette nuit'
    }    
view:
    def index(self):
        page=self.header()
        page+=self.squareWithText('Réserver un vol', 'Pour réserver un vol, cliquez sur <a href="%s/bookAFlight">réserver</a>'%self.getPath())+'<br>'
        page+=self.squareWithText('Présentation', 'CherryPy airline est la compagnie qui vous emmène au 7ème ciel')
        page+=self.footer()
        return page

#######################
CherryClass AirlineEnglish(Airline):
#######################
variable:
    insideColor='#00CCFF'
    borderColor='#3333FF'
    dictionnary={}    
view:
    def index(self):
        page=self.header()
        page+=self.squareWithText('Presentation', 'CherryPy airline is the company that will take you to cloud 9')+'<br>'
        page+=self.squareWithText('Book a flight', '<a href="%s/bookAFlight">Click here</a> to book a flight'%self.getPath())
        page+=self.footer()
        return page

#######################
CherryClass Root(AirlineEnglish):
#######################

This program uses a lot of new features of CherryPy. Let's try to understand how it works:

The idea is to use a generic CherryClass (Airline) that contains functions, masks and views that are common to both versions (English and French) or the website. Then we use 2 CherryClasses (AirlineFrench and AirlineEnglish) to implement specificities of each version.

We use two different ways to implement specificities of each version:

This example also shows some new features of CherryPy:

In the next chapter, we'll learn how to split our code in several source files...

See About this document... for information on suggesting changes.