jforum 动态切换模版

 

要想动态切换模板,首先把default文件夹复制一份,改成新的名称放到同目录下,然后修改你的新模板,无论样式和结构都可以修改,但是文件名称和里面的变量不要动,然后就是修改你的各个模板的header.htm文件。在里面加上切换按钮,每个按钮赋javascript的事件,代码如下:

cookieString="templateName=mosou;path=/";

        

document.cookie=cookieString;

document.location.reload();

从上面代码可以看出是把模板名称保存在cookie里,templateName为模板名称,每个模板的header.htm的切换按钮都要加上这个javascript代码。接下来就是修改两个java文件,首先修改net.jforum.ControllerUtils文件,把prepareTemplateContext方法里的context.put("templateName", SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR));这段代码换为下面的代码:

  Cookie cookie=this.getCookieTemplate("templateName");

  if(cookie != null ){

   

   System.out.println(cookie.getValue());

   context.put("templateName", cookie.getValue());

  }else{

   context.put("templateName", SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR));

  }

最后再修改net.jforum.net.jforum这个文件,把process这个方法里的try块换成下面的代码:

Cookie cookie=this.getCookie("templateName");

         String dir="";

      if(cookie != null ){

       dir=cookie.getValue();

      }else{

       dir=SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR);

      }

         

         

         return JForumExecutionContext.templateConfig().getTemplate(

                new StringBuffer(dir).

                append('/').append(this.templateName).toString());

再在这个类里加入以下方法:

 public static Cookie getCookie(String name)

 {

  Cookie[] cookies = JForumExecutionContext.getRequest().getCookies();

  if (cookies != null) {

   for (int i = 0; i < cookies.length; i++) {

    Cookie c = cookies[i];

    if (c.getName().equals(name)) {

     return c;

    }

   }

  }

  return null;

 }

到此你的jforum论坛就可以手动更换模板和风格了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值