PowerShellASP
前にこのエントリで、ASP.NETからPowerShellスクリプトを呼び出してビュー(というよりデータだけど)を生成するというのをやったけど、それを上回るものを見つけたのでご紹介。
これはPowerShellのエンジンをASP.NETに組み込んでテンプレートエンジンとして使うという代物。
ぐだぐだ説明するよりもコードを見た方が早いので、以下をごらんあれ。
Default.ps1x
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>PowerShellASP</title> </head> <body> <table border="1"> <tr> <th>名前</th> <th>サイズ</th> </tr> <% Get-ChildItem c:\ | % { %> <tr> <td><%= $_.Name %></td> <td><%= $_.Length %></td> </tr> <% } %> </table> </body> </html>
出力結果はだいたい想像できるだろうから割愛するとして、見ての通り「<% %>」で囲んだ部分にPowerShellのスクリプトを記述していくというスタイルを取っている。このへんはWebフォームと同じなので特に違和感は無いと思う。
サーバーサイドとの連携には、以下のように「$Request」変数や「$Response」変数が事前に設定されているようなので、それらを使ってやり取りすることになる。
- $Request: Contains the HttpRequest object.
- $Server: Contains the HttpUtility object.
- $Session: Contains the HttpSession object.
- $Application: Contains the HttpApplication object.
- $Response: Contains the HttpResponse object. You can write directly to the response stream from code if you want, or you can use write-host and friends as well.
- $Cache: Contains the HttpCache object.
- $Context: The HttpContext object associated with the current request.
PowerShellでデータを取得して、手っ取り早く画面に表示したい時なんかは一つの選択肢としてありかもしれない。
ASP.NET MVCをはじめとして.NETの世界にもテンプレートエンジンが続々と出てきた感がする今日このごろ。そういやNVelocityはどうなったんだろうか?