.NET模板引擎 SharpTAL

openkk 14年前發布 | 32K 次閱讀 .NET 模板引擎

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).

功能特性:

  • Fast - Templates are compiled to .NET assemblies at runtime and cached either in memory or filesystem.
  • Multiplatform - Works on .NET Framework under Microsoft Windows and on Mono under Linux.
  • Tested
  • C# language in expressions
  • Direct access to data objects - Don't need an intermediate layer of XML. This is a major advantage compared to XSLT.
  • Collaboration between programmers and designers - 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).

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>

項目主頁:http://www.baiduhome.net/lib/view/home/1322988628483

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!