【问题标题】:showing module code once when multiple joomla modules in same page当同一页面中有多个 joomla 模块时显示模块代码一次
【发布时间】:2013-07-25 12:43:21
【问题描述】:

在 joomla 菜单项页面中,我多次放置了相同的模块实例(我在模块管理器中重复了)。现在严格要求我的模块的某些代码必须在页面中只有一次。因为我的模块的每个实例都注入相同的代码一次又一次。 如何避免它,以便当我在同一页面中有我的模块的多个模块实例时,只有一些代码只会出现在页面中一次? 通过网络搜索我发现了一些不可靠的方法 -

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);
}

但不确定第二种方式与 joomla 2.5 以及 joomla 3 版本的兼容性有多大!如果有更好的无错误方式来做到这一点,请尽快提供给我。

【问题讨论】:

  • 是什么让您认为这些不可靠?
  • 比如该常量是否在全局范围内有效......
  • php.net/manual/en/language.constants.php "顾名思义,该值在脚本执行期间无法更改。"它们非常全球化。您更关心的是定义另一个扩展试图定义的常量。但是只要让你的名字具有描述性(即将模块的名称放在常量中)就可以了。
  • 感谢您的回复,我将尝试将其用作最佳替代方案。还有一个 q- 它在当前页面或其他页面中是全局的吗?
  • 它在对服务器的调用中是全局的。所以它将持续到该页面加载。

标签: php session joomla2.5 joomla3.0 joomla-module


【解决方案1】:

在您的模块代码中,您可以使用 addscript 方法(它检查重复的脚本)而不是直接编写 js。因此,将您的脚本外部化,然后在您的代码中:

$document = JFactory::getDocument();
$document->addScript('your_script.js');

【讨论】:

  • 我知道,但我的代码不仅包含脚本,还包含 html 代码
  • 如果你覆盖它,你可能需要通过在 modules/mod_yourmodule/tmpl 或 templates/your_template/html/mod_yourmodule/ 中添加 somelayout.php 文件来为你的模块添加一些替代布局
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多