Templating with Firedrop2The Power of embedded codeNote For a simpler introduction to templating with Firedrop2, read the Introduction to Templates. TemplatingFiredrop2 works by inserting your blog entries into the HTML templates that you provide. This means that you have complete control over the appearance of your blog. In the course of creating your blog (with front page, category archives, and the archives by date) Firedrop2 generates several (probably many) pages. To do this it uses two or three different template files, and all are kept in the blog directory.
These templates are mainly HTML, with a few special values that do the magic of inserting your entry, its title, the categories, and so on. In order to make the best of your blog, you need to know what these values are - and what properties they have. Embedded CodeFiredrop2 templates are rendered by a module called embedded_code. It is aptly named, because it allows you to embed chunks of Python code into your templates. When your pages are generated, these chunks are executed. This means that if you are a Python programmer the templating system is very straightforward. It is so easy though, that with a bit of knowledge even a non-programmer should
be able to use it. You can get a long way with copy and paste and a few guesses.
There are two different ways of using the magic values. They both involve inserting values between tags - either <% .... %> or <# ... #>``. The simplest is to insert a single name between template tags - <% name %> [1]. This is evaluated and the value inserted into the page. The second way allows you to execute blocks of code. Whole chunks of code can be inserted between <# ... #> tags. Anything that is printed [2] will be put into the page. The code must be valid Python, which includes obeying Python's rules about indentation. Here's a quick example :
<table border="1" width="150" summary="Links to the archives">
<tbody>
<tr><th>Weblog Category Archives</th></tr>
<tr><td>
<a href="all_by_date<% page_ext %>">All By Date</a>
</td></tr>
<tr><td>
<a href="all_by_category<% page_ext %>">All By Category</a>
</td></tr>
<#
# this line starts with a '#'
# which makes it a comment
# this displays the links to the different categories
import urllib
categories = options.get('categories', [])
# we use ``.get`` just in case no entries are set
categories.sort()
row = '<tr><td><a href="%s">%s</a></td></tr>'
for category in categories:
pagename = urllib.quote("arch_%s<% page_ext %>" % (category,))
print row % (pagename, category)
# it's what is printed here
# that ends up in the page
#>
<tr>
<td> </td>
</tr>
</tbody>
</table>
In case you can't work it out, this chunk of code uses urllib.quote to properly escape the links to the category archives. It then inserts the link to each category in a table row (row). The code is surrounded by the HTML for the table. embedded_code will automatically remove any (uniform) excess indentation - this means the whole chunk of code can be indented to fit in with your page structure. The NamespaceThe code chunks are all executed inside a namespace. This determines what variables (names) are available to you to use. So the most important thing is to know what names are available to you to use. These are slightly different in the entry template. The Entry TemplateThese are the most significant objects available to you in your entry template :
Example Entry TemplateSo you can put it in context, this is my entry_template.html. It illustrates several of the values above :
<div class="bloganchor">
<a title="Permanent link to this post" id="e<% entry.get_id() %>"
name="e<% entry.get_id() %>"
href="<% permalink(entry.get_id()) %>"
>#<% entry.get_id() %></a>
</div>
<h2><% entry.title %></h2>
<% entry_body %>
<p class="entryfoot">
Posted by <strong><% options['author'] %></strong> on
<% entry.attributes['insert_date'] %>.
<#
import urllib
if entry.attributes.get('categories',[]):
print '<br />\nCategories: '
catlist= ''
for entry in entry.attributes['categories']:
catlist += '<a href="%s">%s</a>, ' % \
((urllib.quote("arch_%s<% page_ext %>" % entry)), entry)
print catlist[:-2]
#>
</p>
<hr width="50%" />
If you are using your category names as links, then you ought to escape them properly [5]. This is why I include something a bit like : import urllib # this imports the urllib module entry = 'A Category' link_target = urllib.quote("arch_%s.<% page_ext %>" % entry) print '<a href="%s">%s</a>' % (link_target, entry) # urllib.quote does the escaping # the '%s....%s' % (something, something_else) # replaces the '%s' with the values on the right The Page TemplateAfter producing the individual entries, Firedrop2 builds all the pages. This includes the front page and various sorts of archives. Several of the values are the same as the entry_template, but different attributes are more relevant:
Example page_templateFollowing is the framework of my page_template.html file. It serves as an example :
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
lang="en" xml:lang="en">
<head>
<title><% options['title'] %></title>
<link rel="stylesheet"
href="/stylesheets/rest.css" type="text/css" />
</head>
<div id="nav">
<a class="skiplink" href="#startcontent">
Skip over navigation</a>
<a href="/index2.shtml">Home</a> >
<a href="../index.shtml">Python Index</a> >
<a href="index.shtml">Techie Blog</a>
</div>
<div id="main">
<a name="startcontent" id="startcontent"></a>
<div class="title">
<h1><% options['title'] %></h1>
<em><% options['description'] %></em>
</div>
<div class="sitemap" id="index">
<table border="1" width="300"
summary="Links to the archives">
<tbody>
<tr>
<th colspan="2" >Weblog Category Archives</th>
</tr>
<tr><td>
<a href="all_by_date<% page_ext %>">All By Date</a>
</td>
<td>
<a href="all_by_category<% page_ext %>">All By Category</a>
</td></tr>
<#
# this displays the links to the different categories
import urllib
categories = options.get('categories', [])
# just in case no entries are set
categories.sort()
a = 0
start = '<tr><td><a href="%s">%s</a></td>'
end = '<td><a href="%s">%s</a></td></tr>'
finish = '<td> </td></tr>'
last = True
while a < len(categories):
category = categories[a]
p = "arch_%s<% page_ext %>" % (category,)
pagename = urllib.quote(p)
if not a % 2:
print start % (pagename, category)
last = False
else:
print end % (pagename, category)
last = True
a += 1
if not last:
print finish
#>
<tr>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
</div>
<div class="mainbody">
<% body %>
<div class="indexblock" >
<h2>Archives</h2>
<ul class="links">
<#
shared.archive_names.sort()
archlist = shared.archive_names[-10:]
# last ten archives
archlist.reverse()
for archid, pagename in archlist:
line = '<li><a href="' + pagename + '">' \
+ archid + '</a></li>'
print line
#>
</ul>
</div>
<div class="indexblock" >
<ul class="links">
<li><a href="index<% page_ext %>">Front page</a></li>
<li><a href="index.xml">RSS Feed</a></li>
<li><a href="atom.xml">Atom Feed</a></li>
<li><a href="#index">Return to Top</a></li>
</ul>
</div>
</div>
</body></html>
The Indexpage TemplateAfter producing the individual entries, Firedrop2 builds all the pages. This includes the front page and various sorts of archives. If your site contains an Article Collection or Item List, an indexpage will also be built. Most of its values are the same as the entry_template and page_template, but different attributes are more relevant:
Example page_templateFollowing is the framework of an indexpage_template.html file. It serves as an example :
<#
print "<head><title><% options['title'] %></title>"
print '<link rel="stylesheet" href="default.css" type="text/css" /></head>'
print "<body>"
print "<h1>%s</h1>" % options['title']
print "<p> </p>"
print "<p>Article Collection contains these articles:</p>"
#>
<% index_body %>
<#
print "<hr>"
#>
<% footer %>
<#
print "</body>"
#>
A Word About MacrosThere is another way of calling simple functions from within templates. These are called macros. Macros allow you to use a shorthand for often-used text or HTML. Included is an example macro file (and supporting modules). It has macros for inserting smilies, colorising Python files, acronyms, etc. You will need to modify it for your own sites, by putting the correct paths into the source code at least. See the page on macros for more details. Note that macros are evaluated after the template is rendered. Footnotes
Return to Top |
|||||||||||