RazorEngine

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了RazorEngine脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

模版引擎RazorEngine简介

 

ASP.NET MVC的Razor想必大家都比较熟悉,这里介绍一个独立于ASP.NET的RazorEngine。

RazorEngine是一个开的项目,它的基础就是ASP.NET MVC的Razor。GITHub项目地址。

您可以在Windows Console或者Windows Forms使用它。

下面简单介绍如何使用。

1.创建一个Windows Console

2.通过NuGet安装RazorEngine

RazorEngine

3.下面先介绍使用字符串替代cshtML文件模板的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using RazorEngine;
using RazorEngine.Templating;
namespace RazorDemo.ConsoleDemo
{
    class PRogram
    {
        static void Main(string[] args)
        {
            VAR template = "Hello @Model.Name, welcome to use RazorEngine!";
            var result = Engine.Razor.RunCompile(template, "templateKey1"nullnew { Name = "World" });
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

运行程序,查看结果。

 

4.下面是使用cshtml文件作为模板的代码。

4.1添加一个Model文件夹,然后添加一个类UserInfo作为Model。

1
2
3
4
5
6
7
8
namespace RazorDemo.ConsoleDemo.Model
{
    public class UserInfo
    {
        public string FirstName { getset; }
        public string LastName { getset; }
    }
}

  

4.2添加一个文件夹Templates,然后添加一个html文件,将扩展名改为cshtml。这个文件属性改为内容Content,总是拷贝Copy always。

1
2
3
4
5
6
7
8
9
10
11
12
13
@model RazorDemo.ConsoleDemo.Model.UserInfo
<!DOCTYPE html>
 
<html lang="en" XMlns="http://www.w3.org/1999/xhtml">
<head>
    <;meta charset="utf-8" />
    <title>Welcome</title>
</head>
<body>
    <p>Hi @Model.FirstName @Model.LastName,</p>
    <p>Welcome to use RazorEngine!</p>
</body>
</html>

 

4.3最后修改Main方法的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using RazorEngine;
using RazorEngine.Templating;
using RazorDemo.ConsoleDemo.Model;
namespace RazorDemo.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = AppDomain.currentDomain.BaseDirectory + "templates\hello.cshtml";
            var template = File.ReadAllText(path);
            var model = new UserInfo { FirstName = "Bill", LastName = "Gates" };
            var result = Engine.Razor.RunCompile(template, "templateKey2"null, model);
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

运行程序,查看结果。

运行程序的时候Console可能会输出一大串有关临时文件的信息,可以忽略。如果想去掉,请参考这里。

其实在后台RazorEngine将模板和模型结合后生成了一个对应的临时类文件,然后调用Execute方法生成最终结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace CompiledRazorTemplates.Dynamic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    [RazorEngine.COMpilation.HasDynamicModelAttribute()]
    public class RazorEngine_77696eebc8a14ee8b43cc5e2d283e65a : RazorEngine.Templating.TemplateBase<RazorDemo.ConsoleDemo.Model.UserInfo>
    {
        public RazorEngine_77696eebc8a14ee8b43cc5e2d283e65a()
        {
        }
        public override void Execute()
        {
            WriteLiteral("<!DOCTYPE html>rnrn<html");
            WriteLiteral(" lang="en"");
            WriteLiteral(" xmlns="http://www.w3.org/1999/xhtml"");
            WriteLiteral(">rn<head>rn    <meta");
            WriteLiteral(" charset="utf-8"");
            WriteLiteral(" />rn    <title>Welcome</title>rn</head>rn<body>rn    <p>Hi ");
            Write(Model.FirstName);
            WriteLiteral(" ");
            Write(Model.LastName);
            WriteLiteral(",</p>rn    <p>Welcome to use RazorEngine!</p>rn</body>rn</html>rn");
        }
    }
}

  

RazorEngine的用途很多,只要有模板(cshtml)和模型(Model)就可以使用。比如

1)制作一个代码生成器

2)生成邮件正文

 

如果不妥之处,请见谅。

