Liferay Theme的使用范例

 我们可以开发Theme来让系统不使用默认的风格。

当我们用向导创建一个Theme项目并且编译之后,这个项目结构如下图所示:

其中docroot下面所有在_diffs目录外面的都是Liferay从默认的复制过来的资源文件。我们需要改动的都应该在_diffs目录下建平行的目录。

例子1,改动页面或者样式表:

对于改动页面和样式表,这个最简单,只要在_diffs目录下建立平行的目录并且覆盖你要改动的同名文件就可以,比如我们要在主题的顶部添加一行文字叫“Add the content here!”,那么我们只需要在_diffs下建立templates文件夹,然后修改portal_normal.vm,如下代码第27行所示:

 
  
  1. <!DOCTYPE html> 
  2.  
  3. #parse ($init) 
  4.  
  5. <html class="#language("lang.dir")" dir="#language("lang.dir")" lang="$w3c_language_id"> 
  6.  
  7. <head> 
  8.     <title>$the_title - $company_name</title> 
  9.      
  10.     $theme.include($top_head_include) 
  11.  
  12. </head> 
  13.  
  14. <body class="$css_class"> 
  15.  
  16. $theme.include($body_top_include) 
  17.  
  18. #if ($is_signed_in) 
  19.     #dockbar() 
  20. #end 
  21.  
  22. <div id="wrapper"> 
  23.     <a href="#main-content" id="skip-to-content">#language("skip-to-content")</a> 
  24.  
  25.     <header id="banner" role="banner"> 
  26.         <div id="heading"> 
  27.             Add the content here! 
  28.             <h1 class="site-title"> 
  29.                 <a class="$logo_css_class" href="$site_default_url" title="#language("go-to") $site_name"> 
  30.                     <img alt="$logo_description" height="$site_logo_height" src="$site_logo" width="$site_logo_width" /> 
  31.                 </a> 
  32.  
  33. ... 
  34.  
  35. </html> 

我们编译,部署这个Theme到Liferay服务器上,然后在Lifray控制面板中选择我们自定义的主题(charles-theme),则在页面顶部就会显示这行我们新加的内容:

 

 

例子2:改动js:

非外部JS

对于非外部文件的js来说,很简单,只要在_diffs目录下面建立一个main.js,然后main.js里面 有3个方法,依次是在Portal刚启动时调用/每个Portlet加载时调用/Portal加载完毕调用, 我们只直接在AUI.ready()方法体里面(也可以在其他2个方法体里面,看你需求了),加上我们需要执行的js代码就可以了:

比如,我们要控制台打印一行:console.log("AUI().ready()"),则如下面代码第10行所示:

 
  
  1. AUI().ready( 
  2.  
  3.     /* 
  4.     This function gets loaded when all the HTML, not including the portlets, is 
  5.     loaded. 
  6.     */ 
  7.  
  8.     function() { 
  9.          
  10.         console.log("AUI().ready()"); 
  11.          
  12.     } 
  13. ); 
  14.  
  15. Liferay.Portlet.ready( 
  16.  
  17.     /* 
  18.     This function gets loaded after each and every portlet on the page. 
  19.  
  20.     portletId: the current portlet's id 
  21.     node: the Alloy Node object of the current portlet 
  22.     */ 
  23.  
  24.     function(portletId, node) { 
  25.     } 
  26. ); 
  27.  
  28. Liferay.on( 
  29.     'allPortletsReady'
  30.     /* 
  31.     This function gets loaded when everything, including the portlets, is on 
  32.     the page. 
  33.     */ 
  34.  
  35.     function() { 
  36.     } 
  37. ); 

启用这个Theme,然后在浏览器的控制台就可以看到结果了:

 

外部JS:

那么如果我们要让主题Theme使用外部的js文件怎么办呢?

我们首先,还是在_diffs的js目录下定义我们的外部js文件,而且可以在嵌套的目录结构下,比如我们有个外部js文件叫helloworld.js,在charles目录下,如图所示:

这个helloworld.js只做一件事情,打印一条日志:

 
  
  1. function f(){ 
  2.     console.log("helloworld,I am a function in a helloworld.js file!"); 

然后,最关键的,我们必须让页面去读到这个js文件,也就是页面加载js文件,从而这个文件中的全局变量或者函数才可用:

我们在_diffs/templates/portal_normal.vm$theme.include($body_bottom_include)下面一行加入如下代码34行所示:

 
  
  1. <!DOCTYPE html> 
  2.  
  3. #parse ($init) 
  4.  
  5. <html class="#language("lang.dir")" dir="#language("lang.dir")" lang="$w3c_language_id"> 
  6.  
  7. <head> 
  8.     <title>$the_title - $company_name</title> 
  9.      
  10.     $theme.include($top_head_include) 
  11.  
  12. </head> 
  13.  
  14. <body class="$css_class"> 
  15.  
  16. $theme.include($body_top_include) 
  17.  
  18. #if ($is_signed_in) 
  19.     #dockbar() 
  20. #end 
  21.  
  22. <div id="wrapper"> 
  23. ... 
  24.  
  25.     <footer id="footer" role="contentinfo"> 
  26.         <p class="powered-by"> 
  27.             #language("powered-by") <a href="http://www.liferay.com" rel="external">Liferay</a> 
  28.         </p> 
  29.     </footer> 
  30. </div> 
  31.  
  32. $theme.include($body_bottom_include) 
  33.  
  34. #js ("charles-theme/js/charles/helloworld.js") 
  35.  
  36. </body> 
  37.  
  38. $theme.include($bottom_include) 
  39.  
  40. </html> 

 

现在我们_diffs/js/main.js中调用来自外部js文件(helloworld.js)中的函数,如下代码11行所示:

 
  
  1. AUI().ready( 
  2.  
  3.     /* 
  4.     This function gets loaded when all the HTML, not including the portlets, is 
  5.     loaded. 
  6.     */ 
  7.  
  8.     function() { 
  9.          
  10.         console.log("AUI().ready()"); 
  11.         f(); 
  12.          
  13.     } 
  14. ); 
  15.  
  16. Liferay.Portlet.ready( 
  17.  
  18.     /* 
  19.     This function gets loaded after each and every portlet on the page. 
  20.  
  21.     portletId: the current portlet's id 
  22.     node: the Alloy Node object of the current portlet 
  23.     */ 
  24.  
  25.     function(portletId, node) { 
  26.     } 
  27. ); 
  28.  
  29. Liferay.on( 
  30.     'allPortletsReady'
  31.     /* 
  32.     This function gets loaded when everything, including the portlets, is on 
  33.     the page. 
  34.     */ 
  35.  
  36.     function() { 
  37.     } 
  38. ); 

则可以在页面的浏览器控制台正确的这方法执行的结果:(一行控制台记录)

我们也可以在Network面板里面看到这段js被加载:





本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/881912,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值