Gallery:如何在主/根页面上添加自定义内容

来自站长百科
Firebrance讨论 | 贡献2008年11月10日 (一) 15:22的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

如果你想在主页中为访客显示自定义消息的话,当然也可以是别样的层,颜色,头等等,那么就需要如此:

首先,我们需要知道根目录相册的id,这样才能将其与当前相册id进行比较。 通过编辑当前模板的theme.inc文件,就可以得到了。('完整'路径是themes/<your theme>/theme.inc)
由于在相册页面上需要此信息,我们就要将此置入函数showAlbumPage(){ }之中。

/* 获取rootId */
list ($ret, $rootId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.rootAlbum');
if ($ret) {
    return array($ret, null);
}

好了,现在我们获得了rootId,即$rootId,现在将其与当前项目id($item)进行比较:

if ($item->getId() == $rootId) {
}

现在我们就可以向此if {}中插入任何内容了。
我们希望能够对某个变量进行操作,这样就能在模板中使用了(.tpl文件)。

if ($item->getId() == $rootId) {
    $isItRoot = true;
}

只需要做的一件事就是将此变量发送至模板。使用下面的语句就可以办到了:
(Format is setVariable('nameInTheTemplate', $currentVariable) )

$template->setVariable('isItRoot', $isItRoot);

现在对theme.inc的处理就完成了》 我们需要添加的代码如下:

list ($ret, $rootId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.rootAlbum');
if ($ret) {
    return array($ret, null);
}
if ($item->getId() == $rootId) {
    $isItRoot = true;
}
$template->setVariable('isItRoot', $isItRoot);

现在来编辑模板文件,并将我们自己的内容添加进去。现在需要的模板文件是album.tpl。('完整'路径是themes/<your theme>/templates/album.tpl)
很简单,我们有var $isItRoot,在根目录相册中是true,而其他情况下则是false(或null)。
现在我们来试试看添加问候消息吧:

{if $isItRoot} Hi! Welcome in my gallery! {/if}

不错吧?;)

如果你想将其与注册用户(与否的)检查结合使用的话 ($user.isRegisteredUser)(游客则是($user.isGuest),你可以:

{if $isItRoot} 
  {if $user.isRegisteredUser}   
   Hello {$user.userName}, welcome on the main page!
  {else}
   Hello guest, welcome on the main page!
  {/if}
{else}
This is NOT the main page dude.
{/if}

etc :)

注:此教程针对Gallery 2.2。2007年11月3日

另一种办法:http://gallery.menalto.com/node/70763#comment-255788