用 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# 开发者提高自动化能力的必备利器。

相关推荐
消失的旧时光-19433 分钟前
SQL 第四篇:JOIN 实战(数据库到底是怎么“拼表”的)
数据库·sql·mysql
深蓝轨迹36 分钟前
Spring Data JPA 实战指南:从基础配置到高级技巧
数据库·oracle·spring data jpa
rockey62739 分钟前
AScript中一个很有意思的语法
c#·.net·script·eval·expression·动态脚本
刚子编程41 分钟前
C# Join 深度解析:参数顺序、多表关联与空值处理最佳实践
开发语言·c#·最佳实践·join·多表关联·空值处理
爱喝水的鱼丶1 小时前
SAP-ABAP:SAP 与 ABAP 关联逻辑与入门路径:业务×开发的协作指南
服务器·前端·数据库·学习·sap·abap
天天代码码天天1 小时前
C# OnnxRuntime 实现车牌检测识别
c#·车牌识别·号牌识别
刚子编程1 小时前
C# Join 进阶:GroupJoin、性能对决与自定义比较器
java·servlet·c#·join
MandalaO_O1 小时前
SQL 注入
数据库·oracle
eggrall1 小时前
MySQL表的操作
数据库·mysql
wearegogog1231 小时前
MATLAB椭圆参数检测算法实现
数据库·算法·matlab