Skip to content

Commit

Permalink
feat: render template when instantiating
Browse files Browse the repository at this point in the history
  • Loading branch information
octo-gone committed Aug 8, 2024
1 parent 76384b3 commit 209b32c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Cache Poetry
id: cache-poetry
uses: actions/cache@v4
with:
path: ~/.poetry
key: poetry
- name: Install Poetry
if: steps.cache-poetry.outputs.cache-hit != 'true'
run: |
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Cache Poetry
id: cache-poetry
uses: actions/cache@v4
with:
path: ~/.poetry
key: poetry
- name: Install Poetry
if: steps.cache-poetry.outputs.cache-hit != 'true'
run: |
Expand Down
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,60 @@ print(template.render())
# <button>3</button>
# </button>
```

### Rendering component from code

The idea behind this feature is to pass arguments and get rendered component inside the code.

```python
@register(name="button")
class Button(Component):
template_str = "<button>{{ body }}</button>"
@register(name="menu")
class Menu(Component):
template_str = """\
<div class="menu">\
{% for btn in buttons %}
{% button body=btn %}\
{% endfor %}
</button>
"""
template = env.from_string("{% menu buttons=[1, 2, 3] %}")
print(template.render())
# <div class="menu">
# <button>1</button>
# <button>2</button>
# <button>3</button>
# </button>
print(Menu(env, buttons=[1, 2, 3]))
# <div class="menu">
# <button>1</button>
# <button>2</button>
# <button>3</button>
# </button>
```

Also it is possible to pass initialized components (because the are strings).

```python
@register(name="menu")
class Menu(Component):
template_str = """\
<div class="menu">\
{% for btn in buttons %}
{{ btn }}\
{% endfor %}
</button>
"""
rendered = Menu(env, buttons=[Button(env, body=1), Button(env, body=2), Button(env, body=3)])
print(rendered)
# <div class="menu">
# <button>1</button>
# <button>2</button>
# <button>3</button>
# </button>
```
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jinja2-components"
version = "0.0.2"
version = "0.0.3"
description = "Streamlined way to build and reuse your Jinja2 templates from code"
authors = ["octo-gone <[email protected]>"]
readme = "README.md"
Expand Down
4 changes: 4 additions & 0 deletions src/jinja2_components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Component:
template_name: t.ClassVar[t.Optional[str]]
block: t.ClassVar[bool] = False

def __new__(cls, env: "Environment", *args, **kwargs):
template = cls.get_template(env, *args, **kwargs)
return template.render(cls.get_context(*args, **kwargs))

@classmethod
def get_context(cls, *args, **kwargs):
return kwargs
Expand Down
3 changes: 1 addition & 2 deletions src/jinja2_components/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,4 @@ def parse_args(self, parser: "Parser"):

def render(self, tag: str, *args, **kwargs):
component = self.components[tag]
template = component.get_template(self.environment, *args, **kwargs)
return template.render(component.get_context(*args, **kwargs))
return component(self.environment, *args, **kwargs)

0 comments on commit 209b32c

Please sign in to comment.