VTL-vm模板的變數用法

來源:互聯網
上載者:User

載入foot模組頁
#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也可以看做一個字串來操作
$item.Substring(0,15) 取出從0開始的15個字元
------------------------------------------------------------------------------------------
//$listType才能用ToString()時,不能用$!{listType}不會出錯不能被ToString();
//$listType.toString('f2'),四捨五入,保留兩小數。toString();可以加很多的參數,查查參數表.
#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">請選擇</option>
 #foreach($item in $items)
 <option value="$!{item.Id}">$!{item.PositionName}</li>
 #end
 </select>
#end

//#foreach()的迴圈用法。
<select id="sels">
 #foreach($time in [1..$checkorder.TimeCount])
  <option value=$time>$time</option>
 #end
</select>
---------------------------------------------------------------------------------------
2008-1-5:作VTL運算式,Castle工程
VTL運算式不區分大小寫,可以調用方法,屬性,
//$velocityCount是統計迴圈的次數,從1弄開始計算
#set($foo="Holle") ${foo}world
##:是單行注釋。#**#:多行注釋。
<table>
#foreach($info in $array)
 <tr>
 <td>$velocityCount ##統計迴圈的次數從1開始計</td>
 <td>$!{info.name}</td>
 <td>$!{info.Password}</td>
 <td>$!{info.Age}</td>
 <td>$!{info.getvalue}</td>
 </tr>
#end
</table>
迴圈Hashtable是的用法
$allProducts是Hashtable的對象
#foreach($var in $allProducts)
 ##var.key:擷取鍵 var.value:擷取值
 $!{var.key}->$!{var.value}
#end
//另一種Hashtable的迴圈用法
vm頁面用關鍵字點鍵名。
<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());
}
-------------------------------------------------------------------------------------
##是可以用來輸出字面的意思是原樣輸出(注釋用的)
#literal()
#foreach($woogie in $boogie)
 nothing will happen to $woogie
#end
#end
-----------------------------------------------------------------------------------------
//$type裡面的一些方法,比較有用。
#if($type.ToLower() != "noservice")  ToLower():是小寫字串的方法。
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(","))取得逗號位置
$r.Phone.Substring(0,7)****:取出電話號碼為:1371093****
-----------------------------------------------------------------------------------------
this.ProprtBag.Add("time",DateTime.Now);
//用來判斷是否為空白
#if($time!="")
 <h1>$!time</h1>
#end
//當有數組是判斷是否是數
#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
-----------------------------------------------------------------------------------------------------
用<!--  -->:來注釋頁面上用的,不能有套用會無法注釋的如:<!-- <!-- --> -->
---------------------------------------------------------------------------------------------------------
//會依次顯示,當翻頁面時也會接著上一頁繼續顯示編號。其中14為每一頁顯示的條數,根據需要而調整
#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="邀請會員啟用空間" 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="進入TA的個人王國" src="../images/Ties.gif" /></a>
#end
用來判斷為空白值時的處理
-----------------------------------------------------------------------------------------------------------
DataTable或者DataSet的頁面資料載入。
---------------------*.vm----------------------------------------------------------------------------------
頁面上寫的是
#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資料繫結頁面------------------------------------
#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
---------------------背景代碼----------------------------------------------------
using(DataSet ds=_cardsSituation.ByCardType(CurrentMerchant.UserName,year,month))
{
 PropertyBag.Add("ds",ds);
}
-------------------------------------------------------------------------------------
//時間日期的判斷
#if($!{Member.Isusedate.ToShortDateString()} =="0001-1-1")
 ----
#else
 $!{Member.Isusedate.ToShortDateString()}
#end
-------------------------------------------------------------------------------------
//用於計算剩餘的值
#set($Balance = $!item.Money - $!item.FactMoney)
<td  class="last">$Balance.toString('f2')</td>
-------------------------------------------------------------------------------------
//用來顯示DataTable dt類型資料的方法。
#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------------------------------------
頁面上寫的是
#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資料繫結頁面-----------------------------------------------------
#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
---------------------背景代碼
using(DataSet ds=_cardsSituation.ByCardType(CurrentMerchant.UserName,year,month))
{
 PropertyBag.Add("ds",ds);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.