大家都知道Liferay的内核框架由一组Velocity模板文件组成的,它们位于$LIFERAY_HOME\liferay-portal-6.1.0-ce-ga1\tomcat-7.0.23\webapps\ROOT\html\themes\classic\templates 目录下,今天有同事问我,这些模板中的变量在哪里定义的。

 

为了解决这个问题,我们找到了portal-impl.jar中的VelocityVariableImpl 类文件,于是一目了然。因为 portal-normal.vm中的第一行会解析其他文件,被解析的文件在变量init中,如下:

 
  
  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. </head> 
  12. ...
     

 

见03行,而被解析的这个文件,就定义了绝大多数模板中用到的变量,那么这个文件在哪里呢?我们可以参见VelocityVariableImpl中的定义:

 
  
  1. velocityContext.put( 
  2.     "init",  
  3.     themeDisplay.getPathContext() +  
  4.     "_SERVLET_CONTEXT_" +  
  5.     "/html/themes/_unstyled/templates/init.vm"); 
  6.  
  7.   velocityContext.put( 
  8.     "portletGroupId"new Long(themeDisplay.getScopeGroupId())); 
  9.  
  10. Theme theme = (Theme)request.getAttribute("THEME"); 

所以,被parse的init变量 代表的文件是在 ROOT 应用下的/html/themes/_unstyled/templates/init.vm,我们打开看下,果然这里面都是变量定义:

 

因为这个文件很长,所以这里略去

 
  
  1. ## ---------- Common variables ---------- ## 
  2.  
  3. #set ($theme_display = $themeDisplay) 
  4. #set ($portlet_display = $portletDisplay) 
  5.  
  6. #set ($theme_timestamp = $themeDisplay.getTheme().getTimestamp()) 
  7. #set ($theme_settings = $themeDisplay.getThemeSettings()) 
  8.  
  9. ... 

 

结论:

在Liferay框架中的模板文件中的变量,大多数是在init.vm中定义的,少量是直接定义在VelocityVariableImpl类中的

 

 

实战:

比如,同事问我,在portal-normal.vm的第47-53行中的 $content_include变量定义在哪里:

 
  
  1. #if ($selectable) 
  2.     $theme.include($content_include
  3. #else 
  4.     $portletDisplay.recycle() 
  5.  
  6.     $portletDisplay.setTitle($the_title) 
  7.  
  8.     $theme.wrapPortlet("portlet.vm"$content_include
  9. #end 

 

我们在init.vm中进行查找,不难发现这个变量定义为:

 
  
  1. #set ($bottom_include = "$dir_include/common/themes/bottom.jsp"
  2. #set ($bottom_ext_include = $bottom_include) 
  3. #set ($content_include = "$dir_include$tilesContent"
  4. #set ($top_head_include = "$dir_include/common/themes/top_head.jsp"
  5. #set ($top_messages_include = "$dir_include/common/themes/top_messages.jsp"

$dir_include$tilesContent,所以我们还需要递归的继续解析下去:

 

我们在init.vm中继续查找,不难找到$dir_include的变量定义:

 
  
  1. #if ($is_portlet_page && $theme_display.isWapTheme()) 
  2.                 #set ($dir_include = "/wap"
  3. #else 
  4.                 #set ($dir_include = "/html"
  5. #end 

所以,因为我们的官方默认例子不是被wap的,所以$dir_include为/html

 

接下来我们找$tilesContent的定义。很可惜,它没有被定义在init.vm中,所以我们回到VelocityVariableImpl类中,再这里,我们找到了定义:

 
  
  1. String tilesContent = (String)componentContext.getAttribute("content"); 
  2.  
  3. themeDisplay.setTilesContent(tilesContent); 
  4.  
  5. velocityContext.put("tilesContent", tilesContent); 

所以,$tilesContent 是一个程序运行期间动态传递过来的变量:

 

综上所述:同事问我的portal-normal.vm文件中$content_include的变量的值,是在程序运行中动态解析的,默认情况下,它是 “/html” 与程序运行时候动态获取content属性值,然后这2个值拼接起来得到的值。