Skip to content

Commit

Permalink
✨ initial prototype, not tested. #235
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Feb 25, 2019
1 parent 9a9dcc9 commit eab156a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
16 changes: 11 additions & 5 deletions moban/jinja2/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self):
constants.TEMPLATE_ENGINE_EXTENSION, tags=["jinja2", "jinja", "jj2", "j2"]
)
class Engine(object):
def __init__(self, template_dirs, extensions=None):
def __init__(self, template_dirs, options=None):
"""
Contruct a jinja2 template engine
Expand All @@ -73,10 +73,16 @@ def __init__(self, template_dirs, extensions=None):
extension for extension in JINJA2_THIRD_PARTY_EXTENSIONS
], # get a copy of this global variable
)
if is_extension_list_valid(extensions):
# because it is modified here
env_params["extensions"] += extensions
import_module_of_extension(extensions)
if options:
if options.get('extensions'):
if is_extension_list_valid(options['extensions']):
# because it is modified here
env_params["extensions"] += options['extensions']
import_module_of_extension(options['extensions'])

if 'extensions' in options:

This comment has been minimized.

Copy link
@ayan-b

ayan-b Feb 26, 2019

Member

This seems redundant. I guess this should suffice:

if options.get('extensions'):
    ...
    options.pop('extensions')
options.pop('extensions')
env_params.update(options)
self.jj2_environment = Environment(**env_params)
for filter_name, filter_function in FILTERS.get_all():
self.jj2_environment.filters[filter_name] = filter_function
Expand Down
21 changes: 18 additions & 3 deletions moban/plugins/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,29 @@ class MobanFactory(PluginManager):
def __init__(self):
super(MobanFactory, self).__init__(constants.TEMPLATE_ENGINE_EXTENSION)
self.extensions = {}
self.options_registry = {}

def register_extensions(self, extensions):
self.extensions.update(extensions)

def register_options(self, template_types):
self.options_registry.update(template_types)

def get_engine(self, template_type, template_dirs, context_dirs):
engine_cls = self.load_me_now(template_type)
engine_extensions = self.extensions.get(template_type)
engine = engine_cls(template_dirs, engine_extensions)
try:
engine_cls = self.load_me_now(template_type)
engine_extensions = self.extensions.get(template_type)
options = dict(extensions=engine_extensions)
except exceptions.NoThirdPartyEngine:
if template_type not in self.options_registry:
raise
else:
custom_engine_spec = self.options_registry[template_type]
engine_cls = self.load_me_now(custom_engine_spec.base_type)
# TODO:
# support other jinja2 options
options = custom_engine_spec['options']
engine = engine_cls(template_dirs, options)
return MobanEngine(
template_dirs, context_dirs, engine
)
Expand Down

0 comments on commit eab156a

Please sign in to comment.