custom django template tags

custom-built template tags for Django.

Django 1.x, Python 2.5.x

Using It From A Template

snippet of Django template for rendering syntax highlighting.

{%for code in featured.code_set.all%}
    {{code.body|syntax:"python"|safe}}
{%endfor%}

Hand-Rolled Django Template Tag For Syntax Highlighting

Acts as a wrapper around the python-pygments highlighting library. You're watching it in action right now.

@register.filter
def syntax(value, language):
    from pygments import highlight
    from pygments.formatters import HtmlFormatter

    if language == "php":
        from pygments.lexers import PhpLexer;
        lexer = PhpLexer()
    elif language == "html":
        from pygments.lexers import HtmlLexer;
        lexer = HtmlLexer() 
    elif language == "django":
        from pygments.lexers import DjangoLexer
        lexer = DjangoLexer()
    else:
        from pygments.lexers import PythonLexer;
        lexer = PythonLexer()

    return highlight(value, lexer, HtmlFormatter())