Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Simplify template loading code
Browse files Browse the repository at this point in the history
Turns out jinja2.FilesystemLoader can take a list of directories to look for
a given filename in, so there's no need to check for the file ourselves.

We also now reuse the jinja2.Environment object
  • Loading branch information
anoadragon453 committed Aug 14, 2020
1 parent 7b674bf commit 57c64c4
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions synapse/config/emailconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,22 +508,20 @@ def read_templates(
A list of jinja2 templates.
"""
templates = []
for filename in filenames:
template_directory = self.default_template_dir
search_directories = [self.default_template_dir]

# Check whether the template exists in the user-configured template directory
if custom_template_directory:
filepath = os.path.join(template_directory, filename)
if self.path_exists(filepath):
# Use the custom template directory instead
template_directory = custom_template_directory
# The loader will first look in the custom template directory (if specified) for the
# given filename. If it doesn't find it, it will use the default template dir instead
if custom_template_directory:
# Search the custom template directory as well
search_directories.insert(0, custom_template_directory)

# Set up jinja with the template directory
loader = jinja2.FileSystemLoader(template_directory)
env = jinja2.Environment(loader=loader, autoescape=autoescape)
loader = jinja2.FileSystemLoader(search_directories)
env = jinja2.Environment(loader=loader, autoescape=autoescape)

# Apply any custom filters
env.filters.update(filters)
for filename in filenames:
# Update the environment with the default filters plus any custom ones
env.filters = jinja2.filters.FILTERS.copy().update(filters)

# Load the template
template = env.get_template(filename)
Expand Down

0 comments on commit 57c64c4

Please sign in to comment.