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?
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It only takes a minute to sign up.
Sign up to join this communityI'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?
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.
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.
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
.
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'));
script.js
is the same as loading sample.js
.
– Flimm
Jun 22 '15 at 8:30