四月 10

Joomla 模板常用技巧函数收集(不断更新)

在平时开发 Joomla 模板的时候收集了一些极为常见也非常有用的函数,现列出来方便制作 Joomla 模板时查阅,具体内容将不断更新,需要注意的是该页代码在模板中使用时均需嵌套在 <?php  ?> 标签里面(地球人都知道了)更多完整函数可查阅 Joomla 的官方 API 网站 http://api.joomla.org/ 这里列出来的仅是制作模板主题时经常使用到的:

0、关于 jdoc 的使用:

1
2
3
4
5
6
7
8
<jdoc:include type="head" />
<jdoc:include type="message" />
<jdoc:include type="component" />
<jdoc:include type="installation" />
<jdoc:include type="module" name="logo" />
<jdoc:include type="module" name="menu" />
<jdoc:include type="modules" name="side" style="xhtml" />
<jdoc:include type="modules" name="main" style="xhtml" />

其中 type 属性为 jdoc 元素中要渲染的内容类型,属性值可以是下列的值:

head —— 填充当前页<head>标签里的 style script meta 等元素;

message —— 用于在模板显示系统信息或报错信息,发布模板时可以删掉;

component —— 用于页面主体内容输出;

installation —— 仅用于模板安装,渲染安装步骤中的一个页面主内容,通常不需要;

以上内容类型通常在一个页面应当仅出现一次,并且通常没有其他属性,如 name style 等属性;

module —— 渲染由 name 属性指定的单一模块,模块必须是发布的并且当前用户有权访问,另外还可以有 layout 及 controller 属性;

modules —— 渲染由模板 xml 文件中指定的位置,需要留意的是一个位置可以放置多个模块,这点非常重要,能为你节约很多时间和成本;

style —— 是对于 module 及 modules 类型的一个可选属性,这一属性指定了模块被渲染风格,如果没指定将默认为 none 属性,所有属性在 /templates/system/html/modules.php 中有声明,包括有 table horz xhtml rounded outline 当然模板设计者可以在你自己的模板同样位置对其进行自定义,否则建议使用 xhtml 类型。

1、判断当前页面是否为首页:getActive(); getDefault();

1
2
$menu = & JSite::getMenu();
if( $menu->getActive() == $menu->getDefault() ){ /* 这里显示首页代码 */ }else{ /* 这里是非首页代码 */ }

解释一下:这里使用 getActive 函数获取当前页面并与默认页面对比;

2、根据位置是否存在内容设置一些宽度:countModules();

1
2
3
if ($this->countModules('west and east') == 0) { $coreWidth = "100%"; }
if ($this->countModules('west or east') == 1) { $coreWidth = "75%"; }
if ($this->countModules('west and east') == 1) { $coreWidth = "50%"; }

解释一下:通常多栏页面在没有其中一栏或多栏内容的情况下需要将其他栏目做宽度调整,这里使用 countModules() 函数判断是否存在一个或多个位置。

示例中展示的是三栏情况下,假使三栏分别为三个 west core east 位置,其中 west 和 east 为左右两个位置内容为可选,而 core 位置为主体位置内容为必须,通过判断得出四种情况即:无 west 栏、无 east 栏、仅 core 栏及同时具有三栏。示例代码第一行为仅有 core 栏即 west 和 east 都不存在的情况时赋 coreWidth 为 100% 宽度;第二行为仅有 west 或 east 栏其中之一时赋 coreWidth 为 75% 宽度;第三行为同时存在三栏时附 coreWidth 为 50% 宽度。取得了 coreWidth 值之后就可以在 html 代码中引用了,例如:

1
2
3
<div style="width:<?php echo $coreWidth; ?>;">
    <jdoc:include type="modules" name="core" />
</div>

当然,这里省略了 CSS 样式,你需要在 CSS 里面定义 west 和 east 的宽度等参数。

3、根据位置是否有内容控制输出:countModules();

1
2
3
4
5
6
7
<?php if ( $this->countModules('west or core or east') == 1 ) : ?>
<div class="body">
    <div id="west"><jdoc:include type="modules" name="west" style="xhtml" /></div>
    <div id="core"><jdoc:include type="modules" name="core" style="xhtml" /></div>
    <div id="east"><jdoc:include type="modules" name="east" style="xhtml" /></div>
</div>
<?php endif; ?>

解释一下:这种使用其实是对上面第 2 点的一个补充,通常在输出位置的时候我们喜欢使用一个 div 包裹输出的位置内容用来做一些效果,但如果该位置没有输出,或是一个大通栏里面包括的三个位置都没有输出,那么这个通栏就没有必要输出了,使用 countModules 同时判断 west core east 位置是否有任意一个位置有内容输出,如果有任一位置有输出便输出 <div class=”body”> </div> 中间的内容。

4、方便的查看当前模板位置情况:index.php?tp=1

示例代码:网站 URL 路径 + index.php?tp=1 (如 http://127.0.0.1/index.php?tp=1 等)

解释一下:这个地址参数用来方便的查看当前页面的模板上的位置情况,及相应的输出模式,其中的 127.0.0.1 需要根据具体地址修改。

5、直接在模板调用模块:

1
2
3
4
5
6
require_once (JPATH_BASE .DS. 'modules' .DS. 'mod_latestnews' .DS. 'helper.php');
$params = new JParameter();
$params -> set('catid',5);
$params -> set('count',3);
$list = modLatestNewsHelper::getList($params);
require(JModuleHelper::getLayoutPath('mod_latestnews'));

解释一下:这段代码就是在模板中直接调用 LatestNews 模块,并设置分类 ID 28 显示 3 篇文章。

6、……

分享到:

本文网址:http://anyLiv.com/blog/920 转载需注明出处!
This site is licensed under a Creative Commons BY-NC-SA 3.0 License.
本站使用 创作共用版权协议 转载本站内容也必须遵循"署名-非商业性使用-相同方式共享"的共同创作协议。

文章评论:

  1. anyLiv 说:

    忘记说明,仅适用与 1.5.x 版本。

    anyLiv:……

 

发表评论:

NOTICE: You should type some Chinese Word in your comment to pass the spam-check, thanks !

小提示:请务必填写正确的邮箱地址(推荐 QQ 邮箱)以便能及时收到我的回复,另未包含中文的评论将被过滤!


谷歌广告 gAdSense


Themed by anyLiv. Copyright © 2010. Some right reserved.