Skip to content

Instantly share code, notes, and snippets.

@sunnyone
Created April 22, 2015 15:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnyone/e6948988a8e125b779ba to your computer and use it in GitHub Desktop.
Save sunnyone/e6948988a8e125b779ba to your computer and use it in GitHub Desktop.
Invoke RazorEngine from PowerShell
# nuget.exe install RazorEngine
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Add-Type -LiteralPath "${scriptDir}\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll"
Add-Type -LiteralPath "${scriptDir}\RazorEngine.3.4.1\lib\net45\RazorEngine.dll"
# テンプレートに渡すデータ
$array = @(1; 2; 3)
$table = @{ Name = "F#"; ShowDetail = $true; Array = $array }
# テンプレート
$template = @"
Hello @Model.Name! Welcome to Razor!
@if (Model.ShowDetail) {
@:Plain Text!! はろー
}
@foreach (var item in Model.Array) {
@:Item value: @item => @item
}
"@
# ViewBagをModelとして渡す → 割りと普通じゃない
$model = New-Object RazorEngine.Templating.DynamicViewBag -ArgumentList @($null)
$model.AddDictionaryValues($table)
[RazorEngine.Razor]::Parse($template, $model, $null)
Write-Output $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment