[笔记]巧用Plugin管理模板导航条

This is a SiteLog of Easun's WebBlog.

其实就是说本站上面的无序行表导航条:)
但是有个 加重 的选择。根据不同页面加重的部分不一样,比如主页加重的就是 "Home" ,而 BookMarks 选种的就是 "Bookmarks";
我的 Blog 采用的模块化设计,整个头部都是一样的,原来的加重是用一段 JS 来实现的,但是自己觉得不太满意,既然是静态化设计,何不彻底静态化?既然后台是 MT ,那就 MT plugin实现吧。
分析了一下,最简单采用add_global_filter来过滤:)
我的顶部模块名字为 Blog:header 模块,原来加载的办法是 MT 的 MTinclude 办法,如下:

<$MTInclude module="Blog:header"$>

修改后的代码如下

<$MTInclude module="Blog:header" do_notice_id="top_blog_home"$>

添加的 global_filter 名字为 do_notice_id ,其中"top_blog_home" 是我导航菜单中需要加重的部分,可以根据不同页面来换成你加重的id。

实现代码如下:

# Copyright 2007 EasunLee Easun.org # http://easun.org/

package MTPlugins::QiukNav;

use strict;
use warnings;
use MT;
use MT::Template::Context;

if (MT->version_number >= 3) {
use MT::Plugin;
my $plugin;
$plugin = new MT::Plugin(
{
name => 'QiukNav',
version =>'0.1',
description => 'QiukNav',
author_name => 'EasunLee',
author_link => 'http://easun.org/blog/',
}
);
MT->add_plugin($plugin);
}

MT::Template::Context->add_global_filter(do_notice_id => \&_hdlr_easun_do_notice_id);

sub _hdlr_easun_do_notice_id {
my ($str, $param, $ctx) = @_;
$str =~ s/class="notice"//ig;
$str =~ s/(id\="$param")/$1 class="notice"/ig;
$str;
}
1;

保存以上代码。存成 qnav.pl ,放在 MT 的 plugins目录下面,即可生效。
希望对大家有所有帮助:)