Karrigell/Build a simple HTML page

From Wikibooks, open books for an open world
Jump to: navigation, search

There are 3 ways you can build HTML in a script

  1. Raw HTML[edit]

    The first one is to return a string with HTML tags, like this :

    def index():
        return "<html><body>Hello world</body><html>"
    
  2. Templates[edit]

    For a document that holds mainly static HTML, with only a few dynamic values, you can use templates. For instance, create a file called page.tmpl with:

    <html>
    <body>
    Hello world
    </body>
    </html>
    

    and save it in the same folder as index.py. Then modify index.py like this:

    def index():
        return Template("page.tmpl")
    

    The built-in function Template is available in all the scripts managed by Karrigell. It reads the content from the file located at the relative url page.tmpl and returns it. More on the templating system later.

  3. The HTMLTags module[edit]

    The third way, recommended when the page is mostly made of dynamic content, is to use the HTMLTags module, included in the package :

    def index():
        return HTML(BODY("Hello world"))
    

    The HTMLTags module defines classes for all valid HTML tags, in uppercase letters : the classes for tags <html> and <body> are HTML and BODY (surprise !). These classes are available in the scripts managed by Karrigell

    The function index() returns an instance of the class HTML, called with an instance of the class BODY ; the server returns its string representation, which is the HTML code <html><body>Hello world</body><html>