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

Feature: @partial-block for nunjucks engine #1105

Open
thommyhh opened this issue Nov 9, 2021 · 5 comments
Open

Feature: @partial-block for nunjucks engine #1105

thommyhh opened this issue Nov 9, 2021 · 5 comments

Comments

@thommyhh
Copy link

thommyhh commented Nov 9, 2021

What problem would this feature solve?

The handlebars engine supports partial-blocks, making it possible to create wrapper components or override a component's content from parent/including template. I could not find a way to achieve the same result when using nunjucks as template engine.

What the feature should look like?

A nice way would be a partial tag, that renders the content between {% partial '@container-partial' %} and {% endpartial %} and make the result usable as variable or function in the container-partial. This would be similar to nunjucks {% call 'someMacro' %}...{% endcall %} tag using {{ caller() }} inside the macro to render/output the tag body.

I'm not familiar with the internals of either fractal or nunjucks, so I'm not able to create that myself. Therefore I hope, someone has the knowledge and the time to do that.

@mihkeleidast
Copy link
Member

You should be able to use a regular variable in your component for the partial/block content. Then, in the parent component, use the set tag to create a variable block, then pass it into the child.

https://mozilla.github.io/nunjucks/templating.html#set

@mihkeleidast
Copy link
Member

Otherwise, this seems like a wish for a twig-like embed tag: mozilla/nunjucks#790

That issue has been open for five years with no solution. Even Mark, Fractal's creator, supported that addition. I would recommend for this to be requested upstream, as it's not really a Fractal-specific feature.

@thommyhh
Copy link
Author

@mihkeleidast Thank you for your feedback. I will try the {% set ... %} workaround, even though it sounds a bit hacky.

My idea was, that the tag should resolve a component, so I thought it would be fractal specific. But the embed would be really nice. I'll take a look at that.

@mihkeleidast
Copy link
Member

I think it would get the same template path resolving logic that the primary include et al tags use, so should not need anything specific for Fractal!

@thommyhh
Copy link
Author

@mihkeleidast Ok, thanks.

I tried the ´{% set ... %}` approach, but this does not seem to work. My template looks like

{% set fieldsetContent %}
    foo
   {% render '@atoms-form-field', {id: 'name', label: 'Name'}, merge=True %}
{% endset %}
{{ fieldsetContent | dump() }}
{% render '@atoms-form-fieldset', {legend: null, fieldsetContent: fieldsetContent} %}

The result is

"\n foo\n "
<fieldset class="form__fieldset">
    <legend class="form__legend"></legend>

    foo

</fieldset>

It looks like, that the set does not wait for the render to finish. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants