Flutter模板生成插件templater_atreeon的使用

Flutter模板生成插件templater_atreeon的使用

templater_atreeon 是一个文本模板转换工具,可以用于源代码生成。它通过替换文件中的标记来实现这一功能,并支持简单的标记替换、子模板、子子模板和迭代模板。

使用规则

  • <code>%%%keyName%%%</code> 类型必须是 String
  • <code>###keyName|subTemplateName###</code> 类型必须是 Map<String, dynamic>
  • <code>~~~keyName|repeatableTemplateName~~~</code> 类型必须是 List<Map<String, dynamic>>
var t = templateAtreeon(starterTemplate, [templateResources]);
var replaces = {"tableName":"employees", "columns":[{"name":"id", "dbType":"int64", "dartType":"int"}]};
var output = templateAtreeon.replace();

注释必须在模板文件的开头,且不包含空行,以两个连字符(--)开始。

配置模板

有两种配置模板的方法:

  1. 将模板作为 String 传递给构造函数的 templateMain 参数,并将其他模板名称列表传递给 templatesOther 构造参数。
  2. 设置模板目录,并加载模板列表(文件名减去 .txt 扩展名)。主模板应命名为 main.txt

示例用法

简单的标记替换

var template = """
this is my test %%%value1%%% to see
if these tokens %%%value2%%% are
replaced %%%value3%%%
""";

var input = {
  "value1": "hello",
  "value2": "howdy",
  "value3": "gday",
};

var templater = Templater(input, templateMain: template);
var result = await templater.replace();

支持子模板

var template = """
this is my test to see
if a sub template of a pet ###pet|templatePet### and
another sub template of a person
###person|templatePerson###
has been replaced
so %%%value3%%%
""";

var otherTemplates = {
  "templatePet": "Here is my pet %%%name%%% and it is a %%%type%%%",
  "templatePerson": "Hello %%%name%%% you have %%%eyeColour%%% eyes",
};

var input = {
  "value3": "gday",
  "pet": {
    "name": "Sandy",
    "type": "cat",
  },
  "person": {
    "name": "Mel",
    "eyeColour": "green",
  },
};

var templater = Templater(
  input,
  templateMain: template,
  templatesOther: otherTemplates,
);
var result = await templater.replace();

子子模板

var template = """
this is my test to see
owner
###person|templatePerson###
so %%%value3%%%
""";

var otherTemplates = {
  "templatePet": "Here is my pet %%%name%%% and it is a %%%type%%%",
  "templatePerson": """Hello %%%name%%% you have %%%eyeColour%%% eyes
has a pet of
###pet|templatePet###
and thats all from %%%name%%%""",
};

var input = {
  "value3": "gday",
  "person": {
    "name": "Mel",
    "eyeColour": "green",
    "pet": {
      "name": "Sandy",
      "type": "cat",
    },
  },
};

可重复使用的模板

var template = """
Yo! %%%value1%%% to you all
Here are my favourite pet types
~~~pets|petTemplate~~~
so %%%value2%%%
""";

var otherTemplates = {
  "petTemplate": """I had %%%name%%% and it was a %%%type%%%
""";

var input = {
  "value1": "howdy",
  "value2": "gday",
  "pets": [
    {
      "name": "Sandy",
      "type": "cat",
    },
    {
      "name": "Simon",
      "type": "fish",
    },
    {
      "name": "Tommy",
      "type": "tortoise",
    },
  ]
};

var expected = """
Yo! howdy to you all
Here are my favourite pet types
I had Sandy and it was a cat
I had Simon and it was a fish
I had Tommy and it was a tortoise

so gday
""";

更多关于Flutter模板生成插件templater_atreeon的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter模板生成插件templater_atreeon的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


templater_atreeon 是一个用于生成 Flutter 项目模板的插件,它可以帮助开发者快速生成项目结构、页面、组件等代码,从而提高开发效率。以下是使用 templater_atreeon 的基本步骤和示例。

1. 安装 templater_atreeon

首先,你需要在你的 Flutter 项目中安装 templater_atreeon 插件。你可以通过 pubspec.yaml 文件来添加依赖:

dependencies:
  templater_atreeon: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 创建模板文件

templater_atreeon 使用模板文件来生成代码。你可以在项目中创建一个 templates 目录,并在其中定义你的模板文件。

例如,创建一个 page_template.dart 文件:

import 'package:flutter/material.dart';

class {{pageName}}Page extends StatelessWidget {
  const {{pageName}}Page({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('{{pageName}} Page'),
      ),
      body: Center(
        child: Text('This is the {{pageName}} page.'),
      ),
    );
  }
}

在这个模板中,{{pageName}} 是一个占位符,它将在生成代码时被替换为你指定的值。

3. 使用 templater_atreeon 生成代码

你可以通过命令行或 Dart 脚本来使用 templater_atreeon 生成代码。

通过命令行生成代码

假设你已经安装了 templater_atreeon 的命令行工具,你可以运行以下命令来生成代码:

flutter pub run templater_atreeon:generate \
  --template ./templates/page_template.dart \
  --output ./lib/pages/home_page.dart \
  --data '{"pageName": "Home"}'

这个命令会将 page_template.dart 模板文件中的 {{pageName}} 替换为 Home,并将生成的代码保存到 lib/pages/home_page.dart 文件中。

通过 Dart 脚本生成代码

你也可以在 Dart 脚本中使用 templater_atreeon 来生成代码。以下是一个示例脚本:

import 'package:templater_atreeon/templater_atreeon.dart';

void main() {
  final template = Template.fromFile('./templates/page_template.dart');
  final data = {'pageName': 'Home'};
  final output = template.apply(data);

  File('./lib/pages/home_page.dart').writeAsStringSync(output);
}
回到顶部