# mustachio **Repository Path**: zmsofts/mustachio ## Basic Information - **Project Name**: mustachio - **Description**: 一个轻巧,功能强大,可口的模板引擎,适用于C#和其他基于.net的语言。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-08-14 - **Last Updated**: 2022-03-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 本项目来自github:https://github.com/wildbit/mustachio Mustachio Logo # Mustachio [![Nuget](https://img.shields.io/nuget/v/Mustachio)](https://www.nuget.org/packages/Mustachio/) A Lightweight, powerful, flavorful, templating engine for C# and other .net-based languages. 一个轻巧,功能强大,可口的模板引擎,适用于C#和其他基于.net的语言。 #### What's this for? *Mustachio* allows you to create simple text-based templates that are fast and safe to render. It's the heart of [Postmark Templates](http://blog.postmarkapp.com/post/125849089273/special-delivery-postmark-templates), and we're ecstatic to provide it as Open Source to the .net community. Mustachio允许您创建快速且安全地呈现的基于简单文本的模板。这是邮戳模板的核心,我们很乐意将其作为.net社区的开放源代码提供。 #### How to use Mustachio: ```csharp // Parse the template: var sourceTemplate = "Dear {{name}}, this is definitely a personalized note to you. Very truly yours, {{sender}}" var template = Mustachio.Parser.Parse(sourceTemplate); // Create the values for the template model: dynamic model = new ExpandoObject(); model.name = "John"; model.sender = "Sally"; // Combine the model with the template to get content: var content = template(model); ``` #### Extending Mustachio with Token Expanders: ```csharp // You can add support for Partials via Token Expanders. // Token Expanders can be used to extend Mustachio for many other use cases, such as: Date/Time formatters, Localization, etc., allowing also custom Token Render functions. //您可以通过令牌扩展器添加对Partials的支持。 //令牌扩展器可用于将Mustachio扩展为许多其他用例,例如:日期/时间格式化程序,本地化等,还允许自定义令牌渲染功能。 var sourceTemplate = "Welcome to our website! {{{ @content }}} Yours Truly, John Smith."; var stringData = "This is a partial. You can also add variables here {{ testVar }} or use other expanders. Watch out for infinite loops!"; var tokenExpander = new TokenExpander { RegEx = new Regex("{{{ @content }}}"), // you can also use Mustache syntax: {{> content }} ExpandTokens = (s, baseOptions) => Tokenizer.Tokenize(stringData, baseOptions) // Instead of baseOptions, you can pass a new ParsingOptions object, which has no TokenExpanders to avoid infinite loops. }; var parsingOptions = new ParsingOptions { TokenExpanders = new[] { tokenExpander } }; var template = Mustachio.Parser.Parse(sourceTemplate, parsingOptions); // Create the values for the template model: dynamic model = new ExpandoObject(); model.testVar = "Test"; // Combine the model with the template to get content: var content = template(model); ``` #### Installing Mustachio: Mustachio can be installed via [NuGet](https://www.nuget.org/packages/Mustachio/): ```bash Install-Package Mustachio ``` ##### Key differences between Mustachio and [Mustache](https://mustache.github.io/) Mustachio contains a few modifications to the core Mustache language that are important. 1. `each` blocks are recommended for handling arrays of values. (We have a good reason!) 2. Complex paths are supported, for example `{{ this.is.a.valid.path }}` and `{{ ../this.goes.up.one.level }}` 3. Template partials are supported via Token Expanders. ###### A little more about the differences: One awesome feature of Mustachio is that with a minor alteration in the mustache syntax, we can infer what model will be required to completely fill out a template. By using the `each` keyword when interating over an array, our parser can infer whether an array or object (or scalar) should be expected when the template is used. Normal mustache syntax would prevent us from determining this. We think the model inference feature is compelling, because it allows for error detection, and faster debugging iterations when developing templates, which justifies this minor change to 'vanilla' mustache syntax. Mustachio和Mustache之间的主要区别 Mustachio包含对重要的Mustache语言的一些重要修改。 each建议使用块来处理值数组。(我们有充分的理由!) 支持复杂路径,例如{{ this.is.a.valid.path }}和{{ ../this.goes.up.one.level }} 通过令牌扩展器支持模板部分。 有关差异的更多信息: Mustachio的一项令人敬畏的功能是,在胡子语法上进行了少许改动后,我们就可以推断出完全填写模板所需的模型。通过each在对数组进行插入时使用关键字,我们的解析器可以推断出在使用模板时是否应该期待数组或对象(或标量)。普通的小胡子语法会阻止我们确定这一点。 我们认为模型推断功能非常引人注目,因为它可以进行错误检测,并在开发模板时加快调试迭代的速度,这证明了对“普通”胡须语法的微小更改的合理性。