Created
July 31, 2017 15:01
-
-
Save akira345/366f02b193e2d596e461d39618d1a356 to your computer and use it in GitHub Desktop.
c#でテンプレートエンジンRazorEngineを使うサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Windows.Forms; | |
| using RazorEngine; | |
| using System.IO; | |
| using RazorEngine.Configuration; | |
| using RazorEngine.Text; | |
| using RazorEngine.Templating; | |
| namespace template_sample | |
| { | |
| public partial class Form1 : Form | |
| { | |
| public Form1() | |
| { | |
| InitializeComponent(); | |
| } | |
| private void button1_Click(object sender, EventArgs e) | |
| { | |
| string TemplateFilePath = @"C:\temp\Templates\test.tpl"; | |
| string OutputFilePath = @"c:\temp\Templates\test.txt"; | |
| var template = File.ReadAllText(TemplateFilePath); | |
| var model = new | |
| { | |
| Name = "hogehoge", | |
| TEL = "000-111-2345" | |
| }; | |
| //config | |
| var config = new TemplateServiceConfiguration(); | |
| config.Language = Language.CSharp; | |
| config.EncodedStringFactory = new RawStringFactory(); | |
| //Service | |
| var service = RazorEngineService.Create(config); | |
| Engine.Razor = service; | |
| //Generate | |
| string ret = Engine.Razor.RunCompile(template, "templatekey", null, model); | |
| File.WriteAllText(OutputFilePath,ret); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment