gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
74.37k stars 7.43k forks source link

.HasAnyShortcode : return TRUE if a page uses at least one shortcode #7996

Open jeremielp opened 3 years ago

jeremielp commented 3 years ago

Similar to .HasShortcode, but without having the need to specify a shortcode name as parameter.

.HasAnyShortcode would Return TRUE if at least one shortcode is used in the page else Return FALSE

Use case: CSS/JS libraries containing the code needed for a set of shortcodes. This would avoid to have to check for existence of all shortcodes one by one.

Alternatives could be:

bep commented 3 years ago

I agree about the "we need this", but I'm not sure we need another method. I think it fits with what we do in other areas of the template funcs that we have a vararg argument. So if we make HasShortCode(... string) I think most people would understand if we document it.

/cc @moorereason

bep commented 3 years ago

OK, looking at the use case, I think I misunderstood the use case, but know I think. I do, and I take back what I said about "retrofitting" the HasShortcode.

I think we need a method HasShortCodeAny which returns true if one of the given shortcode names is in use.

moorereason commented 3 years ago

First, as is usually the case, when I program late at night, I usually missing something. In this case, I didn't catch the fact that this PR is proposing two changes:

  1. Allow to test for any unnamed shortcodes.
  2. Allow to test for any of a list of named shortcodes.

My PR retrofitting HasShortcode only attempted to address the first request.

@bep, As for your new proposal, are you suggesting we leave HasShortcode unchanged and put these new features into HasShortcodeAny alone?

bep commented 3 years ago

Let us create HasShortcodeAny; which returns true if any of the shortcode names given is in use.

moorereason commented 3 years ago

What about the existing PR and the test for any unnamed shortcodes case?

jeremielp commented 3 years ago

FYI, what I finally did, as I had the case for both partials and shortcodes, is setting a boolean to true in the partial/shortcode using .Scratch.Set, and checking for that boolean to include or not my javascripts. These are all in the footer, after the shortcodes. I have not tested if this work for CSS in the <head> though.