Skip to content

Instantly share code, notes, and snippets.

@altrive
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altrive/2f048e4c78398c055090 to your computer and use it in GitHub Desktop.
Save altrive/2f048e4c78398c055090 to your computer and use it in GitHub Desktop.
RazorEngine Test
#Require PSNuGet<https://github.com/altrive/PSNuGet>
Use-NuGetPackage RazorEngine -Version 3.6.6 -Verbose
# テンプレート
$template = @'
Hello @Model.Name! Welcome to Razor!
@if (Model.ShowDetail) {
@:Plain Text!! はろー
}
@foreach (var item in Model.Array) {
@:Item value: @item => @item
}
'@
# モデル
$model = [pscustomobject] @{
Name = "F#hoge"
ShowDetail = $true
Array = @(1; 2; 3;)
}
#Modelに[pscustomobject]を使う場合は事前コンパイルが必要(でないと初回にModel渡した際に必要なプロパティが存在しない旨のエラーが発生する)
$cacheKey = "TestTemplate"
[RazorEngine.Razor]::Compile($template, $cacheKey)
[RazorEngine.Razor]::Parse($template, $model, $cacheKey)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment