Skip to content

Commit 5ebe5ae

Browse files
committed
jx_template
1 parent f61ead0 commit 5ebe5ae

File tree

3 files changed

+94
-58
lines changed

3 files changed

+94
-58
lines changed

src/jx/component.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Component:
2424
required: tuple[str, ...] = ()
2525
optional: dict[str, t.Any] = {}
2626

27-
template: str = ""
27+
jx_template: str = ""
2828
components: Sequence["Component | type[Component]"] = ()
2929
css: tuple[str, ...] = ()
3030
js: tuple[str, ...] = ()
@@ -63,8 +63,8 @@ def __init__(
6363
self._parse_signature()
6464
self._init_components()
6565

66-
self.template = self.template or self._load_template()
67-
self._template = self._prepare_template(self.template)
66+
self.jx_template = self.jx_template or self._load_template()
67+
self._template = self._prepare_template(self.jx_template)
6868
self._attrs = Attrs({})
6969

7070
def __call__(self, **params: t.Any) -> Markup:
@@ -128,7 +128,7 @@ def render_css(self) -> Markup:
128128

129129
return Markup("\n".join(html))
130130

131-
def render_js(self) -> Markup:
131+
def render_js(self, module: bool = True, defer: bool = True) -> Markup:
132132
"""
133133
Uses the `collected_js()` list to generate an HTML fragment
134134
with `<script type="module" src="{url}"></script>` tags.
@@ -141,11 +141,17 @@ def render_js(self) -> Markup:
141141
for url in self.collect_js():
142142
if not rx_external_url.match(url) and not url.startswith("/"):
143143
url = f"{self.base_url}{url}"
144-
html.append(f'<script type="module" src="{url}"></script>')
144+
if module:
145+
tag = f'<script type="module" src="{url}"></script>'
146+
elif defer:
147+
tag = f'<script src="{url}" defer></script>'
148+
else:
149+
tag = f'<script src="{url}"></script>'
150+
html.append(tag)
145151

146152
return Markup("\n".join(html))
147153

148-
def render_assets(self) -> Markup:
154+
def render_assets(self, module: bool = True, defer: bool = False) -> Markup:
149155
"""
150156
Calls `render_css()` and `render_js()` to generate
151157
an HTML fragment with `<link rel="stylesheet" href="{url}">`
@@ -205,8 +211,13 @@ def _init_components(self) -> None:
205211
self.c[co.name] = co
206212

207213
def _load_template(self) -> str:
208-
filepath = Path(inspect.getfile(self.__class__)).with_suffix(".jinja")
209-
return filepath.read_text() if filepath.exists() else ""
214+
filepath = Path(inspect.getfile(self.__class__))
215+
files = list(filepath.parent.glob(f"{filepath.stem}*.jx"))
216+
if not files:
217+
files = list(filepath.parent.glob(f"{filepath.stem}*.jinja"))
218+
if not files:
219+
return ""
220+
return files[0].read_text()
210221

211222
def _prepare_template(self, template: str) -> str:
212223
parser = JxParser(name=self.name, source=template, components=list(self.c.keys()))

0 commit comments

Comments
 (0)