用 C# 玩转 Scriban:自动生成报告、代码、文本,效率提升 10 倍

在现代软件开发中,动态生成文本、报告甚至代码已经成为常态。Scriban 是一个功能强大的 .NET 模板引擎,它语法简洁、灵活,能帮你极大提高工作效率。本文将从基础到高级,结合 C# 展示 Scriban 的使用方法,让你轻松实现自动化生成。


1. 安装与引入 Scriban

首先,使用 NuGet 安装:

复制代码
Install-Package Scriban

在 C# 中引用:

复制代码
using Scriban;
using Scriban.Runtime;

2. 基础模板语法

Scriban 支持:

  • 变量输出

    {{ name }} // 输出变量
    {{ score + 10 }} // 输出表达式结果

  • 注释

    {# 这是注释 #}

  • 条件语句

    {% if score >= 60 %}
    Pass
    {% else %}
    Fail
    {% end %}

  • 循环语句

    {% for student in students %}
    {{ student.Name }} - {{ student.Score }}
    {% end %}


3. 在 C# 中渲染模板

复制代码
var templateText = @"
Hello {{ name }}!

{% if score >= 60 %}
Congratulations! You passed with {{ score }} points.
{% else %}
Sorry, you failed with {{ score }} points.
{% end %}

Your grade: {{ grade }}
";

var template = Template.Parse(templateText);

var model = new
{
    name = "Alice",
    score = 78,
    grade = "B+"
};

var result = template.Render(model);
Console.WriteLine(result);

输出:

复制代码
Hello Alice!

Congratulations! You passed with 78 points.

Your grade: B+

4. 绑定集合数据与循环

复制代码
var students = new[]
{
    new { Name = "Alice", Score = 78 },
    new { Name = "Bob", Score = 55 },
    new { Name = "Charlie", Score = 92 }
};

var templateText = @"
Student Scores:
{% for student in students %}
  - {{ student.Name }}: {{ student.Score }} points
{% end %}
";

var template = Template.Parse(templateText);
var model = new { students };
var result = template.Render(model);
Console.WriteLine(result);

输出:

复制代码
Student Scores:
  - Alice: 78 points
  - Bob: 55 points
  - Charlie: 92 points

5. 注入自定义函数

你可以在模板中调用 C# 函数,实现复杂逻辑:

复制代码
var templateText = @"
Hello {{ name }}!
Your next score prediction: {{ predict_next_score(score) }}
";

var template = Template.Parse(templateText);

var model = new ScriptObject();
model["name"] = "Alice";
model["score"] = 78;

// 注入 C# 函数
model.Import("predict_next_score", new Func<int,int>(score => score + 5));

var context = new TemplateContext();
context.PushGlobal(model);

var result = template.Render(context);
Console.WriteLine(result);

输出:

复制代码
Hello Alice!
Your next score prediction: 83

6. 模板拆分与复用

大型项目建议拆分模板文件:

header.scriban

复制代码
<h1>Student Report</h1>

body.scriban

复制代码
{% include 'header.scriban' %}
<ul>
{% for student in students %}
  <li>{{ student.Name }} - {{ student.Score }}</li>
{% end %}
</ul>

C# 渲染:

复制代码
var template = Template.Parse(System.IO.File.ReadAllText("body.scriban"));
var students = new[]
{
    new { Name = "Alice", Score = 78 },
    new { Name = "Bob", Score = 55 }
};
var model = new { students };
var result = template.Render(model);
Console.WriteLine(result);

7. 高级示例:循环 + 条件 + 函数

复制代码
var templateText = @"
{% for student in students %}
  {{ student.Name }} - {{ student.Score }} points
  {% if student.Score >= 60 %}
    Pass, next target: {{ next_target(student.Score) }}
  {% else %}
    Fail, try again!
  {% end %}
{% end %}
";

var template = Template.Parse(templateText);

var model = new ScriptObject();
var students = new[]
{
    new { Name = "Alice", Score = 78 },
    new { Name = "Bob", Score = 55 }
};
model["students"] = students;

// 注入 C# 函数
model.Import("next_target", new Func<int,int>(score => score + 10));

var context = new TemplateContext();
context.PushGlobal(model);
var result = template.Render(context);
Console.WriteLine(result);

输出:

复制代码
Alice - 78 points
Pass, next target: 88
Bob - 55 points
Fail, try again!

8. 总结

使用 C# + Scriban,你可以:

  • 自动生成报告、文档、代码模板

  • 灵活绑定数据和自定义函数

  • 轻松管理复杂模板结构

  • 大幅提升开发效率(效率提升 10 倍不是夸张)

Scriban 简单易学,功能强大,是每个 C# 开发者提高自动化能力的必备利器。

相关推荐
不会就选b1 天前
MySQL之视图
数据库·mysql
AAA大运重卡何师傅(专跑国道)1 天前
【无标题】
开发语言·c#
>no problem<1 天前
基于cola5.0的基础设施层的多数据库切换方案思路
数据库·spring boot·mybatisplus·cola5.0·数据库迁移适配
OceanBase数据库官方博客1 天前
OceanBase 赋能央国企:从发电到用电的全链路业务承载
数据库·oceanbase
瀚高PG实验室1 天前
pgsql-ogr-fdw
数据库·postgresql·瀚高数据库·highgo
IvorySQL1 天前
PostgreSQL 技术日报 (6月5日)|PG19 Beta1 上线,PGConf.PL 2026开启征稿
数据库·postgresql·区块链
abcy0712131 天前
pycharm python sqlalchemy mysql增删改查实例csdn
数据库·oracle
无风听海1 天前
IndexedDB 深度指南 浏览器中的事务型对象数据库
前端·数据库
咋吃都不胖lyh1 天前
langgraph基础示例
数据库
网管NO.11 天前
子查询进阶|EXISTS/IN/ANY/ALL,优化查询效率
数据库·sql