 转自:模版引擎RazorEngine简介 - 一点一滴,日积月累 - 博客园 (cnblogs.com)

ASP.NET MVC的Razor想必大家都比较熟悉,这里介绍一个独立于ASP.NET的RazorEngine。

RazorEngine是一个开源的项目,它的基础就是ASP.NET MVC的Razor。GitHub项目地址。

您可以在Windows Console或者Windows Forms使用它。

下面简单介绍如何使用。

1.创建一个Windows Console

2.通过NuGet安装RazorEngine

RazorEngine

3.下面先介绍使用字符串替代cshtml文件模板的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using RazorEngine;
using RazorEngine.Templating;
namespace RazorDemo.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var template = "Hello @Model.Name, welcome to use RazorEngine!";
            var result = Engine.Razor.RunCompile(template, "templateKey1"nullnew { Name = "World" });
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

运行程序,查看结果。

 

4.下面是使用cshtml文件作为模板的代码。

4.1添加一个Model文件夹,然后添加一个类UserInfo作为Model。

1
2
3
4
5
6
7
8
namespace RazorDemo.ConsoleDemo.Model
{
    public class UserInfo
    {
        public string FirstName { getset; }
        public string LastName { getset; }
    }
}

  

4.2添加一个文件夹Templates,然后添加一个html文件,将扩展名改为cshtml。这个文件属性改为内容Content,总是拷贝Copy always。

1
2
3
4
5
6
7
8
9
10
11
12
13
@model RazorDemo.ConsoleDemo.Model.UserInfo
<!DOCTYPE html>
 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Welcome</title>
</head>
<body>
    <p>Hi @Model.FirstName @Model.LastName,</p>
    <p>Welcome to use RazorEngine!</p>
</body>
</html>

 

4.3最后修改Main方法的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using RazorEngine;
using RazorEngine.Templating;
using RazorDemo.ConsoleDemo.Model;
namespace RazorDemo.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + "templates\hello.cshtml";
            var template = File.ReadAllText(path);
            var model = new UserInfo { FirstName = "Bill", LastName = "Gates" };
            var result = Engine.Razor.RunCompile(template, "templateKey2"null, model);
            Console.WriteLine(result);
            Console.Read();
        }
    }
}

运行程序,查看结果。

运行程序的时候Console可能会输出一大串有关临时文件的信息,可以忽略。如果想去掉,请参考这里。

其实在后台RazorEngine将模板和模型结合后生成了一个对应的临时类文件,然后调用Execute方法生成最终结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace CompiledRazorTemplates.Dynamic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    [RazorEngine.Compilation.HasDynamicModelAttribute()]
    public class RazorEngine_77696eebc8a14ee8b43cc5e2d283e65a : RazorEngine.Templating.TemplateBase<RazorDemo.ConsoleDemo.Model.UserInfo>
    {
        public RazorEngine_77696eebc8a14ee8b43cc5e2d283e65a()
        {
        }
        public override void Execute()
        {
            WriteLiteral("<!DOCTYPE html>rnrn<html");
            WriteLiteral(" lang="en"");
            WriteLiteral(" xmlns="http://www.w3.org/1999/xhtml"");
            WriteLiteral(">rn<head>rn    <meta");
            WriteLiteral(" charset="utf-8"");
            WriteLiteral(" />rn    <title>Welcome</title>rn</head>rn<body>rn    <p>Hi ");
            Write(Model.FirstName);
            WriteLiteral(" ");
            Write(Model.LastName);
            WriteLiteral(",</p>rn    <p>Welcome to use RazorEngine!</p>rn</body>rn</html>rn");
        }
    }
}

  

RazorEngine的用途很多,只要有模板(cshtml)和模型(Model)就可以使用。比如

1)制作一个代码生成器

2)生成邮件正文

 

如果不妥之处,请见谅。

 转自:模版引擎RazorEngine简介 - 一点一滴,日积月累 - 博客园 (cnbLOGs.com)

脚本宝典总结

以上是脚本宝典为你收集整理的RazorEngine全部内容,希望文章能够帮你解决RazorEngine所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。