Skip to content

Commit

Permalink
Fix template response (#472)
Browse files Browse the repository at this point in the history
* fix TemplateResponse
- Fix returning strings (or using templates) causes " escaped.
- Fix TemplateResponse's auto-detection of the media-type when used directly.
* add inline comment
* fix release notes
  • Loading branch information
devkral authored Jan 17, 2025
1 parent 5953151 commit fd5a995
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ hide:
- Expose `Controller` in `esmerald` as alternative to `APIView`. This was already available to use but not directly
accessible via `from esmerald import Controller`.

## Fixed

- Fix escaped " in TemplateResponse.
- Fix TemplateResponse's auto-detection of the media-type when used directly.

## 3.6.2

### Added
Expand Down
2 changes: 1 addition & 1 deletion esmerald/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "3.6.2"
__version__ = "3.6.3"


from lilya import status
Expand Down
5 changes: 4 additions & 1 deletion esmerald/responses/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(
background: Optional[Union["BackgroundTask", "BackgroundTasks"]] = None,
headers: Optional[Dict[str, Any]] = None,
cookies: Optional["ResponseCookies"] = None,
media_type: Union[MediaType, str] = MediaType.HTML,
media_type: Union[MediaType, str] = MediaType.JSON,
):
if media_type == MediaType.JSON: # we assume this is the default
suffixes = PurePath(template_name).suffixes
Expand All @@ -38,6 +38,9 @@ def __init__(
self.template = template_engine.get_template(template_name)
self.context = context or {}
content = self.template.render(**context)
# ensure template string is not mangled
if isinstance(content, str):
content = content.encode(self.charset)
super().__init__(
content=content,
status_code=status_code,
Expand Down
13 changes: 10 additions & 3 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ def test_engine_jinja_and_mako(engine, template_dir: "pathlib.Path") -> None:
assert isinstance(app.template_engine, engine)


def test_templates_starlette(template_dir, test_client_factory):
@pytest.mark.parametrize("apostrophe", ["'", '"'])
def test_templates_starlette(template_dir, test_client_factory, apostrophe):
path = os.path.join(template_dir, "index.html")
with open(path, "w") as file:
file.write("<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>")
file.write(
"<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>".replace(
"'", apostrophe
)
)

@get()
async def homepage(request: Request) -> Template:
Expand All @@ -62,7 +67,9 @@ async def homepage(request: Request) -> Template:
)
client = EsmeraldTestClient(app)
response = client.get("/")
assert response.text == "<html>Hello, <a href='http://testserver/'>world</a></html>"
assert response.text == "<html>Hello, <a href='http://testserver/'>world</a></html>".replace(
"'", apostrophe
)


def test_alternative_template(template_dir, test_client_factory):
Expand Down

0 comments on commit fd5a995

Please sign in to comment.