.NET模板引擎 SharpTAL
openkk
13年前
SharpTAL 将基于 XML 的模板编译成 .NET 的编译代码,包含对 Zope Page Templates (ZPT) 语言的完整实现。ZPT 是一个可生成 HTML、XML和纯文本的系统。ZPT 的前身是 TAL (Template Attribute Language), the Expression Syntax (TALES), and the Macro Expansion Template Attribute Language (METAL). <h3>功能特性:</h3> <ul> <li><b>Fast</b> - Templates are compiled to .NET assemblies at runtime and cached either in memory or filesystem.</li> <li><b>Multiplatform</b> - Works on .NET Framework under Microsoft Windows and on Mono under Linux.</li> <li><b>Tested</b> </li> <li><b>C# language in expressions</b></li> <li><b>Direct access to data objects</b> - Don't need an intermediate layer of XML. This is a major advantage compared to XSLT.</li> <li><b>Collaboration between programmers and designers</b> - This is achieved by embedding TAL statements inside valid XML tags which can then be worked on using common design tools. This is a major advantage compared to Text Template Transformation Toolkit (T4).</li> </ul> <p></p> <pre class="brush:c#; toolbar: true; auto-links: false;">using System; using System.IO; using System.Reflection; using System.Collections.Generic; using SharpTAL; namespace Demo { class Demo { static void Main(string[] args) { // Set path to the existing cache folder string cacheFolder = Path.Combine(Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location), "Template Cache"); // Create the template cache. // We want to clear the cache folder on startup, setting the clearCache parameter to true, // and using customized file name pattern. FileSystemTemplateCache cache = new FileSystemTemplateCache(cacheFolder, true, @"Demo_{key}.dll"); // The body of the template string templateBody = @"<html><h1 tal:content=""title"">The title goes here</h1></html>"; // Global variables used in template Dictionary<string, object> globals = new Dictionary<string, object>(); globals["title"] = "Hello World !"; // Finally, render the template. In this moment the assembly will be generated and cached string slowResult = cache.RenderTemplate(templateBody, globals); // The "slowResult" will contain: <html><h1>Hello World !</h1></html> // Set the title to another value globals["title"] = "Hi !"; // A second call to RenderRemplate() will use cached assembly string fastResult = cache.RenderTemplate(templateBody, globals); // The "fastResult" will contain: <html><h1>Hi !</h1></html> } } }</pre> <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1322988628483" target="_blank">http://www.open-open.com/lib/view/home/1322988628483</a></p> <p></p>