Skip to content

Slot is not executed in conditional {% if %} block #37

Closed

Description

@danjac

I have this component template:

<div>
...
{% if show_extra %}
{% slot extra_text %}{% endslot %}
{% endif %}
</div>

And in another template:


{% component_block "mycomponent" show_extra=True %}
{% slot extra_text %}show me!{% endslot %}
{% endcomponent_block %}

The text "show me!" is not rendered, just the slot default content. Overriden {% block %} content on the other hand is rendered inside a conditional.

Activity

  1. danjac commented on Feb 27, 2021

    @danjac
    ContributorAuthor

    I wonder if this is related to #34: the current implementation of slots as a simple list doesn't work when the slots themselves are executed conditionally.

  2. EmilStenstrom commented on Feb 27, 2021

    @EmilStenstrom
    Collaborator

    Interesting, you are a great tester for this project! :) I think we should do whatever block is doing, makes sense.

  3. jereze commented on Mar 19, 2021

    @jereze

    Hi,
    I noticed the same behavior.

  4. EmilStenstrom commented on Mar 20, 2021

    @EmilStenstrom
    Collaborator

    @jereze Would you mind helping to fix it? A PR that adds a test that shows the error would be a great start for instance.

  5. rbeard0330 commented on Mar 20, 2021

    @rbeard0330
    Contributor

    I wonder if this is related to #34: the current implementation of slots as a simple list doesn't work when the slots themselves are executed conditionally.

    I think this is right. The current implementation looks at the components nodelist, identifies any slot nodes in that nodelist, then replaces them with either the provided content or the default content. In both of these issues, the problem is that there is a slot nested within some other node (either an IfNode or another slot). If that happens, then the SlotNode isn't removed when the template is compiled, and it renders itself using its default content.

    You could potentially fix this by making the search for slots recursive. In conjunction with that, I think you'd also want to modify the slot's own nodelist rather than replacing it like we do currently, to minimize the chance that we violate some other node's expectations.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions