搜尋套件、儲存庫與 APP 分類

套件詳情v0.7.2

jinja

Dart 的 Jinja2 模板引擎。變數、表達式、控制結構和模板繼承。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
4.9萬
喜歡數
48
Pub 分數
160

安裝套件

flutter pub add jinja

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Jinja 是 Jinja2 模板引擎的 Dart 實現,支援變數、運算式、控制結構與模板繼承,可實現動態內容產生。它支援伺服器端渲染,並能與 Web 框架良好整合。專為後端服務與 Web 應用設計,透過強大的模板功能協助開發者將邏輯與呈現分離。此套件可在所有主要平台運行,包含行動裝置、Web 與桌面端。

適用情境

  • 伺服器端渲染
  • 動態HTML生成
  • 基於模板的內容創作
  • Web 應用程式佈局
  • 基於API的範本
  • 多平台模板

主要特色

  • 變數與表達式支援
  • 控制結構(if、for)
  • 模板繼承
  • 跨平台相容性
  • 輕量且快速
  • 與 Dart 應用的輕鬆整合

適合對象

  • 後端開發人員
  • 全端Web開發人員
  • 範本驅動的應用程式
  • 伺服器端渲染的 Web 應用
  • 基於 Dart 的 Web 框架

使用建議

  • 不適用於客戶端渲染
  • 有限的文件
  • 需要了解 Jinja2 語法
  • 最適合伺服器環境
  • 無內建的安全過濾器
  • 可能需要對用戶輸入進行額外驗證

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

jinja

Pub Package Test Status CodeCov

Jinja (3.x) server-side template engine port for Dart 3. Variables, expressions, control structures and template inheritance.

Documentation

It is mostly similar to Jinja templates documentation, differences provided below.

work in progress.

Differences with Python version

  • Auto-escaping and related statements, filters and tests have been removed due to the impossibility of extending String. Use the escape filter manually or escape values before passing them to the template.
  • The default filter compares values with null.
  • The defined and undefined tests compare values with null.
  • The map filter also compares values with null. Use attribute and item filters for object.attribute and object[item] expressions.
  • If Environment({getAttribute}) is not passed, the getItem method will be used. This allows you to use {{ map.key }} as an expression equivalent to {{ map['key'] }}.
  • String slices and negative indexes are not supported (list slicing [start:stop] with non-negative integers is supported).
  • Macro arguments without default values are required.
  • No FilterArgumentError error class.
  • No *args and **kwargs arguments.
  • No Template module.
  • work in progress

Dynamically invoked members (can increase the size of the JS output)

  • [], +, -, *, /, ~/, % operators
  • object.length getter
  • object.call getter
  • Function.apply(function, ...)

Example

import 'package:jinja/jinja.dart';

// ...

var environment = Environment(blockStart: '...', blockEnd: '...');
var template = environment.fromString('...source...');
print(template.render({'key': value}));
// or write directly to StringSink (IOSink, HttpResponse, ...)
template.renderTo(stringSink, {'key': value});

See also examples with conduit and reflectable.

Status:

TODO:
  • Informative error messages
    • Template name 🔥
    • Source span 🔥
  • Template class:
    • generate method
    • stream method
  • Relative template paths
  • Async Support
  • Template Inheritance
    • Super Blocks
      • {{ super.super() }}
  • List of Control Structures
    • Extends
      • Execute non-block statements and expressions
        {% extends 'base.html' %}
        {% set title = 'Index' %}
        {% macro header() %}
          <h1>{{ title }}</h1>
        {% endmacro %}      ```
        {% block body %}
          {{ header() }}
        {% endblock %}
        
  • Expressions
    • Dart Methods and Properties
      • !./?.
  • Loaders
    • PackageLoader (VM)
    • ...
  • List of Global Functions
    • lipsum
    • cycler
    • joiner
  • Extensions
    • i18n
    • Loop Controls
    • Debug Statement
  • Template compiler (builder)
  • ...
Done:

Note: item - not supported

  • Variables
  • Filters
    • forceescape
    • safe
    • unsafe
  • Tests
    • escaped
  • Comments
  • Whitespace Control
  • Escaping (only escape filter)
  • Line Statements
    • Comments
    • Blocks
  • Template Inheritance
    • Base Template
    • Child Template
    • Super Blocks
    • Nesting extends
    • Named Block End-Tags
    • Block Nesting and Scope
    • Required Blocks
    • Template Objects
  • HTML Escaping
  • List of Control Structures
    • For
    • If
    • Macro
    • Call
    • Filters
    • Assignments
    • Block Assignments
    • Extends
    • Blocks
    • Include
    • Import
    • Try / Catch
  • Import Context Behavior
  • Expressions with filters and tests
    • Literals
      • "Hello World"
      • 42 / 123_456
      • 42.23 / 42.1e2 / 123_456.789
      • ['list', 'of', 'objects']
      • ('tuple', 'of', 'values')
      • {'dict': 'of', 'key': 'and', 'value': 'pairs'}
      • true / false
      • null
    • Math
      • +
      • -
      • /
      • //
      • %
      • *
      • **
    • Comparisons
      • ==
      • !=
      • >
      • >=
      • <
      • <=
    • Logic
      • and
      • or
      • ?? (null-coalescing / fallback)
      • not
      • (expr)
    • Other Operators
      • in
      • is
      • |
      • ~
      • ()
      • ./[]
      • [:] (list slice)
    • Conditional Expressions
      • {{ list.last if list }}
      • {{ user.name if user else 'Guest' }}
      • {{ condition ? 'yes' : 'no' }} (ternary operator)
    • Dart Methods and Properties (if reflection is on)
      • {{ string.toUpperCase() }}
      • {{ list.add(item) }}
  • List of Global Functions
    • dict
    • print
    • range
    • list
    • namespace
  • Loaders
    • FileSystemLoader
    • MapLoader (equivalent to Jinja's DictLoader)
  • Extensions
    • Expression Statement (do)
    • With Statement
  • Autoescape Overrides

Contributing

Contributions are welcome! Please open an issue or pull request on GitHub. Look at the ToDo list and comments in the code for ideas on what to work on. There are no strict rules, but please try to follow the existing code style.

As non-native English speaker and learner, I will be grateful for any corrections in the documentation and code comments.

Support

Post issues and feature requests on the GitHub issue tracker.

所屬分類

依賴

依賴清單 5 項

原始碼儲存庫

使用此套件的專案