Javascript & Snippets


I want to use snippets for galleries and sliders etc that i easily want to add to page layouts.
Some of them use huge JS Libs and I would like like to add these only when the snippet is used instead of putting them into the header or footer. What is the preferred way to do this in Kirby? Would it make more sense to create a plugin?


If the decision if you use the snippets is based on the template, you could check for the template in your footer snippet:

<?php if(in_array($page->template(), ['template-a', 'template-b'])): ?>
<?php echo js('myhugejslib.js') ?>
<?php endif ?>
1 Like

Thank you Lukas, I will try this.

I also found this map snippet which I want to try. It uses custom external google JS in its snippet and some inline JS. I am wondering if this would work with the js() loader and local JS.

Please note that this plugin is an archived Kirby 1 plugin. Google has changed their API a lot and it probably makes sense to use their latest code as the base.

And yes, embedding external JS with the js() helper will work, just give it the absolute URL.

yes, thanks, just noticed that it is for the old version.