Skip to content

Commit 526caf7

Browse files
committed
Normalize preloaded template loader names to support dot-prefixed partials
1 parent 5ae804f commit 526caf7

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

src/fstache/_factories.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ def load_template(name: str) -> CompiledTemplate:
283283
if template is not None:
284284
return template
285285

286+
normalized = _normalize_template_name(
287+
name,
288+
extension=extension,
289+
remove_extension=remove_extension,
290+
)
291+
if normalized is not None and normalized != name:
292+
template = templates.get(normalized)
293+
if template is not None:
294+
templates[name] = template
295+
296+
return template
297+
286298
return resolve_missing_template(name)
287299

288300
return load_template
@@ -337,6 +349,27 @@ def _template_path_for_name(
337349
extension: str,
338350
remove_extension: bool,
339351
) -> Path | None:
352+
normalized = _normalize_template_name(
353+
name,
354+
extension=extension,
355+
remove_extension=remove_extension,
356+
)
357+
358+
if normalized is None:
359+
return None
360+
361+
if remove_extension and extension != "":
362+
return root / f"{normalized}{extension}"
363+
364+
return root / normalized
365+
366+
367+
def _normalize_template_name(
368+
name: str,
369+
*,
370+
extension: str,
371+
remove_extension: bool,
372+
) -> str | None:
340373
if remove_extension and extension != "":
341374
path = Path(f"{name}{extension}")
342375
else:
@@ -347,7 +380,12 @@ def _template_path_for_name(
347380
if path.is_absolute() or ".." in path.parts:
348381
return None
349382

350-
return root / path
383+
name_with_ext = path.as_posix()
384+
if remove_extension and extension != "":
385+
if name_with_ext.endswith(extension):
386+
return name_with_ext[: -len(extension)]
387+
388+
return name_with_ext
351389

352390

353391
def _iter_template_sources(

tests/test_factories.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,27 @@ def test_preload_templates_true_does_not_see_root_template_edits(
298298

299299
assert render("page.mustache", {"name": "Ada"}).to_bytes() == b"Hello Ada"
300300

301+
def test_preloaded_and_live_renderers_resolve_dot_prefixed_partials_equally(
302+
self,
303+
tmp_path: Path,
304+
) -> None:
305+
_write_template(tmp_path / "page.mustache", b"Hello {{>./partial.mustache}}")
306+
_write_template(tmp_path / "partial.mustache", b"Partial")
307+
308+
def create(preload_templates: bool) -> fstache.TemplateRenderer:
309+
return fstache.create_renderer(
310+
tmp_path,
311+
preload_templates=preload_templates,
312+
resolve_missing_template=fstache.resolve_missing_template_as_empty,
313+
)
314+
315+
live = create(preload_templates=False)
316+
preloaded = create(preload_templates=True)
317+
expected = live("page.mustache", {}).to_bytes()
318+
319+
assert expected == b"Hello Partial"
320+
assert preloaded("page.mustache", {}).to_bytes() == expected
321+
301322
def test_preload_templates_false_sees_partial_template_edits(
302323
self,
303324
tmp_path: Path,

0 commit comments

Comments
 (0)