-
Notifications
You must be signed in to change notification settings - Fork 20
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
Assets not loaded when using shortcode only in Twig #98
Comments
@rhukster Looks like this is a valid bug. |
Any status change on this issue? Trying to use Shortcode Owl Carousel plugin. It works fine when using from markdown, but not in Twig. Code is parsed fine in Twig, but assets (javascripts, stylesheets and inline javascript) are missing. |
I don't think there has been any progress on this. I still have it on my radar for potential contribution, but considering the use case and the existing workarounds, I personally decided to put it at the very bottom of my priorities when compared to other stuff here and there in the Grav ecosystem. The workarounds still work fine by the way ;) |
I'm afraid the workarounds do no work for shortcodes which assets depend on the |
I'm not sure I understand what you are meaning by "assets depending on the From what I see over at the Owl Carousel shortcode plugin code, it looks to be loading static assets via which from my testing will indeed break when used from Twig only. However it should still be possible to have a commented Owl Carousel shortcode in content, so as to force load assets and have them available for when Twig needs it. This is actually exactly the case I was encountering initially, and for which I am still using the same workaround up until now. So I'm puzzled as to why it wouldn't work in this case 🤔 |
An example of assets depending on
The inline jQuery script looks for a div with an id which is generated using The commented Owl Carousel in Markdown would generate a script targeting an id specific to that shortcode, which is different from the id generated for the shortcode in Twig. Another example could be when assets depend on the parameters provided for the shortcode.
|
I see, I missed that part while glancing at the code! Thanks for the clarification. You're right: in this case, none of the above workarounds will help :/ |
Have this In it list to look at tomorrow. |
Do you have any update on this topic? Since I move to php8 and probably did some updates on Grav, Owl Carousel shortcode is not adding any asset. Thank you for your answers 🙏 |
sorry never did get around to it. just too many balls to juggle. would be nice if some others could jump in and debug this as I just don't have enough hours in the day to do it all. |
Hello,
There is an issue where shortcodes used only in Twig templates (and not content) will not load assets correctly if they are added as recommended from examples (from the
process
function passed when adding a new shortcode handler).Minimal plugin reproducing the issue: shortcode-asset-twig-demo.zip
Steps:
blank.js
is supposed to be loaded L19 inAssetTwigDemoShortcode.php
while processingassetTwigDemo
shortcode.[assetTwigDemo]
in content.<script src="/user/plugins/shortcode-asset-twig-demo/blank.js"></script>
will be added to generated page.{{ '[assetTwigDemo]'|shortcodes }}
<script src="/user/plugins/shortcode-asset-twig-demo/blank.js"></script>
missing in generate page.This is because Twig shortcodes are processed directly via
grav-plugin-shortcode-core/classes/plugin/ShortcodeManager.php
Lines 283 to 289 in 793bcd3
grav-plugin-shortcode-core/shortcode-core.php
Lines 110 to 146 in 793bcd3
grav-plugin-shortcode-core/shortcode-core.php
Lines 173 to 204 in 793bcd3
Two simple workarounds:
<!-- [assetTwigDemo] -->
. The shortcode will be correctly processed (thus, assets are loaded), but since it's commented it won't render anything on the page. This has the disadvantage of leaving a comment in the generated source code.init()
rather than in theprocess
function. You can uncomment L19 inAssetTwigDemoShortcode.php
to test this workaround. This has the disadvantage of loading assets for this shortcode on all pages, even when no shortcode of this type is present.I guess the proper solution would be to load assets not only when processing content but also when processing Twig directly. What do you think? If you like this approach, I'm willing to try my hand at an implementation.
Cheers!
The text was updated successfully, but these errors were encountered: