Joomla的countModules方法可以计算一个指定模块位置上发布的模块的数量,通常我们会基于这个函数计算的结果来实现空位置的隐藏。典型的案例就是隐藏侧边栏——隐藏没有发布模块的位置-v0.0.14
1,本节目标
详细的了解countModules这个方法的用法
2,用法说明
2.1 最简单的用法
countModules方法接受一个字符串的参数。该参数为你需要计算的位置的名称。返回该位置发布模块的个数。一段典型的代码如下:
<?php if ($this->countModules( 'sidebar' )) : ?>
<div class="sidebar">
<jdoc:include type="modules" name="sidebar" style="html5" />
</div>
<?php endif; ?>
上面的代码通过检查sidebar这个位置的模块数量,如果该位置的模块数量不为0,那么就输出这一个位置。
2.2 不同位置的判断
// 调整内容区域的宽度
if ($this->countModules('position-7') && $this->countModules('position-8'))
{
$span = "col-md-6"; //两个位置都发布了模块 那么内容就占据 col-md-6
}
elseif ($this->countModules('position-7') && !$this->countModules('position-8'))
{
$span = "col-md-9";//发布一个,内容占col-md-9
}
elseif (!$this->countModules('position-7') && $this->countModules('position-8'))
{
$span = "col-md-9";
}
else
{
$span = "col-md-12";//内容占全部
}
以上代码依据position-7 和 posistion-8这两个位置模块的发布情况来调整内容宽度
2.3 高级用法
有时候,我们需要判断两个位置的发布情况,我们也可以使用操作符来简化操作。最常见的操作符有 or ,and.
<?php if ($this->countModules( 'user1 or user2' )) : ?>
上面判断 user1 和user2的位置,只要两个中有一个发布,那么结果就为真。否则为假。
<?php if ($this->countModules( 'user1 and user2' )) : ?>
上面判断 user1 和user2的位置,只要两个都同时发布,那么结果就为真。否则为假。
一段典型的用法如下:
<?php if ($this->countModules( 'user1 or user2' )) : ?>
<div class="user1user2">
<?php if ($this->countModules( 'user1' )) : ?>
<jdoc:include type="modules" name="user1" style="xhtml" />
<?php endif; ?>
<?php if ($this->countModules( 'user1 and user2' )) : ?>
<div class="divider"></div>
<?php endif; ?>
<?php if ($this->countModules( 'user2' )) : ?>
<jdoc:include type="modules" name="user2" style="xhtml" />
<?php endif; ?>
</div>
<?php endif; ?>
只要发布了user 1或者user2中的位置中的一个,那么就会输出 user1user2这类。只有同时发布了user1和user2这两个位置时,才会输出divider类。
3,countModules操作符说明
| 操作符 | 实例 | 说明 |
|---|---|---|
| + | user1 + user2 |
user1,user2位置发布的模块的只能总数量 |
| - | user1 - user2 |
user1位置模块的数量 — user2位置发布的模块的数量 |
| * | user1 * user2 |
The number of Modules in the user1 position multiplied by the number in the user2 position. |
| / | user1 / user2 |
The number of Modules in the user1 position divided by the number in the user2 position. |
| == | user1 == user2 |
Returns true if user1 and user2 have the same number of Modules enabled; otherwise returns false. |
| != | user1 != user2 |
Returns true if user1 and user2 do not have the same number of Modules enabled; otherwise returns false. |
| <> | user1 <> user2 |
Same as !=. |
| < | user1 < user2 |
Returns true if user1 has strictly less Modules enabled than user2; otherwise returns false. |
| > | user1 > user2 |
Returns true if user2 has strictly more Modules enabled than user1; otherwise returns false. |
| <= | user1 <= user2 |
Returns true if user1 has the same or less Modules enabled than user2; otherwise returns false. |
| >= | user1 >= user2 |
Returns true if user2 has the same or more Modules enabled than user1; otherwise returns false. |
| and | user1 and user2 |
user1,user2位置必须同时发布模块才就返回true,否则返回false |
| or | user1 or user2 |
user1,user2位置至少发布一个模块就返回true,否则返回false |
| xor | user1 xor user2 |
user1,user2位置至少发布一个模块,但user1,user2不同时发布就返回true,否则返回false Returns true if user1 or user2 but not both have at least 1 enabled Module; otherwise returns false. |

评论 (0)