Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to slot content inside template : Nunjucks #189

Closed
chadams opened this issue Apr 16, 2022 · 1 comment
Closed

How to slot content inside template : Nunjucks #189

chadams opened this issue Apr 16, 2022 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@chadams
Copy link

chadams commented Apr 16, 2022

Given the template

  <div class="container flex flex-wrap items-center justify-between p-4 mx-auto sm:p-6 ">
          {{comp.Button({text:"example1"})}}
          {{comp.Button({text:"example2"})}}
  </div>

What I really want is to slot content into a reusable component, like React

  <Container>
          {{comp.Button({text:"example1"})}}
          {{comp.Button({text:"example2"})}}
  </Container>

My initial thought is to pass in like this...

{{comp.Container({content:[
    comp.Button({text:"example1"}), 
    comp.Button({text:"example1"})
]})}}

but is starting to look messy, is there a way to put JSX components inside NJK? or somehow pass NJK blocks to a NJK component?

@oscarotero
Copy link
Member

You can use lume components as react components. For example:

export default function Container({ comp })  {
    return <>
        <comp.Button text="example1" />
        <comp.Button text="example2" />
    </>
}

And then this component can be used in other jsx component or nunjucks:

<div>
    {{ comp.Container() }}
</div>

The comp argument is always available not only in the layouts but also the components.

FYI, I'm planning to implement the following syntax for components in nunjucks:

{% comp Container %}
    {{comp.Button({text:"example1"})}}
    {{comp.Button({text:"example2"})}}
{% endcomp %}

So this ease the use of nested components. But it's not implemented yet.

@oscarotero oscarotero added the enhancement New feature or request label Apr 16, 2022
@oscarotero oscarotero added this to the 1.8.0 milestone Apr 16, 2022
oscarotero added a commit that referenced this issue Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants