Skip to content

Commit

Permalink
Ensure Jinja2Renderer.render_file() sets the template name based on t…
Browse files Browse the repository at this point in the history
…he file (#1861)
  • Loading branch information
bartfeenstra authored Aug 6, 2024
1 parent 7471c09 commit 3c15690
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions betty/jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
select_autoescape,
FileSystemLoader,
pass_context,
Template,
)
from jinja2.runtime import StrictUndefined, Context, DebugUndefined
from typing_extensions import override
Expand Down Expand Up @@ -320,6 +321,20 @@ def __init__(

return self._context_class

async def from_file(self, file_path: Path) -> Template:
"""
Create a :py:class:`jinja2.Template` out of the given Jinja2 file path.
This method is intended for rendering individual files once. It **MUST NOT**
be used for reusable templates.
"""
async with aiofiles.open(file_path) as f:
template_source = await f.read()
template_code = self.compile(
template_source, filename=str(file_path.expanduser().resolve())
)
return self.template_class.from_code(self, template_code, self.globals)

@pass_context
def _gettext(self, context: Context, message: str) -> str:
return context_localizer(context).gettext(message)
Expand Down Expand Up @@ -420,11 +435,8 @@ async def render_file(
):
resource = "/".join(resource_parts[1:])
data["page_resource"] = resource
async with aiofiles.open(file_path) as f:
template_source = await f.read()
rendered = await self._environment.from_string(
template_source, self._environment.globals
).render_async(data)
template = await self._environment.from_file(file_path)
rendered = await template.render_async(data)
async with aiofiles.open(destination_file_path, "w", encoding="utf-8") as f:
await f.write(rendered)
await aiofiles_os.remove(file_path)
Expand Down

0 comments on commit 3c15690

Please sign in to comment.