跳到主要內容

發表文章

目前顯示的是有「Mustache」標籤的文章

Mustache - Show List

Test Objects 以下為測試的資料物件,可以從中取得一個空List與有兩筆資料的List: public class TestBean { public List < TestSubBean > getEmptyList ( ) { return new ArrayList <> ( ) ; }   public List < TestSubBean > getTestList ( ) { List < TestSubBean > list = new ArrayList <> ( ) ; list. add ( new TestSubBean ( "testSubBean1" ) ) ; list. add ( new TestSubBean ( "testSubBean2" ) ) ; return list ; } } public class TestSubBean { private String name ; public TestSubBean ( String name ) { this . name = name ; }   public String getName ( ) { return name ; } } Template 樣本的部分,有資料顯示使用#,無資料顯示使用^: list: {{#testList}} name= {{name}} {{/testList}} emptylist: {{^emptyList}}empty list{{/emptyList}} Output list: name= testSubBean1 name= testSubBean2 emptylist: empty list

Mustache - Hello World

Introduction Mustache是Albert所找的template system engine。目的是為了讓你可以簡單的只使用資料物件(bean),用少少甚至無邏輯去定義輸出格式。如果想切換輸出格式,只要修改樣本檔案就好。 How to? Create Engine 使用Mustache的第一步就是要建立它的Engine instance,而方法就是透過MustacheEngineBuilder。以我的例子來說,我告訴了builder樣本的放置位置,第一優先從classpath中的templates資料夾中找尋txt結尾之檔案,第二優先為工作目錄的templates中找尋txt結尾之檔案: TemplateLocator locator1 = new ClassPathTemplateLocator ( 1 , "templates" , "txt" ) ; TemplateLocator locator2 = new FileSystemTemplateLocator ( 2 , Paths. get ( "templates" ) . toString ( ) , "txt" ) ;   MustacheEngine mustacheEngine = MustacheEngineBuilder . newBuilder ( ) . addTemplateLocator ( locator1 ) . addTemplateLocator ( locator2 ) . build ( ) ; 第一個Template 以下為我的測試資料物件: public class TestBean {   public String getName ( ) { return "testName" ; }   public String getPasswd ( ) { return "testPasswd" ; }   public boolean showPasswd ( ) { return false ; } } 以下為我的執行轉換程式...