6

I want to load some JS but only on the page where it is needed.

I'm trying to use the alias of the active article to determine that.

My code in the component.php file is:

   $path = &JFactory::getURI()->getPath();
   $length = strlen($path);

   for ($i = $length; $i >= 0 ; $i--)
         if ($path[$i] == '/')
            return substr($path, $i + 1, $length - $i - 1);

   $alias = $path;

and this part in the < head>:

if ($alias == "elearning")) { ?>
    <script src="/media/system/js/ddslick.js" type="text/javascript"></script>
<?php } ?>

Just doesn't seem to work.
Any suggestions where I'm going wrong?

  • Wood and trees. Found my problem. The page where I wanted it to load wasn't loading component.php. After transferring above to the index.php I got my JS. – BodgeIT Jun 24 '14 at 13:39
  • For the sake of connecting topics: joomla.stackexchange.com/questions/5198/… – FFrewin Apr 26 '16 at 10:32
1

One possibility is slightly modifying the layout to allow more template controls. This is not directly related to getting the alias of the article, but another approach.

To do this add this field inside the templateDetails.xml file

<field name="htmllayout" type="list" default="center"
    label="HTML Layout"
    description="Dynamic layout style from index.php"
    filter="word"
>
    <option value="main">main</option>
    <option value="raw">Raw</option>
</field>

You can add as many options as you need, I have 2 there as an example. This will create a setting in the template manager. Opening up the template in the template manager should allow you to change that setting as well. The key is to leave the current template on main, then duplicate the template (It only duplicates the settings, not the files). In this second template you can change the HTML Layout and use the tab "menu assignment" to specify which page may use it. So in the case of an alias of an article, as long as the article has a menu item associated with it (most likely with the same alias) it should be fairly easy to assign that template to the specified alias (and all others).

So basically you will have a template specified for use only with specific pages.

This also requires some edits to the index.php as well though.

add the following to the top of the file, within the first php block:

$htmllayout = $this->params->get('htmllayout');

Then, anywhere you need to specify something different for a special layout then do the following:

<?php if($htmllayout=='LAYOUTNAME'): ?>
anything you need here
<?php endif; ?>
  • Thanks for this @Jordan, an excellent procedure for many customisations and one that goes into my toolbox. – BodgeIT Jun 25 '14 at 7:58
1

If you're talking about the article alias, invoke the content model or read it from the database.

If you're talking about the alias in the url, getting it is not easy: Joomla has so many ways of generating it: it will be different if you get to the article :

  • via a blog or category list, it will be based on the article alias;
  • through a direct menu item, it will be based on the menu item alias;
  • through a search, Joomla decides;
  • you have a SEF extension? you make your own rules

And this is just for the last section of the alias; the first part (or 'path') depends on the navigation that took you to the article.

To give you an idea of how complex this is, a mainstream SEF extension invokes the frontend in order to get the alias, since they obviously found out it was not reliable to generate an alias in the backend.

Bottom line: forget the alias.

Ways around this: $input = JFactory::getApplication()->input;

  • Use the menu item id: $input->get('Itemid');
  • Use the component / view name: $input->get('option') . '.' . $input->get('view');;
  • Use the article id: $input->get('id'); (but check if option=com_content & view=article)

Hope this answers your question.

  • Thanks @Riccardo this looks like a solid solution although in return, the above code does provide the final part of the page alias. What's not clear is if it will work in all situations, so your method looks more robust. – BodgeIT Jun 25 '14 at 7:56
0

If you would like to load a js only on certain pages, try using jumi. That wokerd for me. http://extensions.joomla.org/extensions/core-enhancements/coding-a-scripts-integration/custom-code-in-content/1023

  • Thanks @Eva, this looks like it would work but not going to up-vote a recommendation for 3rd party extension as it is unnecessary in this case. – BodgeIT Aug 9 '17 at 7:13
-1

This is for Joomla 2.5.

$params = &$app->getParams();

# Get page title
$pageclass = $params->get('page_title');

# Convert title to alias
$pageclass = str_replace(' ', '-' ,strtolower($pageclass));

var_dump($pageclass);
  • 1
    not a reliable way of getting the alias which may have been altered by author. – BodgeIT Aug 9 '17 at 7:11

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.