搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目