.net 開源模板引擎jntemplate 教程:基礎篇之語法

1、基本概念

上一篇咱們簡單的介紹了jntemplate並寫了一個hello world(若是沒有看過的,點擊查看),本文將繼續介紹jntemplate的模板語法。html

咱們在講解語法前,首先要了解一下標籤的概念。在jntemplate中,標籤特指用來包含模板代碼的語法塊,它是基本的呈現單元,在模板解析完成後,該語法塊將會被替換成具體的數據或者內容。git

標籤一般使用${開頭,而且以}結尾,絕大部分標籤能夠省略大括號進行簡寫,如${model.UserName} 能夠簡寫爲 $model.UserNamegithub

下面將重點介紹幾個經常使用的語法標籤:c#

注意:標籤的符號是能夠自定義的,好比你能夠自定義爲{{model.UserName}} 或者@{model.UserName},本文爲了方便講解,僅以默認配置爲準。數組

2、變量

用法:用於在模板中輸出一個變量,該變量能夠是任何對象。如:${var},能夠簡寫爲$var,其中var 爲變量名,變量名只能是字母,下劃線與數字的組合,且必須以字母開頭。mvc

例:asp.net

var template = Engine.CreateTemplate("<h1>$title</h1>!");
template.Set("title", "jntemplate");
template.Render(Console.Out);

3、屬性與字段

用法: 用於訪問對象屬性或者字段,用法與c#相似,字段與屬性必須是公開的(public),v2.0.0 中暫時不支持匿名對象的訪問。如:${model.Name},能夠簡寫爲$model.Name.函數

例一:spa

var template = Engine.CreateTemplate("<h1>$model.Title</h1>!");
template.Set("model", new Site{ Title="jntemplate" });
template.Render(Console.Out);

若是訪問靜態屬性或字段,須要經過template.SetStaticType(...)來指定靜態對象類型。.net

例二:

var templateContent = "${DateTime.Now}";
var template = Engine.CreateTemplate(templateContent);
template.SetStaticType("DateTime", typeof(DateTime));
template.Render(Console.Out);

! 注意:當前版本暫時不支持匿名對象訪問。

4、索引

用法:用於訪問數組或者IList<T>及其它支持索引的對象,用法與c#相似,如${arr[1]}

例:

var template = Engine.CreateTemplate("${arr[0]}");
template.SetStaticType("arr", new int[]{ 1,2,3});
template.Render(Console.Out);

5、函數方法

用法:用於調用對象實例方法,靜態方法,或者委託。如:${func(p1,p2....) },能夠簡寫爲$func(p1,p2....)

注意:方法必須是公開的(public),若是是私有的(private)則沒法訪問。

例一(實例方法):

Class1類

public class Class1
{
    public int Add(int a,int b)
    {
        return a+b; 
    }
}

模板代碼:

var template = Engine.CreateTemplate("$obj.Add(1,2)");
template.Set("obj", new Class1());
template.Render(Console.Out);

例二(靜態方法):

var templateContent = "${string.Concat(\"str1\",\"str2\")}";
var template = Engine.CreateTemplate(templateContent);
template.SetStaticType("string", typeof(string));
template.Render(Console.Out);

例三(委託方法):

var template = Engine.CreateTemplate("$add(8,-2)");
template.Set<Func<int, int, int>>("add", (x, y) =>
{
    return x + y;
});
template.Render(Console.Out);

6、邏輯判斷(IF)

用法:用於處理代碼邏輯,等同於c#裏面的ifelse if ,支持如下邏輯運算符:大於(>)小於(<)大於等於(>=)小於等於(<=)等於(==)不等於(!=)或者(||)而且(&&)

例一:

模板:demo.html

<span>
$if(id>0)
     編號大於零
$elif(id==0)
    編號等於零
$else
    編號小於零
$end
</span>

後臺代碼:

var template = Engine.LoadTemplate(@"c:\demo.html");
template.Set("id",15);
template.Render(Console.Out);

注意:else if 能夠寫做$elseif 也能夠寫做 $elif。標籤必須以$end結束

7、列表迭代(Foreach)

用法:用來遍歷輸出一個列表,等同於c#foreach,目標能夠是數組或者實現了IEnumerable 接口的任意對象.

例一:

模板:demo.html

$foreach(model in list)
<ul>
 <li><a href="${model.Url}">${model.Title}</a></li></li>
</ul>
$end
var template = Engine.LoadTemplate(@"c:\demo.html");
template.Set("list",new NewInfo[]{ ... });
template.Render(Console.Out);

$foreach(model in list) 也能夠寫做 $for(model in list) ,必須使用$end 結束標籤。

8、模板引用

用法:用於引用某一個公共模板,有二種寫法$load("路徑")$inclub("路徑"):

  • load 引用並解析模板
  • inclub:引用模板(不解析),適用於不須要解析的文件,好比JS,CSS等

例:

$load("public/header.html")
<div>這是內容</div>

9、總結

本文介紹了jntemplate經常使用的模板語法,包括變量,屬性,方法,邏輯判斷,迭代與模板引用這幾大塊,只要靈活組合使用,徹底能夠知足平常開發了。

部分不怎麼經常使用的語法你們能夠自行能夠參考官方文檔。

下一節,咱們將介紹如何在asp.net mvc中使用jntemplate。

目錄:

  1. .net 開源模板引擎jntemplate 教程:基礎篇之入門
  2. .net 開源模板引擎jntemplate 教程:基礎篇之語法

所有章節寫完後,會再補齊所有目錄

jntemplate源碼下載

相關文章
相關標籤/搜索