Joomla 模块可以“知道”吗?它处于什么位置?

发布于 2024-12-08 04:57:16 字数 274 浏览 0 评论 0原文

我对 Joomla 相当陌生(我更喜欢 WordPress),我有一个关于模块位置的问题。

模块可以知道它在什么位置吗?例如,我可以做这样的事情吗:

if(modulePosition =='left'){
    Do this...
}else{
    Do that...
}

这看起来很简单,但我已经搜索了几个小时,但找不到任何可以帮助我的东西。我知道有一个 countModules 函数,但据我所知,它只是检查模块是否处于活动状态。

感谢您的帮助!

I'm fairly new to Joomla (I've been more of a Wordpress guy) and I have a question about module positions.

Can a module know what position it's in. For instance can I do something like this:

if(modulePosition =='left'){
    Do this...
}else{
    Do that...
}

It seems easy enough, but I've searched for hours and can't find anything that will help me with that. I know there is a countModules function but from what I can tell, that just checks to see if the module is active or not.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一念一轮回 2024-12-15 04:57:16

我找到了答案!主要感谢@Hanny。他使用模块 id 的想法让我通过谷歌搜索找到了答案。对于其他碰巧想做类似事情的人来说,就是这样。

您使用全局变量 $module (谁会想到,对吧?)

所以我的代码现在看起来像这样:

$class = '';
if($module->position == 'position1'){
     $class = 'class1';
}
and so on...

非常简单,是吧?

要了解您还可以使用全局变量 $module 做什么,只需将其放入您的代码中,然后查看您可以使用哪些信息:

echo(print_r($module));

感谢您的所有帮助!

I found the answer! Mostly thanks to @Hanny. His idea of using the modules id got me googling for that and I came across the answer. For anyone else that happens to be looking to do something similar here it is.

