Scriban templete 模板使用

官网地址:scriban/scriban: A fast, powerful, safe and lightweight scripting language and engine for .NET (github.com)

在线测试地址:Scriban Online Demo - Scriban Online

Builtins

官网内置函数,github地址

string functions
  • string.escape
  • string.append
  • string.capitalize
  • string.capitalizewords
  • string.contains
  • string.empty
  • string.whitespace
  • string.downcase
  • string.ends_with
  • string.handleize
  • string.literal
  • string.lstrip
  • string.pluralize
  • string.prepend
  • string.remove
  • string.remove_first
  • string.remove_last
  • string.replace
  • string.replace_first
  • string.rstrip
  • string.size
  • string.slice
  • string.slice1
  • string.split
  • string.starts_with
  • string.strip
  • string.strip_newlines
  • string.to_int
  • string.to_long
  • string.to_float
  • string.to_double
  • string.truncate
  • string.truncatewords
  • string.upcase
  • string.md5
  • string.sha1
  • string.sha256
  • string.hmac_sha1
  • string.hmac_sha256
  • string.pad_left
  • string.pad_right
  • string.base64_encode
  • string.base64_decode
  • string.index_of

循环

       {{-for option in options}}
             {{-if option.Label == "1" }}
               {{option.label}}
             {{ else if option.Label == "2" }}
               {{option.value}}
             {{ else }}
               {{option.value}}
             {{-end}}
        {{-end}}

调用多次方法

// 字符串转小写,在判断是否是"this"开头
{{ "This is easy" | string.downcase | string.starts_with "this" }}
// 返回true

{{ "This is easy" | string.downcase | string.starts_with "This" }}
// 返回false

输出模板字符串 {%{ }%}

// 输出双大括号 {{  }}
{%{{{data}}}%}

绑定.NET后台StringUtil辅助类

       public static string ToString(object value)
       {
            if (value == null)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToString(value);
            }
       }
        
            var scriptObject = new ScriptObject();
            scriptObject.Import(typeof(StringUtil));
            

例如:模板绑定StringUtil类中的ToString方法使用

如果后台有多个同名ToString函数,根据测试,模板会调用最后一个,目前还没弄清楚,调用指定参数的函数

            string str="123";
            //"This is Hello:  {{to_string value:str}}";
            // 或 "This is Hello: {{to_string str}}";
            
            多个参数用空格隔开,例如:
            {{to_string str1 str2}}";

特殊情况

json数据:"options": [
            {
                "value": "Option 1"
            },
            {
                "value": "Option 2"
            },
            {
                "value": "Option 3"
            }
        ]

在模板中直接输出绑定就变了

成了这样:
'[{key: "value", value: "Option 1"},{key: "value", value: "Option 2"},{key: "value", value: "Option 3"}]' 
因此我是在模板中做循环处理
  {{-for option in options}}
       value="{{option.value}}"
  {{-end}}
如果想转成原字符串输出,目前没找到方法,只能在后端已字符串的类型绑定,或者模板循环拼接
建议后台转换一下SerializeUtil.JsonSerialize(options);

posted @   亘古不变  阅读(9)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示