ASTTemplateParser 2.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package ASTTemplateParser --version 2.0.5
                    
NuGet\Install-Package ASTTemplateParser -Version 2.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ASTTemplateParser" Version="2.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ASTTemplateParser" Version="2.0.5" />
                    
Directory.Packages.props
<PackageReference Include="ASTTemplateParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ASTTemplateParser --version 2.0.5
                    
#r "nuget: ASTTemplateParser, 2.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ASTTemplateParser@2.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ASTTemplateParser&version=2.0.5
                    
Install as a Cake Addin
#tool nuget:?package=ASTTemplateParser&version=2.0.5
                    
Install as a Cake Tool

AST Template Parser

NuGet License .NET

A blazing-fast, security-hardened template engine for .NET with HTML-like syntax and 2000x faster cached rendering.


โšก Performance at a Glance

Scenario Speed Comparison
Cached Render ~0.001ms ๐Ÿ”ฅ 2000x faster
Data-Aware Cache ~0.01ms ๐Ÿš€ 200x faster
Normal Render ~2ms Baseline

โœจ Features

Core

  • ๐Ÿš€ High Performance - 1,000,000+ cached renders/second
  • ๐Ÿ”’ Enterprise Security - XSS protection, loop limits, property blocking
  • ๐Ÿงฉ Component System - <Element>, <Block>, <Data>, <Nav> components
  • ๐Ÿ“ Layout System - Master layouts with sections and slots

Caching (NEW in v2.0.5)

  • โšก Render Caching - Cache rendered output for instant response
  • ๐Ÿ”„ Auto File Invalidation - Cache updates when template file changes
  • ๐Ÿ“Š Data-Aware Caching - Auto-invalidate when variables change
  • โฐ Time-Based Expiration - Optional cache TTL

Template Features

  • ๐ŸŽฏ Indexers - {{ items[0] }}, {{ dict["key"] }}
  • ๐Ÿงช Filters - {{ Name | uppercase }}, {{ Price | currency }}
  • ๐ŸŒ Global Variables - Set once, use everywhere
  • ๐Ÿ” Fragments - <Define> and <Render> for recursion

๐Ÿ“ฆ Installation

# NuGet Package Manager
Install-Package ASTTemplateParser

# .NET CLI
dotnet add package ASTTemplateParser

๐Ÿš€ Quick Start

using ASTTemplateParser;

var engine = new TemplateEngine();
engine.SetPagesDirectory("./pages");

// Set data
engine.SetVariable("User", new { Name = "Alice", Role = "Admin" });

// โšก Cached render - 2000x faster on repeat calls!
string html = engine.RenderCachedFile("dashboard.html", "dashboard", includeDataHash: true);

โšก Caching Guide (NEW!)

Static Pages (Fastest)

// Cache indefinitely until file changes
string about = engine.RenderCachedFile("about.html", "about");
string faq = engine.RenderCachedFile("faq.html", "faq");

User-Specific Pages (Smart Cache)

// Auto-invalidate when variables change
engine.SetVariable("User", currentUser);
engine.SetVariable("Cart", cartItems);

string dashboard = engine.RenderCachedFile(
    "dashboard.html", 
    "dashboard", 
    includeDataHash: true  // โฌ…๏ธ Magic! Data changes = new render
);

Time-Based Expiration

// Cache for 5 minutes
string news = engine.RenderCachedFile(
    "news.html", 
    "news", 
    expiration: TimeSpan.FromMinutes(5)
);

Cache Management

// Invalidate specific cache
TemplateEngine.InvalidateCache("dashboard");

// Invalidate by prefix (e.g., all user caches)
TemplateEngine.InvalidateCacheByPrefix("user-");

// Clear all cache
TemplateEngine.ClearRenderCache();

// Get stats
var stats = TemplateEngine.GetRenderCacheStats();
Console.WriteLine($"Cached pages: {stats["TotalEntries"]}");

๐Ÿ“– Template Syntax

Variables & Indexers

{{Name}}                      
{{User.Address.City}}         
{{Items[0].Title}}            
{{Config["apiKey"]}}          

Filters

{{ Name | uppercase }}                    
{{ Price | currency:"en-US" }}            
{{ Created | date:"dd MMM yyyy" }}        
{{ Description | truncate:100 }}          

Conditionals

<If condition="IsLoggedIn">
    <p>Welcome, {{User.Name}}!</p>
<ElseIf condition="Role == 'guest'">
    <p>Hello, Guest!</p>
<Else>
    <p>Please log in</p>
</If>

Loops

<ForEach var="product" in="Products">
    <div class="card">
        <h3>{{product.Name}}</h3>
        <p>{{product.Price | currency}}</p>
    </div>
</ForEach>

