StringTemplate.Net 學習筆記(3):運算式元素文法(上)

來源:互聯網
上載者:User

1.簡單屬性

對於簡單屬性(區別於集合類型),都是直接調用它的ToString()方法

    StringTemplate query = new StringTemplate("$title$");
    query.SetAttribute("title", "StringTemplate學習");
    Console.WriteLine(query.ToString());輸出:StringTemplate學習再看一個例子:

    StringTemplate query = new StringTemplate("$title$");
    query.SetAttribute("title", typeof(object));
    Console.WriteLine(query.ToString());輸出:System.Object

2.集合的索引

    string[] strArray = new string[] { "a","b","c","d","e" };
    StringTemplate query = new StringTemplate("$array:{/n當前索引 從1開始計算:$i$,從0開始計算:$i0$ }$");
    query.SetAttribute("array", strArray);
    Console.WriteLine(query.ToString());輸出:

    當前索引 從1開始計算:1,從0開始計算:0
    當前索引 從1開始計算:2,從0開始計算:1
    當前索引 從1開始計算:3,從0開始計算:2
    當前索引 從1開始計算:4,從0開始計算:3
    當前索引 從1開始計算:5,從0開始計算:4集合不限於Array,可以是實現IList,ICollection,IDictionary等介面的類型。

3.屬性引用

如果attribute是一個集合 或 自訂類型,則可以通過attribute.property訪問它的屬性,自訂類型

    class User {
        public string Name {get;set;}
        public int Age {get;set;}
    }
    public static void Main(string[] args)
    {
        User u = new User { Name = "囧月", Age = 1 };
        StringTemplate query = new StringTemplate("姓名:$user.Name$,年齡:$user.Age$");
        query.SetAttribute("user", u);
        Console.WriteLine(query.ToString());
    }    輸出:姓名:囧月,年齡:1集合類型,可以是Dictionary,HashTable等有Key/Value的對象,如:

    Dictionary u = new Dictionary(1);
    u.Add("Name", "囧月");
    u.Add("Age", "1");
    StringTemplate query = new StringTemplate("姓名:$user.Name$,年齡:$user.Age$");
    query.SetAttribute("user", u);
    Console.WriteLine(query.ToString());還有一種情況,應該屬性ST的自訂集合,沒有詳細研究:

    StringTemplate st = new StringTemplate("$user:{姓名:$it.Name$,年齡:$it.Age$}$");
    st.SetAttribute("user.{Name,Age}","囧月", 1);
    Console.WriteLine(st.ToString());

當有保留字時:

    Dictionary items = new Dictionary();
    items.Add("first", "第一");
    items.Add("last", "最後");
    items.Add("1", "數字1");
    StringTemplate st = new StringTemplate("$items.(/"first/")$,$items.(/"last/")$, $items.(/"1/")$");
    st.SetAttribute("items", items);
    Console.WriteLine(st.ToString());    輸出:第一,最後,數字1其中first、last為保留字,數字1不能直接作為屬性名稱來引用,與保留字同樣處理

下面再看一個錯誤的例子,當key為int類型時,數字作為保留文書處理不能得到預期的結果:

Dictionary dict = new Dictionary();
            dict.Add(1,11);
            dict.Add(2,22);
            dict.Add(3,33);
            StringTemplate st1 = new StringTemplate("$u.(/"1/")$");
            st1.SetAttribute("u", dict);
            Console.WriteLine(st1.ToString());這個例子輸出為空白,應該只能使用集合的方式來得到值

4.多個屬性/集合

    string[] strArray = new string[] { "a","b","c","d","e" };
    StringTemplate query = new StringTemplate("$array$");
    query.SetAttribute("array", strArray);
    Console.WriteLine(query.ToString());    輸出:abcde它等效於:

    string[] strArray = new string[] { "a","b","c"};
    StringTemplate query = new StringTemplate("$array$");
    query.SetAttribute("array", strArray);
    query.SetAttribute("array", "d");
    query.SetAttribute("array", "e");
    Console.WriteLine(query.ToString());最終都會把array合并成一個集合

    Type[] typeArray = new Type[]{typeof(object),typeof(string),typeof(int),typeof(IList)};
    StringTemplate query = new StringTemplate("$array$");
    query.SetAttribute("array", typeArray);
    Console.WriteLine(query.ToString());    輸出:System.ObjectSystem.StringSystem.Int32System.Collections.IList通過以上例子可以看出,ST對集合類型,都是先調用每個元素的ToString()方法,再把它們串連起來。

而且預設情況下,是沒有分隔字元的。接下來看一下分隔字元的使用,分隔字元兩邊必須加上雙引號:

    string[] strArray = new string[] { "a","b","c","d","e" };
    StringTemplate query = new StringTemplate("$array;separator=/"--/"$");
    query.SetAttribute("array", strArray);
    Console.WriteLine(query.ToString());    結果:a--b--c--d--e當集合中有元素為null的時候,這個元素將不會被輸出,如果需要輸出,則需要指定null條件:

    string[] strArray = new string[] { "a",null,"c",null,"e" };
    StringTemplate query = new StringTemplate("$array;null=/"k/",separator=/"--/"$");
    query.SetAttribute("array", strArray);
    Console.WriteLine(query.ToString());    結果:a--k--c--k--e  (如果未指定null則為:a--c--e)

5、集合合并

對於多個attribute參數,使用方括弧把它們合并成單個集合:

    string[] array1 = new string[] { "a","b","c" };
    string[] array2 = new string[] { "d","e" };
    StringTemplate query = new StringTemplate("$[list1,list2]:{$it$};separator=/",/"$");
    query.SetAttribute("list1", array1);
    query.SetAttribute("list2", array2);
    Console.WriteLine(query.ToString());    輸出結果:a-b-c-d-e可以看到array1和array2被合并成1個集合,如果模板定義改為$[list1,list2]:{x,y|$x$,$y$};separator=","$則會出現錯誤。

再看一個例子,不同元素長度:

StringTemplate st1 = new StringTemplate("$[a,b,c]:{$it$};separator=/",/"$");
            st1.SetAttribute("a", "aa");
            st1.SetAttribute("b", new string[]{"b","c","d"});
            st1.SetAttribute("c", typeof(object));
            Console.WriteLine(st1.ToString());    輸出結果:aa,b,c,d,System.Object再看一個例子,不同類型的合并:

StringTemplate st1 = new StringTemplate("$[a,b,c]:{$if(it.Name)$ $it.Name$ $else$ $it$ $endif$};separator=/",/"$");
            st1.SetAttribute("a", new User{Name="aa"});
            st1.SetAttribute("b", new User[]{new User{Name="b"},new User{Name="c"},new User{Name="d"}});
            st1.SetAttribute("c", typeof(object));
            Console.WriteLine(st1.ToString());    輸出結果: aa , b , c , d , Object可以看出支援不同類型合并到同一個集合

另外,雖然與$list1,list2:{ x,y | ...}$只差了一對方括弧[],但絕對不是一樣的。

看起來內容有點多,還是分成2部分吧,省得等下格式亂掉了。

參考:http://www.antlr.org/wiki/display/ST/Expressions

原文來自:雨楓技術教程網 http://www.fengfly.com
原文網址:http://www.fengfly.com/plus/view-175119-1.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.