使用FreeMaker模板来定义栏目中的新闻列表布局

下载FreeMaker简明手册

更复杂一些的示例

文字包围缩略图示例

背景色交替列表显示例

 

    系统提供了向导式的布局设置,同时也提供了使用FreeMaker模板来定义栏目中的新闻列表布局。

以下是一个示例:

<#setting number_format="#">
<table border=0 width="100%" cellspacing=0 cellpadding=2>
<#list newslist as news>
<tr>
     <td nowrap>${news_index+1+(page-1)*countperpage}</td>
    <td><img src='${news.smallimg}' width=40 height=30></td>
    <td width="90%"><div style="height:30px ; width:100px; overflow:hidden;">${news.title}</div></td>
</tr>
<tr>
    <td colspan=3 style='border-bottom:1px dotted #00a0f0; ' align=right>零售价</b>:
            <font color=red>${news.price_ls?string.currency}</font>&nbsp;
            <img src="image/button_buy.jpg" class=asbutton onclick=" alert('ok');">
    </td>
</tr>
</#list>
</table>

显示效果如下图:

 

其中可以使用的变量如下:

变量 含义 示例 示例说明
news_index 在循环中用来表示当前索引号,以0开始 <#list newslist as news>
${news_index+1+(page-1)*countperpage}
</#list>
对新闻列表进行循环,输出它是第几条新闻。其中因为news_index是从0开始,所以要+1,(page-1)*countperpage表示当前页号×每页显示条数。整个表达式表示的是当前新闻在所有新闻中的行数
page 表示当前显示的是第几页,page=1表示第一页
countperpage 表示每页显示几条新闻
subjectid 表示当前的栏目的ID

${news.smallimg}

<#list newslist as news>
<a href='viewnews.jsp?subjectid=${subjectid}&
newsid=${news.id}'>${news.title} </a><br>
</#list>

构造一个新闻标题列表,点击列表查看新闻
newslist 是新闻列表,使用<#list newslist as news>后可以用news.xxx来访问某个新闻的属性

表示取新闻的指定属性,比如news.id表示新闻的id ,news.title 表示新闻的标题等,所有新闻的属性,参看news_news.sql

当栏目的数据是从指定数据源中用指定语句取出时,可以用news.字段名来访问字段的内容。

newslist?size 新闻列表里有多少条新闻    
subsubjectlist 是当前栏目直属下级栏目列表,使用<#list subsubjectlist as subs>后可以用subs.xxx来访问某个子栏目的属性 提示:如果只显示子栏目,而不需要显示新闻列表,在样式设置中请把显示的行数设置成比较大的数,比如999999 ,这样在栏目列表下就不会有一个分页导航条

<#setting number_format="#">
<table border=0 width="610" cellspacing=0 cellpadding=0 >

<#list subsubjectlist as subs>
<tr><td height="60" width=604 valign="center" background="customize/jc2j/images/book.jpg"
style="background-repeat:no-repeat;
font-size:16px; padding-left:60px;">
<a href="subject.jsp?subjectid=${subs.id}" >
<b>${subs.name}</b>
</a> </td>
</tr>

</#list>
</table>

 
subsubjectlist?size 本栏目下有多少个子栏目(禁用的栏目不取)    

 

关于语法:

循环

<#list newslist as news>
......
</#list>

上述语法表示循环访问新闻列表中的每个新闻记录,使用news对象来访问其中的新闻。用news.字段名来访问新闻中指定字段的内容。

条件

<#if subsubjectlist?size != 0 >

<#else>

</#if>

上述条件表示当子栏目个数不为0时,做一些处理

 

关于格式:

  • <#setting number_format="#">用来设置数字格式,表示不显示千分位逗号
  • <div style="height:30px ; width:100px; overflow:hidden;">${news.title}</div> 把标题限定是100像素宽,30像素高,当字数比较多时,会自动裁剪,而不会挤破版面。
  •