Breadcrumbs

Breadcrumbs are typically displayed at the top of web pages, giving the user an indication of where the current page is within a site hierarchy, and a quick way to navigate up the hierarchy.

<microcms function="breadcrumbs" path="path of template file" />

Microcms replaces this <microcms> tag with a trail of breadcrumbs leading back up the directory hierarchy to the root page. At each level in the hierarchy, the index.html page is used as the breadcrumb. The text displayed for each breadcrumb is supplied in the meta tag with name "microcms-title".

Code for the breadcrumbs plugin is shown below:

breadcrumbs.py
# breadcrumbs.py - microcms plugin for adding a breadcrumbs trail to an HTML page
# Copyright (c) 2009 Niall McCarroll  
# Distributed under the MIT/X11 License (http://www.mccarroll.net/snippets/license.txt)

import os.path
from tinplate.tinplate import tinplate

def compile_breadcrumb(breadcrumb,depth):
    path = ''
    while depth > 0:
        path += "../"
        depth -= 1
    path += "index.html"
    breadcrumb['bpath'] = path
    
    return breadcrumb    
    
def compile_breadcrumbs(breadcrumbs):
    depth = len(breadcrumbs)-1
    b1 = compile_breadcrumb(breadcrumbs[0],depth)
    depth -= 1
    b2 = []
    for b in breadcrumbs[1:-1]:
        if b != None:
            b2.append(compile_breadcrumb(b,depth))
        depth -= 1 
    b3 = compile_breadcrumb(breadcrumbs[-1],depth)
    return (b1,b2,b3)

def process(cfile,attrs):
    npath = attrs["path"]
    breadcrumbs = [cfile.metadata]
    currentdir = cfile.parentdir
    while currentdir.parentdir != None:
        currentdir = currentdir.parentdir
        indexfile = currentdir.indexfile
        if indexfile != None:
            breadcrumbs = [indexfile.metadata]+breadcrumbs
        else:
            breadcrumbs = [None]+breadcrumbs
        
    if len(breadcrumbs) > 1:
        template = open(os.path.join(os.path.split(cfile.path)[0],npath),"r").read()
        t = tinplate()
        (homecrumb,crumbs,thiscrumb) = compile_breadcrumbs(breadcrumbs)
        return t.process(eval(template),[homecrumb,crumbs,thiscrumb])
    else:
        return "NO BREADCRUMBS"

 

Leave a comment

Anti-Spam Check
Comment