その中使っていたスクリプトがこちら。
This file contains hidden or 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 |
DynamicViewBagがModelってナニソレって感じなんですが、このRazorEngineは、Modelクラスを作って渡すことを想定されているので、クラスを作るのが大変なPowerShellからはまっとうな呼び方をするのが難しかったのです。いわば抜け穴。これを探してたのは、LTしませんかと現場で言われてから発表までの話なので、もうちょっと頑張ればいい方法があるかもしれませんが、がんばってません。ASP.NET MVC屋的に気持ち悪いだけで、名前がアレなのを除けば、PowerShellとのつなぎという意味ではそれほど悪くないかなとも思っていますが、このまま行くか頑張って直すかは使われる皆様の判断にお任せします。
ちなみにおすすめはC#からRazorEngineを使う、です。
追記
RazorEngineのソースを見たところ、当時読んでたソースとだいぶ変わっているようです。もしかすると、こんなややこしい呼び方は要らないかもしれません。もし動かないとかあれば、RazorEngineのソースをご確認ください。
追記2
altriveさんが、ModelにPSCustomObjectを渡す例を書いてくれました。一度コンパイルするといいらしいです。要ちぇっく。
https://gist.github.com/altrive/2f048e4c78398c055090
0 件のコメント:
コメントを投稿