6

I've seen code like this:

JHtml::_("script", "path/to/script.js");
JHtml::_("stylesheet", "path/to/stylesheet.css");
JHtml::_("jquery.framework");

What does the underscore function do here?

7

Most of the time

JHtml::_("script", "path/to/script.js");
JHtml::_("stylesheet", "path/to/stylesheet.css");
JHtml::_("jquery.framework");

will call

JHtml::Script("path/to/script.js");
JHtml::Stylesheet("path/to/stylesheet.css");
JHtmlJquery::framework();

However there is a small difference between the two calls. The first one allows system plugins to override the core classes.

Example

Let's assume you don't like the JQuery version shipped with Joomla for some funny reason. You could now create a system plugin and in the onAfterInitialise event you just register a different method for the jquery.framework call. You can do that using JHtml::register('jquery.framework', 'ClassToLoad::MethodToCall')

Michael Babker has written an example plugin which would allow to change the loaded Bootstrap version. That may help to figure out how it works.

2

It's simply a shortcut, to save typing I suppose. The documentation calls it a class loader method.

JHtml::_("script", "path/to/script.js");
# is the same as:
JHtml::script("path/to/script.js");

JHtml::_("stylesheet", "path/to/stylesheet.css");
# is the same as:
JHtml::stylesheet("path/to/stylesheet.css");

JHtml::_("jquery.framework");
# is the same as:
require_once 'libraries/cms/html/jquery.php';
JHtmlJquery::framework();

There's an algorithm that transforms the first argument to the correct function name, you can have a look a the source code in the file libraries/cms/html/html.php.

  • 3
    JHtml::_('script') is not exactly the same as JHtml::script(). The first one allows system plugin to override the core methods. Michael Babker did some example plugin here: github.com/mbabker/bs3-demo/blob/master/plg_system_bootstrap3/… – Bakual Jun 18 '15 at 19:18
  • 1
    @Bakual - definitely write this as an answer. I've been waiting a long time to know this – Lodder Jun 19 '15 at 7:15
  • 1
    Done. hope it helps :) – Bakual Jun 19 '15 at 15:44
-1

Same like this

    JHtml::_("script", "path/to/script.js");
    JHTML::script('sample.js', 'templates/custom/js/');

    JHtml::_("stylesheet", "path/to/stylesheet.css");
    JHtml::stylesheet('com_search/search.css', array(), true);

    JHtml::_("jquery.framework");
    JHtml::jquery('jquery.ui');
    JHtml::jquery('jquery.ui', array('sortable'));
  • I don't understand your answer. Which lines are the same as the others? – Flimm Jun 19 '15 at 9:06
  • You can write in two ways for example JHtml::_("script", "path/to/script.js"); (here _ are there) or JHTML::script('sample.js', 'templates/custom/js/'); (here _ is replace to script) both are provide same answer – Jextn Jun 19 '15 at 9:19
  • Your edit made it a bit clearer, but I'm still confused as to why loading script.js is the same as loading sample.js. – Flimm Jun 22 '15 at 8:30

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.