Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Loading plugins only on specific pages

Started by jordan 5 years ago · 5 replies · 536 views
5 years ago

Hi!

Some plugins I use (I think about Events and Lightslider especially) are not needed in every page, but their css and js are always loaded. It makes some pages unnecessarily heavy and I would like to avoid that by loading these plugins only on some pages with specific templates.
I imagine it's not so difficult to achieve in php, but I'm not familiar with this language and I haven't tried to dig into php files so far... could someone help me with that?

Thanks!

👍 1
5 years ago

@jordan ,
yes, your idea of loading assets only on the pages where needed via template is right.
all you have to do is writing the correct template.
as an example, you can take this template as a reference.
no PHP expertise needed here 😃

👍 1
5 years ago

@hoernerfranz, I'm afraid your suggestion will not work...

Both 'LightSlider' and 'Event' plugin add css and js assets in PHP and unfortunately, there is no check in PHP if assets should be loaded or not.

5 years ago

I believe you'll have to open issue for each plugin. Just checked Events and it loads assets on every page

Didn't even check the other. Every plugin might be different in how they load assets.

5 years ago

ah, yes, of course the mechanism of loading assets is unique to each plugin, and, @pamtbaau you are right in that my suggestion would not work with the mentioned plugins without changes to the plugin's php source.
my intention was rather to provide an example how to do this.
so the OP could
create an issue as @Karmalakas proposed
or
adapt the beforementioned plugins and create a pull request

5 years ago

Thanks for your answers.
I will create an issue for the plugins concerned. And in the meantime I'll have to modify the plugin's php file by adding a condition to the loading of the assets.
That's what I've done for the plugin event and it does the job. In the public function onTwigSiteVariables() of the file events.php, it looks like this

PHP
if ($page->template() == 'calendar') {
  $js = 'plugin://events/assets/events.min.js';
  $assets->add('jquery');
  $assets->addJs($js, 1);

  // styles
  $css = 'plugin://events/assets/events.min.css';
  $assets->addCss($css);
}
👍 1
last edited 03/29/21 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 47 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 72 2 months ago