Skip to content

Commit

Permalink
[#2375] Recursive ckan_extends tags supported ///\\\OO///\\\
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Aug 9, 2012
1 parent ac8e2a0 commit 54271f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
3 changes: 1 addition & 2 deletions ckan/config/environment.py
Expand Up @@ -296,8 +296,7 @@ def genshi_lookup_attr(cls, obj, key):

# Create Jinja2 environment
env = lib.jinja_extensions.Environment(
loader=lib.jinja_extensions.CkanFileSystemLoader(template_paths,
ckan_base_path=paths['templates'][0]),
loader=lib.jinja_extensions.CkanFileSystemLoader(template_paths),
autoescape=True,
extensions=['jinja2.ext.i18n', 'jinja2.ext.do', 'jinja2.ext.with_',
lib.jinja_extensions.SnippetExtension,
Expand Down
34 changes: 21 additions & 13 deletions ckan/lib/jinja_extensions.py
Expand Up @@ -40,12 +40,25 @@ class CkanExtend(Extension):

tags = set(['ckan_extends'])

def __init__(self, environment):
Extension.__init__(self, environment)
self.searchpath = environment.loader.searchpath[:]

def parse(self, parser):
# if the template name has a * as the first char it will only be
# looked for in the ckan base templates
node = nodes.Extends(lineno=next(parser.stream).lineno)
node.template = parser.parse_expression()
node.template.value = '*' + node.template.value
template_path = parser.filename
# find where in the search path this template is from
index = 0
for searchpath in self.searchpath:
if template_path.startswith(searchpath):
break
index += 1
# provide our magic format
# format is *<search path parent index>*<template name>
node.template.value = '*' + str(index) + '*' + node.template.value
return node


Expand Down Expand Up @@ -94,21 +107,16 @@ class CkanFileSystemLoader(loaders.FileSystemLoader):
=====================================================================
'''

def __init__(self, searchpath, ckan_base_path, encoding='utf-8'):
# ckan changes: we accept and store the ckan_base_path
if isinstance(searchpath, basestring):
searchpath = [searchpath]
self.searchpath = list(searchpath)
self.ckan_base_path = ckan_base_path
self.encoding = encoding

def get_source(self, environment, template):
# if the template name starts with * then this should be
# treated as a ckan base template. otherwise we check all the
# template paths.
# treated specially.
# format is *<search path parent index>*<template name>
# so we only search from then downwards. This allows recursive
# ckan_extends tags
if template.startswith('*'):
template = template[1:]
searchpaths = [self.ckan_base_path]
parts = template.split('*')
template = parts[2]
searchpaths = self.searchpath[int(parts[1]) + 1:]
else:
searchpaths = self.searchpath
# end of ckan changes
Expand Down

0 comments on commit 54271f6

Please sign in to comment.