Express yourself, part II

By Serdar Yegulalp | 2016/07/10 08:00


MeTal generates pages by way of templates, whose filenames and URLs are determined by what are called mappings. Mappings are Python expressions or list comprehensions, although I'm finding I may in time need to have more robust ways to generate mappings, perhaps by way of entire modules.

Here's a simple mapping:

page.primary_category.title+'/%Y/%m/'+$i

(The $i at the end is a macro that inserts the name of the blog's default page type (e.g., index.html). This may change.)

Here's a more complex mapping:

[((x,),'tags/{}/index.html'.format(x.as_basename)) for x in page.tags_public]

This is for generating multiple mappings, in this case a list of mappings that are generated from all the tags in a page. (The tuple at the left is used as a mechanism to pass the tag itself, as context, to the generated page.)

You can see how this might get unwieldy. Think, for instance, how much more complicated it could get if we have to include pagination as part of the deal.

What I'm leaning towards is a mechanism -- optional but useful -- that allows you to generate mappings by way of a function stored in a module that's actually a template. The module, or the function in the module, would be passed the blog's contextual information, and then you could use functions from within the module to return mappings.

I don't plan on implementing this right away, but it's a good direction to lean in.


Tags: Python features templates


comments powered by Disqus