Open main menu

Wikibooks β

Karrigell/Build a simple HTML page

< Karrigell

There are 3 ways you can build HTML in a script

  1. Raw HTMLEdit

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

    def index():
        return "&lt;html&gt;&lt;body&gt;Hello world&lt;/body&gt;&lt;html&gt;"
  2. TemplatesEdit

    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:

    Hello world

    and save it in the same folder as Then modify 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 moduleEdit

    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>