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

Add when and unless to partial tag #7054

Merged
merged 3 commits into from Nov 30, 2022

Conversation

edalzell
Copy link
Contributor

A DX improvement so this:

{{ if subtitle }}
    {{ partial:components/subtitle }}
        {{ subtitle }}
    {{ /partial:components/subtitle }}
{{ /if }}
{{ partial:components/subtitle :when="subtitle" }}
    {{ subtitle }}
{{ /partial:components/subtitle }}

Also supports the converse unless

@what-the-diff
Copy link

what-the-diff bot commented Nov 15, 2022

  • Added a new method to the Partial class called shouldRender()
  • This method checks if there is a when or unless parameter in the partial tag and returns true/false accordingly
  • If no such parameters are present, it will return true by default (i.e., render)
  • The render() function now calls this before rendering anything so that we can skip partials based on conditions

@jackmcdade
Copy link
Member

jackmcdade commented Nov 15, 2022

Couldn't you do {{ subtitle ?= {partial:components/subtitle} }} though? 🤔 Like right from the docs: https://statamic.dev/new-antlers-parser#gatekeeper

@edalzell
Copy link
Contributor Author

Couldn't you do {{ subtitle ?= {partial:components/subtitle} }} though? 🤔 Like right from the docs: statamic.dev/new-antlers-parser#gatekeeper

No, because in my example we're using a slot and I don't want the partial called at all. I'm not assigning the subtitle to the results of the partial, I'm passing it in.

@jasonvarga
Copy link
Member

jasonvarga commented Nov 15, 2022

What Jack suggested just uses subtitle as the condition then renders the partial if subtitle is truthy. Good solution.

However it doesn't handle using partial as a tag pair.

@robdekort
Copy link
Contributor

This is neat!

@jasonvarga jasonvarga merged commit d56f583 into statamic:3.3 Nov 30, 2022
@edalzell edalzell deleted the feature/partial-when-unless branch January 6, 2023 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants