Skip to content

Instantly share code, notes, and snippets.

@akira345
Created July 31, 2017 15:01
Show Gist options
  • Save akira345/366f02b193e2d596e461d39618d1a356 to your computer and use it in GitHub Desktop.
Save akira345/366f02b193e2d596e461d39618d1a356 to your computer and use it in GitHub Desktop.
c#でテンプレートエンジンRazorEngineを使うサンプル
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