Jump to content

Using partial with dash in name


Ivan Gretsky
 Share

Recommended Posts

Good evening @teppo and everyone!

I am getting acquainted with partials.

I like the way to include them with chaining interface

<?= $partials->common->summaryblock() ?>

much better than the include way (opinionated ?)

<?php include 'partials/common/summaryblock.php'; ?>

One problem here is that I have to add those brackets, as without them I get the file path. IMHO the rendered markup should be the default to be more in line with ProcessWire way.

But my main problem is that I cannot use dashes (or dots) in partial file names. Is there a way to get around this limitation? Maybe something like this...

<?= $partials->get('common/summary-block') ?>

In simple cases it is just a matter of changing preferences, but if we get to partials with arguments, that seem to be the deal breaker, as we cannot do this:

<?= $partials->common->summary-block(['heading' => 'Hi!']) ?>
Link to comment
Share on other sites

4 hours ago, Ivan Gretsky said:

One problem here is that I have to add those brackets, as without them I get the file path. IMHO the rendered markup should be the default to be more in line with ProcessWire way.

A bit of background: rendering partials (using the partial name as method) is a relatively new addition, while including partials with chained syntax (<?php include $partials->common->summary ?>) has been an option since the very beginning.

I don't have a strong opinion about what should be the default behaviour, but the main reason why I likely won't change this is that it would potentially break a lot of sites ?

4 hours ago, Ivan Gretsky said:

But my main problem is that I cannot use dashes (or dots) in partial file names. Is there a way to get around this limitation? Maybe something like this...

<?= $partials->get('common/summary-block') ?>

In simple cases it is just a matter of changing preferences, but if we get to partials with arguments, that seem to be the deal breaker, as we cannot do this:

<?= $partials->common->summary-block(['heading' => 'Hi!']) ?>

At this point I would recommend against partial names with dots or dashes. Personally I tend to use snake_case for naming, as I find that the most sensible option: dashes or dots won't work, and camelCase and PascalCase are prone to errors (although it's customary to use them in class names). It is actually possible to access properties with dashes as well, but it's not exactly pretty: <?= $partials->common->{'summary-block'}(['heading' => 'Hi!']) ?>.

Extending $partials->get() with support for paths does seem like a good idea, though, so I'll add this to my todo list ?

  • Like 1
Link to comment
Share on other sites

On 1/18/2022 at 11:28 PM, teppo said:

Extending $partials->get() with support for paths does seem like a good idea, though, so I'll add this to my todo list ?

I would love to see that todo list) It might be a good way to create a github issue for each item and group them with projects or project boards. Should I create an issue based on this topic?

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...