Components


<Element component="button">
    <Param name="text" value="Click Me" />
    <Param name="type" value="primary" />
</Element>


<Block component="hero">
    <Param name="title" value="{{PageTitle}}" />
</Block>

๐Ÿ”’ Security

var security = new SecurityConfig {
    MaxLoopIterations = 500,        // DoS protection
    MaxRecursionDepth = 5,          // Stack protection
    HtmlEncodeOutput = true,        // XSS protection
    BlockedPropertyNames = new HashSet<string> { "Password", "Secret" }
};

var engine = new TemplateEngine(security);

๐Ÿ“Š Performance Benchmarks

Operation Speed Notes
Cache Hit (Static) ~0.001ms 1M+ ops/sec
Cache Hit (Data Hash) ~0.01ms 90K+ ops/sec
Normal Render ~2ms 500 ops/sec
Property Access ~0.00008ms 12M+ ops/sec
File Timestamp Check ~0.001ms Auto-invalidation

Tested on .NET 8.0 / Intel i7 / Windows 11


๐ŸŒ Global Variables

// Set once at startup
TemplateEngine.SetGlobalVariable("SiteName", "My Website");
TemplateEngine.SetGlobalVariable("Year", DateTime.Now.Year);

// Available in ALL templates automatically!
// No need to call SetVariable() every time

๐Ÿ”ง API Reference

Rendering Methods

Method Description
Render() Normal template rendering
RenderFile() Render from file
RenderCached() Cached string template
RenderCachedFile() โญ Recommended - Cached file render

Cache Methods

Method Description
InvalidateCache(key) Remove specific cache
InvalidateCacheByPrefix(prefix) Remove matching caches
ClearRenderCache() Clear all caches
HasCachedRender(key) Check cache exists
RenderCacheCount Get cache count
GetRenderCacheStats() Get detailed stats

๐Ÿ“„ License

MIT License - see LICENSE for details.



Made with โค๏ธ for the .NET community

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.8 47 2/15/2026
2.0.7 85 2/10/2026
2.0.6 97 2/10/2026
2.0.5 81 2/9/2026
2.0.4 83 2/9/2026
2.0.3 87 1/21/2026
2.0.2 83 1/20/2026
2.0.0 94 1/20/2026
1.0.45 97 1/19/2026
1.0.43 87 1/19/2026
1.0.42 82 1/19/2026
1.0.41 85 1/19/2026
1.0.40 87 1/19/2026
1.0.39 95 1/17/2026
1.0.38 81 1/15/2026
1.0.37 89 1/15/2026
1.0.36 92 1/15/2026
1.0.35 86 1/15/2026
1.0.33 87 1/14/2026
1.0.32 80 1/14/2026
Loading failed

v2.0.5 - (February 9, 2026)
- ADDED: High-Performance Render Template Caching (RenderCachedFile, RenderCached).
- ADDED: Data-aware caching with includeDataHash parameter for auto-invalidation.
- ADDED: Automatic file-based cache invalidation via timestamp detection.
- ADDED: Support for searching both Pages and Components directories in RenderCachedFile.
- ADDED: Optional time-based cache expiration (TimeSpan).
- ADDED: Cache management APIs: InvalidateCache, InvalidateCacheByPrefix, ClearRenderCache.
- ADDED: Cache statistics via GetRenderCacheStats() for monitoring.
- PERFORMANCE: 2000x faster repeated renders with cache hit (~0.001ms vs ~2ms).

v2.0.3 - (January 21, 2026)
- ADDED: Template Filters with Pipe syntax (e.g. {{ Name | uppercase }}).
- ADDED: Built-in filters: uppercase, lowercase, date, currency.
- ADDED: Support for Filter Arguments (e.g. {{ Price | currency:"bn-BD" }}).
- ADDED: Custom Filter Registration via TemplateEngine.RegisterFilter().
- IMPROVED: Filter execution performance with cached delegates.

v2.0.2 - (January 20, 2026)
- FIXED: Resolved "Unsafe expression" errors by making SecurityConfig less restrictive by default.
- FIXED: Improved regex for safe characters in expressions (bracket and quote support).
- IMPROVED: Added detailed reason to TemplateSecurityException for easier debugging.
- IMPROVED: Added support for IList and native Array indexing (e.g. item[0]).
- IMPROVED: Exposed Security property on TemplateEngine for instance-level configuration.

v2.0.1 - (January 20, 2026)
- ADDED: High-performance Indexer Support (item[key]).
- ADDED: Compiled delegate caching for indexers for maximum speed.
- ADDED: Support for dynamic key resolution in templates.
- IMPROVED: Expression evaluation robustness.
- SECURITY: Enhanced indexer validation with BlockedPropertyNames.