You use a global variable $module (who'd a thought, right?)

So my code now looks like this:

$class = '';
if($module->position == 'position1'){
     $class = 'class1';
}
and so on...

Pretty simple, huh?

To find out what else you can do with the global variable $module just put this in your code and see what info you can use:

echo(print_r($module));

Thanks for all your help!

或十年 2024-12-15 04:57:16

简短的答案是“是”,您将根据模板为模块分配一个位置。当它出现时,您可以有关于该位置的条件(不同的模板对位置有不同的命名约定,因此请确保您在编码之前知道它们是什么)。

例如,有的使用“Position12”,有的可能使用“leftcol”等。你只需查看模板文件就可以看到(可以查看模板目录中的.xml文件来查看模板中列出的位置,或者在 index.php 文件中查找 jdoc 包含)。

根据我的一些经验,您真正需要这样的代码的唯一一次是在模板的核心布局文件中(例如,如果您有不同的列宽度,具体取决于模块是否存在),否则就会赢实际上并不是“可能会或可能不会”出现模块的时间 - 因为您将明确告诉他们在后端的位置和时间。

The short answer is 'yes', you'll assign a module a position based on your template. When it shows up you can have conditionals like that regarding that position (different templates have different naming conventions for positions, so make sure you know what they are before coding).

For example, some use "Position12", others may use "leftcol", etc. You just have to check in the template files to see (you can check the .xml file in the template directory to see the positions listed in the template, or look in the index.php file for the jdoc includes).

In some of my experience, the only time you'll really ever need code like that is in the core layout files of the template (for example, if you have different widths of columns depending on modules being present or not), otherwise there won't really be a time where you 'may or may not' have a module showing up - because you'll explicitly be telling them where to be and when on the back end.

油焖大侠 2024-12-15 04:57:16

我试图在约翰的解决方案下发表评论,但我没有足够的代表点 - 我想添加你在模板中命名模块位置的名称并不重要,从 $module 中返回的位置名称 - >position 始终为小写,无论您如何在模板中命名该位置...即。在您的模板 xml 中,当您尝试检查时,您可能会将 topBar 位置命名为“topbar”而不是“topBar”

if($module->position == 'topBar') //always false... use instead
if($module->position == 'topbar') //what you need to use

I tried to comment under john's solution but I don't have a enough rep points-- I wanted to add it doesn't matter what you name the module position in your template case-wise the position name you get back from $module->position is always all lowercase regardless of how you named the position in the template... ie. in your template xml somewhere you might have topBar position will be named 'topbar' not 'topBar' when you try to check it with

if($module->position == 'topBar') //always false... use instead
if($module->position == 'topbar') //what you need to use
梦幻之岛 2024-12-15 04:57:16

我不同意汉尼的观点。我想答案是否定的,不是像你描述的那样。

模板知道它何时到达模块位置,并获取分配给该位置的模块列表,然后调用它们进行渲染。但它不会传递该信息。它也不存储在 JApplication 或 JDocument 等中(例如,没有任何内容存储模板中渲染的位置或任何内容)。

不过,有一些技巧可以让你几乎得到你想要的东西。如果您知道需要搜索的模板位置(遗憾的是,没有简单的方法可以从模板中获取此位置 - 否则您可以解析模板的 .XML 文件中的 元素...),那么你可以这样做:

<?php

$positions = array('left', 'right', 'top', 'bottom')

$found_in = false;

foreach ($positions as $cur_position)
{
    $module_positions = JModuleHelper::getModules($cur_position);
    foreach ($module_positions as $cur_module_in_pos)
    {
        if ($cur_module_in_pos->module == 'mod_MYMODULE')
        {
            $found_in = $cur_position;
        }
    }
}

if ($found_in)
...

当然,如果你的模块在页面上多次包含,这不会很好地工作,但也许这是你可以做出的假设?

否则就需要破解核心 - 您可以在模板调用模块之前使用 JDispatcher::trigger() 调用,设置一些 var 等。不幸的是,核心中没有这样的事件可以启动(一个 pre_module_render 或其他东西)。

I'm going to disagree with Hanny. I think the answer is no, not as you describe.

The template knows when it has reached a module position, and it gets a list of modules assigned to that position, then calls for them to be rendered. But it doesn't pass that information on. It's not stored in JApplication or JDocument etc either (like, nothing stores where in the template the rendering is up to or anything).

There are some hacky ways to almost get what you want though. If you know the template positions you need to search (sadly there's no easy method for getting this from the template - otherwise you could parse your template's .XML file for <position> elements...), then you could do something like:

<?php

$positions = array('left', 'right', 'top', 'bottom')

$found_in = false;

foreach ($positions as $cur_position)
{
    $module_positions = JModuleHelper::getModules($cur_position);
    foreach ($module_positions as $cur_module_in_pos)
    {
        if ($cur_module_in_pos->module == 'mod_MYMODULE')
        {
            $found_in = $cur_position;
        }
    }
}

if ($found_in)
...

Of course, this doesn't work well if your module is included multiple times on the page, but maybe that's an assumption you can make?

Otherwise it'd be up to hacking the core - you could use a JDispatcher::trigger() call before the template calls a module, set some var etc. Unfortunately there's no such event in core to start (a pre_module_render or something).

樱花落人离去 2024-12-15 04:57:16

模块实例被分配给单个位置,该位置存储在数据库中,通常您会在模板中设置该位置的样式。一个模块实例只能分配到一个位置。因此,虽然这是一个有趣的问题,但它并不是一个真正实用的问题。

例外情况如下:
loadposition ...您可能想知道是否正在使用插件加载模块,因为这可能会将其放置在该位置的样式区域之外的某个位置。不过,我建议始终为此创建一个新实例,以便您拥有更多控制权。

loadmodule ...使用插件按名称加载的模块。在这种情况下,您可能最好创建一个模块的新实例并对其进行样式设计。另外,无论如何,我都会将它放在 span 或 div 中,具体取决于它是什么。

jdocinclude:module ...直接在模板中加载模块。同样,如果您这样做,我会将其包装在 span 或 div 中。在这种情况下,如果您喜欢的话,还可以包含一串内联样式。

将模块渲染为字符串并回显它,这基本上是一个非常定制的解决方案,您需要设置样式和选项。

A module instance is assigned to a single position and this is stored in the database and normally you would style the position in the template. A module instance can only be assigned to one position. So while it's an interesting question, it's not really a practical one.

The exceptions to this are the following:
loadposition ... you might want to know if a module is being loaded using the plugin because this would put it potentially somewhere besides the styled area for the position. THough i would recommend always making a new instance for this precisely so you have more control.

loadmodule ... module loaded by name using the plugin. In this case again you are probably better off making a new instance of the module and styling it. Also I'd put it in a span or div anyway, depending what it is.

jdocinclude:module ... loading a module directly in a template. Again if you are doing this I would wrap it in a span or div. In this case you are also allowed to include a string of inline styles if you like that kind of thing.

Rendering the module to a string and echoing it, again that is basically a very customized solution and you would want to set the styles and options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文