Merge pull request #243 from xouillet/jinja_filters

Permit filters.py file in template dir to define custom jinja filters
This commit is contained in:
Simon Conseil
2017-03-12 01:13:53 +01:00
committed by GitHub
2 changed files with 19 additions and 0 deletions

View File

@@ -59,6 +59,15 @@ You can use the following variables in your template:
``theme.name``, ``theme.url``
Name and url of the currently used theme.
Filters
~~~~~~~
You can define custom jinja filters for your template by creating a ``filters.py`` script
at the root of your template directory.
This script will then be imported and all defined functions will be available as jinja filters
with the same names in your templates.
Documentation of sigal's main classes
-------------------------------------

View File

@@ -26,8 +26,10 @@ from __future__ import absolute_import
import codecs
import jinja2
import logging
import imp
import os
import sys
import types
from distutils.dir_util import copy_tree
from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PrefixLoader
@@ -81,6 +83,14 @@ class Writer(object):
**env_options
)
# handle optional filters.py
filters_py = os.path.join(self.theme, 'filters.py')
if os.path.exists(filters_py):
mod = imp.load_source('filters', filters_py)
for name in dir(mod):
if isinstance(getattr(mod, name), types.FunctionType):
env.filters[name] = getattr(mod, name)
try:
self.template = env.get_template(self.template_file)
except TemplateNotFound: