jpsca / jinjax

Server-Side Components with Jinja
https://jinjax.scaletti.dev/
MIT License
233 stars 14 forks source link

It'd be nice if could pass attrs from parent to child at compose time #13

Closed neilmcguigan closed 1 year ago

neilmcguigan commented 1 year ago

Say I want a generic container component that can dynamically set args on declared child components

like this:

<Parent>
  <Child {someAttrsFromParent} />
  <Child />
</Parent>

You can do this w Jinja macros (awkwardly):

{% call(kwargs) parent() %}
  {{ child(foo=1, **kwargs) }}
  {{ child(foo=2, **kwargs) }}
{% endcall %}

where parent looks like this:

{% macro parent() %}
...
{{  caller( dict(bar=2) ) }}
...
{% endmacro %}
jpsca commented 1 year ago

You can! But it's barely mentioned in the docs :( (https://jinjax.scaletti.dev/guide/extra/#attrs-methods).

<Parent>
  <Child __attrs={attrs} />
  <Child />
</Parent>
  1. attrs are the parent attrs
  2. __attrs begins with two underscores

If you only want to pass some arguments (and you know which ones), you can do it like any other value (attrs is similar to a dict)

<Parent>
  <Child title={attrs.get('title') or 'Hello'} />
  <Child />
</Parent>