Page-specific logic in Joomla

2019-05-31 00:52发布

问题:

I am trying to enable JavaScript in a Joomla template to behave differently depending on the page. In particular, I have set the Key Reference as that appears to be the most appropriate value I could find for this purpose. Unfortunately, I can't seem to access it in my code. I tried:

$this->params->get("keyref")

and a few other variations, but they simply returned a blank. How can I retrieve this value or is there a better way of writing page specific logic.

Related Articles

  • Joomla load script in a specific page: This would work, but seems like overkill for what I want to do here.

回答1:

Each page can be given an alias. We can retrieve the alias using code from the forum:

function getCurrentAlias()
{
   $menu   = &JSite::getMenu();
   $active   = $menu->getActive();
   return $active->alias;
}

We can then inject this into the Javascript:

var alias= '<?php echo getCurrentAlias(); ?>';


回答2:

I'm not aware of keyref, but I would solve it by using the class suffix parameter you can set for each menu entry.see I would use a space in front of this suffix. With javascript you can then try to read this classname (suffix without the space) on each page.

getElementsByClassName("mysuffix");

for example

If this returns multiple objects, you know on which page you are. Will that help you?



标签: joomla