2009-10-20 33 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