t_template_engine

A lightweight, powerful, and flexible custom template engine for Dart and Flutter. It supports logic-based templates with features like loops, conditionals, partials, and filter pipelines.


✨ Features

  • Variable Interpolation: Support for nested objects using dot notation {{ user.profile.name }}.
  • Control Flow: Simple #if/else conditions and #for/each loops.
  • Filter Pipelines: Chain multiple filters like {{ name | trim | upper }}.
  • Partials & Layouts: Reuse templates with the {{ include 'filename' }} directive.
  • Security: Automatic HTML escaping to prevent XSS. Use {{{ raw }}} for trusted content.
  • Clean Output: Automatic removal of line comments and unnecessary whitespace.

🚀 Installation

Add this to your pubspec.yaml:

dependencies:
  t_template_engine:
    git:
      url: [https://github.com/ThanCoder/t_template_engine.git](https://github.com/ThanCoder/t_template_engine.git)


📖 Usage

1. Basic Setup

import 'package:t_template_engine/t_template_engine.dart';

void main() {
  final engine = TTemplateEngine();

  final template = """
    <h1>Hello, {{ name | capitalize }}!</h1>
    {{#if isAdmin}}
      <p>Welcome to the admin panel.</p>
    {{else}}
      <p>Welcome back, User.</p>
    {{/if}}
  """;

  final data = {
    'name': 'than coder',
    'isAdmin': true,
  };

  print(engine.render(template, data));
}

2. Nested Property Access

Use dot notation to access data inside nested maps.

<p>User City: {{ user.address.city }}</p>

3. Loops (for/each)

<ul>
  {{#for item in items}}
    <li>{{ item }}</li>
  {{/for}}
</ul>

4. Partials (Includes)

You can register partials manually or load them from files.

engine.setPartial('header', '<nav>Navigation Bar</nav>');

await engine.setPartialFile(name, filePath) //file

final source = "{{ include 'header' }} <div>Main Page</div>";
print(engine.render(source, {}));

5. HTML Escaping (Security)

{{ value }}: Escapes HTML (Recommended for user input).

{{{ value }}}: Renders raw HTML (Use for trusted content only).

Libraries

t_template_engine