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

Ability to use partials in a partials directory and underscored #305

Closed
dannyuk1982 opened this issue Sep 2, 2020 · 8 comments
Closed

Comments

@dannyuk1982
Copy link

The docs at https://statamic.dev/views#templates say to prefix partials with _underscores, and "You don’t need to include the underscore in your partial tag.".

Using {{ partial:foo }} with partials/foo.antlers.html works fine.

Using {{ partial:_foo }} with partials/_foo.antlers.html works fine.

Using {{ partial:foo }} with partials/_foo.antlers.html breaks with the error:

InvalidArgumentException
View [foo] not found.

Running 3.0.2

@duncanmcclean
Copy link
Member

duncanmcclean commented Sep 2, 2020

If you want to use it like that you'll need to specify the partial directory, like so:

{{ partial:partials/_foo }}

@dannyuk1982
Copy link
Author

dannyuk1982 commented Sep 2, 2020

Yup, but that's not how it's documented (or, I'm guessing, how it's intended to work).

You don't need to specifiy the partials directory either, just the underscore is enough.

Here is a fix to work as described in cms/src/Tags/Partial.php line 34, add:

if (view()->exists($subdirectoriedAndUnderscored = 'partials.' . $this->underscoredViewName($partial))) {
  return $subdirectoriedAndUnderscored;
}

@jonassiewertsen
Copy link

In that case a PR to the docs would be nice :-)

In bigger projects you will quickly get to that point, where you may have partials with the exact same name, just living in different directories.

As I see it, the behavior is expected as it does work now :-)

@jasonvarga
Copy link
Member

The initial idea was to do it one way or the other.

The underscores are so you can tell which views are partials when mixed in with everything else.
Or, if you're putting them in a partials directory, you know they're partials, so no need for an underscore.

We can add support for doing both, I suppose.

@dannyuk1982
Copy link
Author

I wasn't sure if it was a design decision, maybe just the docs need tweaking to make it clearer as it feels like a bug at the moment. My use case is smalled partials (like literally for a button, I use this a lot with Tailwind), I am using an _ and then larger things like header/footer without. That's very much a preference though, and it's easy for me to just add the _ to the partial tag.

@jasonvarga jasonvarga changed the title Partials with underscores do not work as documented Ability to use partials in a partials directory and underscored Sep 7, 2020
@jasonvarga jasonvarga transferred this issue from statamic/cms Sep 7, 2020
@jackmcdade
Copy link
Member

I actually can't recreate this. 🤔

Using {{ partial:foo }} with partials/foo.antlers.html works fine.

Using {{ partial:_foo }} with partials/_foo.antlers.html works fine.

Using {{ partial:foo }} with partials/_foo.antlers.html works fine.

@dannyuk1982
Copy link
Author

dannyuk1982 commented Sep 7, 2020

Hmm, I just tried again on 3.0.5 - same

image

That's with

{{
    partial:button
    :text="content_link_text"
    :url="content_link"
}}

image

I don't understand how "Using {{ partial:foo }} with partials/_foo.antlers.html works fine." works, as the code in cms/src/Tags/Partial.php checks the partials directory OR for underscores :-/ unless I'm really missing something

@jackmcdade
Copy link
Member

jackmcdade commented Sep 7, 2020

Oh my bad, I copied @damcclean's example {{ partial:partials/foo }}. You're 100% right. It's not like I wasn't confused myself lol

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

No branches or pull requests

5 participants