Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

JavaScript & custom shortcodes: Is the JS included in every page or only pages that have shortcode in the content area?


Go to End


1479 Views

Avatar
forumq42

Community Member, 18 Posts

23 July 2016 at 11:45am

Edited: 23/07/2016 11:53am

The documentation and example are a bit sparse but I've made a few custom shortcodes in the past as proof of concept and was wondering: regarding custom shortcodes, if one is pulling in some JavaScript for some reason in a shortcode code, either in:
- the .php file under /mysite/code
- or in the template area
- /mysite/template/<somefile>.ss
- or /themes/__/template/<somefile>.ss

and one puts the shortcode handler either in :
- /mysite/code/Page.php
- or /mysite/code/shortcodes/<some-shortcode.php>

does the JavaScript get loaded for every page on the CMS or only pages that have the shortcodes in the content field?
---

If a custom shortcode (or general page template for that matter) uses a JavaScript library that requires the use inclusion of JQuery, what is the correct way to have JQuery included before that library needs it? Normally I would just include JQuery in the <script> line above the library in question, however I know SilverStripe makes extensive use of JQuery and just adding JQuery above another library will possibly result in a conflict. At the moment, including JQuery in the shortcode controller above the external library seems to work but I get a feeling this is going to break something at some point.

Using:
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');

in the <?php section of the shortcode controller and excluding <script src=...> in the same file pointing to jquery from a CDN however results in things breaking.

as does adding Requirements:: to an init() function for the shortcode handler:
public function init() {
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
}
---
Lastly, having only used modules but never written one, if one wanted to just put all the shortcodes into a SilverStripe module, is it simply a matter of creating a directory in the docroot like:
/html/shortmodule

and under /shortmodule/code having the handler .php file defined, and the .ss template under /html/shortmodule/template and any custom required JS under /shortmodule/javascript ?

Reading the docs on modules, it looks like any PHP classes & templates there are automatically included.

Is there anything special that would need to be in the /shortmodule/_config.php file other than a <?php and registration code for the shortcode handler?