NVelocity


NVelocity.dllはCastleプロジェクトのアーカイブの中のものを使用しています。


まず、NVelocity.propertiesとしては以下の様なものを用意しました。
エンコードの指定だけですが。

input.encoding=Shift-JIS
output.encoding=Shift-JIS

そしてテンプレートファイルTest.vmは以下のようなものを用意しました。

$name だよもん。

NVelocity.dllを参照設定して、以下をusing。

using NVelocity;
using NVelocity.App;
using NVelocity.Exception;

あとはこんなコードで、「うさうさ だよもん。」みたいなメッセージが取得できます。

Velocity.Init();

VelocityContext ctx = new VelocityContext();

ctx.Put( "name", "うさうさ" );

StringWriter writer = new StringWriter();
Velocity.MergeTemplate( "Test.vm", "Shift-JIS", ctx, writer );
System.Windows.Forms.MessageBox.Show( writer.GetStringBuilder().ToString() );

この例のような単純な置換以外に、条件分岐や繰り返しもできます。
用途として思いつくのは送信メールの文面作成とかですが、他にも何か面白い使い方ってあるんでしょうかね(´ω`)