Ophelia: Usage example


Deutsche Fassung

Ophelia's documentation contains a small usage example which has been installed at this site and will be explained in the following.

The example essentially consists of these directories and files:

Assuming an appropriate server configuration (see the installation instructions) it provides the following resources:

Files under static/ will be delivered verbatim:

static/foo/baz.html

<html>
  <head>
    <title>baz</title>
  </head>
  <body>
    <p>
      This page was not built by Ophelia.
    </p>
  </body>
</html>

/foo/baz.html

In contrast, the resource / is assembled from templates. To this end, Ophelia first runs the Python script in ophelia_pages/__init__ and uses the template found in the same file as the basis for the page to be delivered. Script and template are separated by the <?xml?> line:

ophelia_pages/__init__

site = __request__.site

<?xml?>
<!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" xml:lang="en" lang="en">
  <head>
    <link rel="stylesheet" type="text/css" href=""
          tal:attributes="href string:${site}example.css"/>

    <title tal:content="title" />
  </head>

  <body>
    <h1 tal:content="title" />

    <div tal:replace="structure innerslot" />
  </body>
</html>

Since this file serves as the basis for all those pages of the example which are assembled from templates, the contained Python script sets only one context variable needed by all pages: the example's base URL. The context variable title has to be set in another Python script. The content of innerslot is generated from the other templates used by Ophelia itself.

Since the URL / contains no other path segments, the file containing the unique page content is evaluated next: ophelia_pages/index.html. The Python script in this file now must set the title of the page and the template must not contain any further innerslot.

ophelia_pages/index.html

title = "Ophelia example site"

<?xml?>
<p>
  This is the index page of the
  <a href="http://www.thomas-lotze.de/software/ophelia/">Ophelia</a>
  example site located at
  <a href="http://www.thomas-lotze.de/software/ophelia/example/">
    http://www.thomas-lotze.de/software/ophelia/example/</a>.
</p>

<p>
  Take a look at the <a href="foo/">foo</a>, <a href="foo/bar.html">foo/bar</a>,
  and <a href="foo/baz.html">foo/baz</a> pages.
</p>

/

To construct the resource /foo/ Ophelia again needs to evaluate all files named __init__ found on the path: first the file ophelia_pages/__init__, already treated above, and then ophelia_pages/foo/__init__:

ophelia_pages/foo/__init__

<div class="boxed"
     tal:content="structure innerslot" />

This file contains only one template which will be inserted into the main template as innerslot. On the other hand, this template includes an innerslot in turn which will be filled from the next evaluated file. The effect of the template in ophelia_pages/foo/__init__ is to wrap the content of any pages under /foo/ in a <div> element which is given a border using CSS.

The final file contributing to the resource /foo/ is ophelia_pages/foo/index.html. Again, it contains a Python script which sets the page title context variable, and a template without an innerslot:

ophelia_pages/foo/index.html

title = "index page"

<?xml?>
<p>
  This is the index page of the foo folder.
</p>

/foo/

When building the resource /foo/bar.html, Ophelia evaluates the same __init__ files as for /foo/ since the path contains the same directories. Only the final file considered is different:

ophelia_pages/foo/bar.html

import datetime

title = "bar page"
now = datetime.datetime.now()

<?xml?>
<p>
  This is the bar page of the foo folder. The current date and time is
  <span tal:replace="now" />.
</p>

/foo/bar.html

If you compare ophelia_pages/foo/index.html to ophelia_pages/foo/bar.html, you see that Ophelia really relieves you from writing repetitive code. Everything common between /foo/ and /foo/bar.html is written down exactly once in one of the __init__ files.


2015/10/02