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:
# - microcms plugin for adding a breadcrumbs trail to an HTML page
# Copyright (c) 2009 Niall McCarroll  
# Distributed under the MIT/X11 License (

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:
        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
            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])
        return "NO BREADCRUMBS"


Leave a comment

Anti-Spam Check