Embedded code in Kaa

A short overview of what is possible with embedded Python code.

Generic Python code

You can, at least theoretically, embed any Python code in your HTML templates and messages. Just put the code between <% %> (for expressions) or <# #> (for statements and code blocks).

For expressions, the expression is evaluated and the result is embedded in the final HTML.
For statements and code blocks, all code is executed, and anything printed to stdout is displayed in the HTML. Anything else does not affect the HTML.

For example, the following Python expression will evaluate to '4':

  <% 2+2 %>
And this code will embed the text 'Hello, world!' in HTML:
  <#
    print "Hello, world!"
  #>
Of course, more complex code is possible... the sky is the limit. For multiple lines of code, make sure you have the indentation right, though.

From the code, you can access two important objects: blog and entry.

The blog object

The blog object is an instance of the Blog class, representing the weblog, and storing all kinds of information about it, like the title, entries for the current page, name of the current page, etc. You'll want to use this in your page template.

For a quick start, here are the attributes that are most important:

blog.title

The title of the weblog, obviously. Use this in your <title> tag, or just to display the title anywhere you want (page header, etc).
blog.messages()
A very important method: it embeds the messages (entries) in the page. IOW, it's a way of saying "messages go here". Note that every message is wrapped in its own template; see the entry template for that.
Which entries are displayed on which page, depends on the situation and the type of page being generated. Your "start page" can display the N most recent entries, archive pages will display entries of certain dates, and for custom pages you can select any entry you want.
Other, less important attributes include:

blog.current_page

The name (URL) of the current page. This is not a fully qualified URL with "http://" and all that, by the way; it's just a page name.
blog.next_page, blog.previous_page
These are only used on archive pages. Use them to point to the next or previous page; this obviously doesn't make sense for single pages.
blog.categories
A list of categories (as strings) that this weblog has.
blog.archive_list()
Returns an archive list, in HTML. This is used to embed ready-to-go HTML that displays a list of archive files, separated by <br> tags.
May go away or be moved in future versions, since it's too specific for my taste.
blog.filename
The name of the front page.
Maybe this name should change...
blog.description
The description of the weblog, as entered by you in Kaa.
blog.get_permalink(entry_id)
Generates a permalink for the given entry, using its entry id.
XXX Is this the same as entry.anchor? Should it be?

The entry object

The entry object is an instance of the Entry class, storing data about weblog entry, obviously by the name. Use this in the entry template.

entry.title

Speaks for itself.
entry.insert_date
The date/time you inserted the entry, represented as an ISO date string (YYYY-MM-DD HH:MM:SS). You can display this verbatim or use slicing to chop off unwanted parts, or even use your own function to process this date and make it into something more human-readable ("Wednesday 9 July", etc). The point is that this date representation is unambiguous, easy to store and use, and sortable.
entry.modified_date
The date and time this entry was last modified.
entry.blogentry_id
The id of the current entry, in the database table. The id is important since it's used in permalinks, etc. It can also be used for commenting systems (YACCS, Haloscan, etc).
entry.anchor
A permalink to the current entry. Or rather, to the archived version of the current entry. (An entry on the start page doesn't have a permalink to itself, since that link isn't permanent at all... rather, it has a link to the entry in the archives.)
entry.misc
This is a dictionary that can contain all kinds of data; currently it's not been used to its full extent by Kaa... yet. Currently implemented are:


Last update: 2002.10.13