hugo
hugo copied to clipboard
.HasAnyShortcode : return TRUE if a page uses at least one shortcode
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:
- a specific parameter for
.HasShortcode, like.HasShortcode Any - use
.HasShortcodewith an array to test if one of the shortcodes in the array is used
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
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.
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:
- Allow to test for any unnamed shortcodes.
- 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?
Let us create HasShortcodeAny; which returns true if any of the shortcode names given is in use.
What about the existing PR and the test for any unnamed shortcodes case?
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.
This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.