2009-10-20 71 views
15

我的客戶端應用程序需要生成HTML。我想使用像Spark這樣的模板/視圖引擎解決方案,但我不確定Spark是否可以在ASP.NET應用程序之外使用。有沒有人知道有關以這種方式使用Spark的任何示例或文檔?在獨立應用程序中使用Spark View引擎

(如果你知道可以單獨使用其他視圖引擎解決方案,我很好奇地聽到這些,太。)

回答

14

除了其它實例中,我發現了一個簡單的一個在Spark源本身。 Xpark項目是一個使用Spark轉換XML的命令行應用程序。 Spark的創造者Louis DeJardin在他的博客上描述how Xpark works

相關的代碼片段:

// Create an engine using the templates path as the root location 
    // as well as the shared location 
    var engine = new SparkViewEngine 
     { 
      DefaultPageBaseType = typeof(SparkView).FullName, 
      ViewFolder = viewFolder.Append(new SubViewFolder(viewFolder, "Shared")) 
     }; 

    SparkView view; 

    // compile and instantiate the template 
    view = (SparkView)engine.CreateInstance(
          new SparkViewDescriptor() 
           .AddTemplate(templateName)); 

    // render the view to stdout 
    using (var writer = new StreamWriter(Console.OpenStandardOutput(), Encoding.UTF8)) 
    { 
     view.RenderView(writer); 
    } 

這是足以讓我在正確的方向。但我一定會深入其他例子。

1

肯定。可能最完整的示例是查看ASP.NET MVC本身的Spark視圖引擎代碼。

它也在測試中,所以閱讀測試應該給你一個非常好的起點。

1

如果它對別人有幫助,我需要在MVC項目之外使用Spark引擎完成類似的事情。
我創建了一個使用Spark視圖引擎執行簡單模板操作的示例(非常簡化)C#項目。也許有人會用它作爲我的大部分模板代碼是基於關閉審查,他用來模板星火引擎以類似的方式Jonas Gauffin's C# WebServer項目的起點/聳肩
http://jezel.googlecode.com/files/SparkTemplateExample.zip