0

In a joomla menu item page i placed same module instances multiple times(i duplicated in module manager).Now it's a severe requirement that some codes of my module have to be only one time in page.Because my module's each instance injects same code again and again. how to avoid it so that only some codes will appear in page only once when i have multiple module instances of my module in that same page? searching through web i found some unreliable ways -

1.Using session in module i track whether same module loading two or more times and restrict code injecting one time only
2.using a constant like this way - 
if(!defined('MODULE_ALREADY_LOADED')){
//echo codes that you only want done once
//this will set the constant and now the other modules won't load/echo the code
define('MODULE_ALREADY_LOADED', true);
}

But not sure how much compatible the 2nd way is with joomla 2.5 as well as joomla 3 versions !.If there is a better error free way to do this then provide to me asap.

developer
  • 78
  • 1
  • 13
  • What makes you think that these are unreliable? – David Fritsch Jul 25 '13 at 17:27
  • like if that constant works globally or not.... – developer Jul 25 '13 at 20:40
  • http://php.net/manual/en/language.constants.php "As the name suggests, that value cannot change during the execution of the script." They are very global. Your bigger concern would be defining a constant that another extension tries to define. But just make your names descriptive (ie. put the name of the module in the constant) and you should be fine. – David Fritsch Jul 26 '13 at 03:25
  • thanks for reply i will try to use it as a best alternative.One more q- is it global in current page or in other pages too? – developer Jul 26 '13 at 14:18
  • It is global within that call to the server. So it will last for that page load. – David Fritsch Jul 26 '13 at 17:04
  • did not get "within that call to the server".you mean each page load starts with no constant value set? so in a menu item page load i define it then if i reload page or go another page that is undefined again and start from fresh? – developer Jul 26 '13 at 21:11
  • exactly. So every time the browser calls the server no constants are set for that page. Each ajax call is a call to the server. Each page load is a call to the server. – David Fritsch Jul 26 '13 at 21:46

1 Answers1

0

in your module code, you can use addscript method (it checks the duplicate scripts) instead of writing js directly. So, externalize your scripts, then in your code :

$document = JFactory::getDocument();
$document->addScript('your_script.js');
scraaappy
  • 2,830
  • 2
  • 19
  • 29
  • i knew it but my codes not only contain scripts but also html codes – developer Jul 26 '13 at 13:40
  • you maybe have to add some alternative layouts for your module by adding somelayout.php files in modules/mod_yourmodule/tmpl or templates/your_template/html/mod_yourmodule/ if you override it – scraaappy Jul 26 '13 at 14:44