mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Adds support for themes functions
With this changes the themes can have (not obligatory) a functions.py file bundled with the templates. The purpose of this file is to hold helper functions to be injected into the jinja2 templates. To control which functions will be injected is specified the __export__ list. A sample was inserted in pelican/themes/simple/templates/functions.py.
This commit is contained in:
parent
7a15045fbc
commit
d9ff5bac32
2 changed files with 18 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
|||
from __future__ import unicode_literals, print_function
|
||||
|
||||
import os
|
||||
import imp
|
||||
import math
|
||||
import random
|
||||
import logging
|
||||
|
|
@ -62,6 +63,14 @@ class Generator(object):
|
|||
]),
|
||||
extensions=self.settings['JINJA_EXTENSIONS'],
|
||||
)
|
||||
try:
|
||||
theme_functions = imp.load_source('theme_functions', self._templates_path[0]+'/functions.py')
|
||||
except IOError:
|
||||
theme_functions = None
|
||||
|
||||
if theme_functions and '__export__' in vars(theme_functions):
|
||||
for func_name in theme_functions.__export__:
|
||||
self.env.globals[func_name] = getattr(theme_functions, func_name)
|
||||
|
||||
logger.debug('template list: {0}'.format(self.env.list_templates()))
|
||||
|
||||
|
|
|
|||
9
pelican/themes/simple/templates/functions.py
Normal file
9
pelican/themes/simple/templates/functions.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# encoding: utf-8
|
||||
|
||||
__export__ = ["helperFunction"]
|
||||
|
||||
def helperFunction():
|
||||
return "<p>I'm from a helper function!</p>"
|
||||
|
||||
def anotherHelper():
|
||||
return "I'm not being exported!"
|
||||
Loading…
Add table
Add a link
Reference in a new issue