Skip to content

Commit 94755d4

Browse files
committed
simplify component template
1 parent 5bff9c0 commit 94755d4

File tree

3 files changed

+7
-21
lines changed

3 files changed

+7
-21
lines changed

reflex/compiler/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def _compile_component(component: Component | StatefulComponent) -> str:
333333
Returns:
334334
The compiled component.
335335
"""
336-
return templates.COMPONENT.render(component=component)
336+
return templates.component_template(component=component)
337337

338338

339339
def _compile_components(

reflex/compiler/templates.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
if TYPE_CHECKING:
1515
from reflex.compiler.utils import _ImportDict
16+
from reflex.components.component import Component, StatefulComponent
1617

1718

1819
def _sort_hooks(hooks: dict[str, VarData | None]):
@@ -513,28 +514,16 @@ def context_template(
513514
}}"""
514515

515516

516-
def _component_template(**kwargs):
517+
def component_template(component: Component | StatefulComponent):
517518
"""Template to render a component tag.
518519
519520
Args:
520-
**kwargs: Template context variables including component.
521+
component: The component to render.
521522
522523
Returns:
523524
Rendered component as string.
524525
"""
525-
component = kwargs.get("component", {})
526-
# If component has a render method, call it, otherwise use it as-is
527-
rendered_data = component.render() if hasattr(component, "render") else component
528-
result = _RenderUtils.render(rendered_data)
529-
530-
# Add trailing newline for HTML elements (those with quoted tag names)
531-
# to match original Jinja behavior
532-
if isinstance(rendered_data, dict) and rendered_data.get("name", "").startswith(
533-
'"'
534-
):
535-
result += "\n"
536-
537-
return result
526+
return _RenderUtils.render(component.render())
538527

539528

540529
def page_template(
@@ -823,9 +812,6 @@ def render(self, **kwargs) -> str:
823812
return self.func(**kwargs)
824813

825814

826-
# Template to render a component tag.
827-
COMPONENT = TemplateFunction(_component_template)
828-
829815
# Code to render Component instances as part of StatefulComponent
830816
STATEFUL_COMPONENT = TemplateFunction(_stateful_component_template)
831817

tests/units/components/core/test_html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_html_create():
1919
assert str(html.dangerouslySetInnerHTML) == '({ ["__html"] : "<p>Hello !</p>" })' # pyright: ignore [reportAttributeAccessIssue]
2020
assert (
2121
str(html)
22-
== 'jsx("div",{className:"rx-Html",dangerouslySetInnerHTML:({ ["__html"] : "<p>Hello !</p>" })},)\n'
22+
== 'jsx("div",{className:"rx-Html",dangerouslySetInnerHTML:({ ["__html"] : "<p>Hello !</p>" })},)'
2323
)
2424

2525

@@ -39,5 +39,5 @@ class TestState(State):
3939
)
4040
assert (
4141
str(html)
42-
== f'jsx("div",{{className:"rx-Html",dangerouslySetInnerHTML:{html_dangerouslySetInnerHTML!s}}},)\n'
42+
== f'jsx("div",{{className:"rx-Html",dangerouslySetInnerHTML:{html_dangerouslySetInnerHTML!s}}},)'
4343
)

0 commit comments

Comments
 (0)