Created
April 22, 2015 15:58
-
-
Save sunnyone/e6948988a8e125b779ba to your computer and use it in GitHub Desktop.
Invoke RazorEngine from PowerShell
This file contains 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
# 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