5

I'm trying to get started with Joomla extension development, and am having trouble following along with simple examples.

Where is the Jhtml class defined and/or documented? I see it being used in examples and extensions that come with Joomla, but I can't find any current documentation on it. Existing links into https://api.joomla.org/cms-3/ all appear to be broken, and my attempts to grep the Joomla source have not turned up anything.

Update:

Please tell me whether I understand this correctly. It looks like libraries/classmap.php calls JLoader::registerAlias to dynamically alias the name JHtml to HTMLHelper. I'm unable to find documentation on JHtml because there is no such class.

1
  • Your update is entirely correct (surprised no one answered this before now). Anytime you're looking for a a class definition, especially for one that starts with "J", start in classmap.php; you'll probably get there faster.
    – Arlen
    May 14, 2020 at 14:21

3 Answers 3

7

The JHtml class was changed to \Joomla\CMS\HTML\HTMLHelper (/libraries/src/HTML/HTMLHelper.php) since 3.8 as a part of namespacing the core.

I believe you can still use documentation (and API ref) of the old JHTML class.

The best way to learn everything about it - use IDE like PHPStorm for example, and you'll get a lot of useful hints, not to mention class propositions based on partial name typed.

Also what you may put a file like this one: _ide_helper.php in the root of your project to make your transition from <3.8 smoother with IDE.

2
  • Hey alexandr could add a link to the old documentation to yr post? Dec 19, 2017 at 9:17
  • Added, but it's almost safe to say that Joomla has almost no documentation for developers, but PHPDocs of the methods are useful.
    – Alexandr
    Dec 19, 2017 at 12:29
6

As of Joomla 3.8, the majority of classes have been namespaced but with a fallback for when migrating to J4.

So, you can still use JHtml::XXX, but the new approach is:

use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('script', 'path/to/file.js');
HTMLHelper::_('stylesheet', 'path/to/file.css');

You can find the source code for the HTMLHelper class here:

https://github.com/joomla/joomla-cms/blob/staging/libraries/src/HTML/HTMLHelper.php

The code is still the same as it was before, so you can continue reading through the current Joomla documentation for JHtml.

6
  • But what does this do specifically? Does it add the script to the head of the page? YES IT DOES!
    – Eoin
    Jun 10, 2019 at 19:52
  • So we should not use JFactory::getDocument(); anymore?
    – Eoin
    Jun 10, 2019 at 19:54
  • 1
    @Eoin, you can still use it if you want, but I'd suggest against it. HTMLHelper has additional features such as override support.
    – Lodder
    Jun 11, 2019 at 7:30
  • 1
    Let say you create an extension and load a Javascript file within it using HTMLHelper, users will be able to copy/paste the file into their template directory and make modifications to it if they want. Joomla will automatically load the overridden JS file from the template directory if it exists.
    – Lodder
    Jun 11, 2019 at 11:28
  • 1
5

Yeah the google links are messed up because of the namespacing and shifting changes. Most of the Joomla core classes are all now in libraries/src here:

https://github.com/joomla/joomla-cms/tree/staging/libraries/src

JHtml is in the HTML folder. The api listing is here:

https://api.joomla.org/cms-3/classes/Joomla.CMS.HTML.HTMLHelper.html

As for "Documentation"... lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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