VTL-vm模板的变量用法
加載foot模塊頁(yè)
#parse("foot.vm")
#foreach($item in $tables)
?#set($strEnd = $item.Length - 1)
?#set($sheetName = $item.Substring(0, $strEnd))
?<option value="$item">$sheetName</option>
#end
$strEnd也可以看做一個(gè)字符串來(lái)操作
$item.Substring(0,15) 取出從0開(kāi)始的15個(gè)字符
------------------------------------------------------------------------------------------
//$listType才能用ToString()時(shí),不能用$!{listType}不會(huì)出錯(cuò)不能被ToString();
//$listType.toString('f2'),四舍五入,保留兩小數(shù)。toString();可以加很多的參數(shù),查查參數(shù)表.
#if($listType.ToString() == "List")
?#foreach($item in $items)
?<li><a href="FindByPosition.page?positionId=$!{item.Id}">$!{item.PositionName}</a></li>
?#end
#elseif($listType.ToString() == "Select")
?<select id="position" name="employee.Position">
?<option value="-1">請(qǐng)選擇</option>
?#foreach($item in $items)
?<option value="$!{item.Id}">$!{item.PositionName}</li>
?#end
?</select>
#end
//#foreach()的循環(huán)用法。
<select id="sels">
?#foreach($time in [1..$checkorder.TimeCount])
??<option value=$time>$time</option>
?#end
</select>
---------------------------------------------------------------------------------------
2008-1-5:作VTL表達(dá)式,Castle工程
VTL表達(dá)式不區(qū)分大小寫(xiě),可以調(diào)用方法,屬性,
//$velocityCount是統(tǒng)計(jì)循環(huán)的次數(shù),從1弄開(kāi)始計(jì)算
#set($foo="Holle") ${foo}world
##:是單行注釋。#**#:多行注釋。
<table>
#foreach($info in $array)
?<tr>
?<td>$velocityCount ##統(tǒng)計(jì)循環(huán)的次數(shù)從1開(kāi)始計(jì)</td>
?<td>$!{info.name}</td>
?<td>$!{info.Password}</td>
?<td>$!{info.Age}</td>
?<td>$!{info.getvalue}</td>
?</tr>
#end
</table>
循環(huán)Hashtable是的用法
$allProducts是Hashtable的對(duì)象
#foreach($var in $allProducts)
?##var.key:獲取鍵 var.value:獲取值
?$!{var.key}->$!{var.value}
#end
//另一種Hashtable的循環(huán)用法
vm頁(yè)面用關(guān)鍵字點(diǎn)鍵名。
<h3>$!{hash.aa}</h3>
<h3>$!{hash.bb}</h3>
<h3>$!{hash.cc}</h3>
<h3>$!{hash.dd}</h3>
Controll層里
public void Index()
{
?Hashtable hash = new Hashtable();
?hash.Add("aa","one");
?hash.Add("bb",DateTime.MaxValue);
?hash.Add("cc",DateTime.MinValue);
?hash.Add("dd",DateTime.Now.ToString());
}
-------------------------------------------------------------------------------------
##是可以用來(lái)輸出字面的意思是原樣輸出(注釋用的)
#literal()
#foreach($woogie in $boogie)
?nothing will happen to $woogie
#end
#end
-----------------------------------------------------------------------------------------
//$type里面的一些方法,比較有用。
#if($type.ToLower() != "noservice")? ToLower():是小寫(xiě)字符串的方法。
ToString():
#set($index=$item.Content.IndexOf(","))
$!{item.ReceiveTime.ToString("yyyy-mm-dd HH:mm")}
$!{consumeLog.OperateDate.ToString("yyyy-MM-dd HH:mm")}
$!{consume.ConsumeDate.ToString("d")}
$!{consume.ConsumeDate.ToString("t")}
$!{sign.FirstStartTime.ToShortDateString()}與$!{sign.FirstStartTime.ToString("yyyy-MM-dd")}效果一樣的。
#set($index = $customer.IndexOf(","))取得逗號(hào)位置
$r.Phone.Substring(0,7)****:取出電話號(hào)碼為:1371093****
-----------------------------------------------------------------------------------------
this.ProprtBag.Add("time",DateTime.Now);
//用來(lái)判斷是否為空
#if($time!="")
?<h1>$!time</h1>
#end
//當(dāng)有數(shù)組是判斷是否是數(shù)
#if($items.Count>0)
?#foreach($item in $items)
??$!{item}
?#end
#end
-----------------------------------------------------------------------------------------
#elseif:多重條件判斷
#if(!$order)
?100001
#elseif($order.CustomerId && $order.CustomerId != "" && $order.CustomerId != $userName)
?100002
#elseif($order && ($order.CustomerId == "" || !$order.CustomerId || $order.CustomerId == $userName))
?100003
#end
-----------------------------------------------------------------------------------------------------
用<!--? -->:來(lái)注釋頁(yè)面上用的,不能有套用會(huì)無(wú)法注釋的如:<!-- <!-- --> -->
---------------------------------------------------------------------------------------------------------
//會(huì)依次顯示,當(dāng)翻頁(yè)面時(shí)也會(huì)接著上一頁(yè)繼續(xù)顯示編號(hào)。其中14為每一頁(yè)顯示的條數(shù),根據(jù)需要而調(diào)整
#if(!$page || $page <= 0)
?#set($page = 1)
#end
#set($rowIndex = ($page - 1) * 14 )
#foreach($log in $logDt.Rows)
?$rowIndex
#end
---------------------------------------------------------------------------------------------------------
$!{consumeLog.OperateDate.ToString("yyyy-MM-dd HH:mm")}
---------------------------------------------------------------------------------------------------------
#if(!$log.UserName || $log.UserName == "")
?<a title="邀請(qǐng)會(huì)員激活空間" href="javascript:sendMessage('$!{log.CardId}')"><img alt="未綁定" src="../images/noTies.gif" /></a>
?<div id="ajaxmsg"></div>
#else
?<a href="http://$!%7blog.username%7d.i.myking.cn/" target="_blank"><img alt="進(jìn)入TA的個(gè)人王國(guó)" src="../images/Ties.gif" /></a>
#end
用來(lái)判斷為空值時(shí)的處理
-----------------------------------------------------------------------------------------------------------
DataTable或者DataSet的頁(yè)面數(shù)據(jù)加載。
---------------------*.vm----------------------------------------------------------------------------------
頁(yè)面上寫(xiě)的是
#foreach($log in $table.Rows)
?$!{log.Id}>>>$!{log.User}>>>$!{log.Phone}
#end
----------------controller----------------------------------
public void Index()
{
?DataTable table = new DataTable();
?table.Columns.Add("Id",typeof(int));
?table.Columns.Add("User",typeof(string));
?table.Columns.Add("Phone",typeof(string));
?for(int i=0;i<3;i++)
?{
??DataRow row = table.NewRow();
??row["Id"]=i;
??row["User"]="cheng";
??row["Phone"]="2222222";
??table.Rows.Add(row);
?}
?this.ProperBag.Add("table",table);
}
----------------------------DateSet數(shù)據(jù)綁定頁(yè)面------------------------------------
#foreach($t in $ds.Tables)
?<table class="month">
?<tr>
??#foreach($col in $t.Columns)
???<th>$col.ColumnName.Replace("日","")</th>
??#end
?<tr>
?#foreach($r in $t.Rows)
?<tr>
??#foreach($c in $r.ItemArray)
???<td align="center">
???#if($c==0)--#end
??#if($c>0) $c.ToString() #end</td>
?#end
?</tr>
?#end
?</table>
#end
---------------------后臺(tái)的代碼----------------------------------------------------
using(DataSet ds=_cardsSituation.ByCardType(CurrentMerchant.UserName,year,month))
{
?PropertyBag.Add("ds",ds);
}
-------------------------------------------------------------------------------------
//時(shí)間日期的判斷
#if($!{Member.Isusedate.ToShortDateString()} =="0001-1-1")
?----
#else
?$!{Member.Isusedate.ToShortDateString()}
#end
-------------------------------------------------------------------------------------
//用于計(jì)算剩余的值
#set($Balance = $!item.Money - $!item.FactMoney)
<td? class="last">$Balance.toString('f2')</td>
-------------------------------------------------------------------------------------
//用來(lái)顯示DataTable dt類(lèi)型數(shù)據(jù)的方法。
#foreach($col in $dt.Columns)
?<th>$col.ColumnName</th>
#end
<tr>
#foreach($dr in $dt.Rows)
?<tr>
?#foreach($c in $dr.ItemArray)
??<td align="center">
??#if(!$c || $c.ToString()=="" || $c.ToString()=="0")
??--
??#else
??$c.ToString()
??#end</td>
?#end
?</tr>
#end
--------------------------------------------------------------------------------------
---------------------*.vm------------------------------------
頁(yè)面上寫(xiě)的是
#foreach($log in $table.Rows)
?$!{log.Id}>>>$!{log.User}>>>$!{log.Phone}
#end
----------------controller----------------------------------
public void Index()
{
?DataTable table = new DataTable();
?table.Columns.Add("Id",typeof(int));
?table.Columns.Add("User",typeof(string));
?table.Columns.Add("Phone",typeof(string));
?for(int i=0;i<3;i++)
?{
??DataRow row = table.NewRow();
??row["Id"]=i;
??row["User"]="cheng";
??row["Phone"]="2222222";
??table.Rows.Add(row);
?}
?this.ProperBag.Add("table",table);
}
----------------------------DateSet數(shù)據(jù)綁定頁(yè)面-----------------------------------------------------
#foreach($t in $ds.Tables)
??<table class="month">
??<tr>
??#foreach($col in $t.Columns)
???<th>$col.ColumnName.Replace("日","")</th>
??#end
??<tr>
??#foreach($r in $t.Rows)
???<tr>
???#foreach($c in $r.ItemArray)
????<td align="center">
????#if($c==0)--#end
????#if($c>0) $c.ToString() #end</td>
???#end
???</tr>
??#end
??</table>
?#end
---------------------后臺(tái)的代碼
using(DataSet ds=_cardsSituation.ByCardType(CurrentMerchant.UserName,year,month))
{
?PropertyBag.Add("ds",ds);
}
轉(zhuǎn)載于:https://www.cnblogs.com/wei9931/archive/2009/05/23/1487931.html
總結(jié)
以上是生活随笔為你收集整理的VTL-vm模板的变量用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 求世间始终你好歌词!
- 下一篇: 高冷个性签名女生霸气