Skip to content

Commit 209b32c

Browse files
committed
feat: render template when instantiating
1 parent 76384b3 commit 209b32c

File tree

6 files changed

+63
-15
lines changed

6 files changed

+63
-15
lines changed

.github/workflows/publish.yml

-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ jobs:
1818
uses: actions/setup-python@v5
1919
with:
2020
python-version: '3.11'
21-
- name: Cache Poetry
22-
id: cache-poetry
23-
uses: actions/cache@v4
24-
with:
25-
path: ~/.poetry
26-
key: poetry
2721
- name: Install Poetry
2822
if: steps.cache-poetry.outputs.cache-hit != 'true'
2923
run: |

.github/workflows/release.yml

-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ jobs:
1818
uses: actions/setup-python@v5
1919
with:
2020
python-version: '3.11'
21-
- name: Cache Poetry
22-
id: cache-poetry
23-
uses: actions/cache@v4
24-
with:
25-
path: ~/.poetry
26-
key: poetry
2721
- name: Install Poetry
2822
if: steps.cache-poetry.outputs.cache-hit != 'true'
2923
run: |

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,60 @@ print(template.render())
131131
# <button>3</button>
132132
# </button>
133133
```
134+
135+
### Rendering component from code
136+
137+
The idea behind this feature is to pass arguments and get rendered component inside the code.
138+
139+
```python
140+
@register(name="button")
141+
class Button(Component):
142+
template_str = "<button>{{ body }}</button>"
143+
144+
@register(name="menu")
145+
class Menu(Component):
146+
template_str = """\
147+
<div class="menu">\
148+
{% for btn in buttons %}
149+
{% button body=btn %}\
150+
{% endfor %}
151+
</button>
152+
"""
153+
154+
template = env.from_string("{% menu buttons=[1, 2, 3] %}")
155+
print(template.render())
156+
# <div class="menu">
157+
# <button>1</button>
158+
# <button>2</button>
159+
# <button>3</button>
160+
# </button>
161+
162+
print(Menu(env, buttons=[1, 2, 3]))
163+
# <div class="menu">
164+
# <button>1</button>
165+
# <button>2</button>
166+
# <button>3</button>
167+
# </button>
168+
```
169+
170+
Also it is possible to pass initialized components (because the are strings).
171+
172+
```python
173+
@register(name="menu")
174+
class Menu(Component):
175+
template_str = """\
176+
<div class="menu">\
177+
{% for btn in buttons %}
178+
{{ btn }}\
179+
{% endfor %}
180+
</button>
181+
"""
182+
183+
rendered = Menu(env, buttons=[Button(env, body=1), Button(env, body=2), Button(env, body=3)])
184+
print(rendered)
185+
# <div class="menu">
186+
# <button>1</button>
187+
# <button>2</button>
188+
# <button>3</button>
189+
# </button>
190+
```

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "jinja2-components"
3-
version = "0.0.2"
3+
version = "0.0.3"
44
description = "Streamlined way to build and reuse your Jinja2 templates from code"
55
authors = ["octo-gone <nklimin007@gmail.com>"]
66
readme = "README.md"

src/jinja2_components/component.py

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class Component:
1010
template_name: t.ClassVar[t.Optional[str]]
1111
block: t.ClassVar[bool] = False
1212

13+
def __new__(cls, env: "Environment", *args, **kwargs):
14+
template = cls.get_template(env, *args, **kwargs)
15+
return template.render(cls.get_context(*args, **kwargs))
16+
1317
@classmethod
1418
def get_context(cls, *args, **kwargs):
1519
return kwargs

src/jinja2_components/ext.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,4 @@ def parse_args(self, parser: "Parser"):
8181

8282
def render(self, tag: str, *args, **kwargs):
8383
component = self.components[tag]
84-
template = component.get_template(self.environment, *args, **kwargs)
85-
return template.render(component.get_context(*args, **kwargs))
84+
return component(self.environment, *args, **kwargs)

0 commit comments

Comments
 (0)