今天讨论的是如何在Powershell里实现一个简单的Template Engine的功能。

假设模板文件的内容如下:template.tt

hello $name
welcome $company

模板引擎的函数定义在Invoke-Template.ps1

function Invoke-Template {
param(
[string]$Path,
[Scriptblock]$ScriptBlock
)
function Get-Template {
param($TemplateFileName) $content = [IO.File]::ReadAllText(
(Join-Path $Path $TemplateFileName) ) $res = Invoke-Expression "@`"`r`n$content`r`n`"@"
return $res
}
& $ScriptBlock
}

调用的Powershell script是

. .\Invoke-Template.ps1
$root = $PSScriptRoot
$res = invoke-Template -Path $root -scriptblock { $name="andy"
$company="hp"
Get-Template tempalte.tt
} $res

输出为

最新文章

  1. 记在virtualbox下挂载共享文件夹的方法
  2. 本机ip+端口不能访问web server,外部却可以访问
  3. CSS3中box-sizing的理解
  4. 记一次小团队Git实践(中)
  5. 从自签名证书导出pfx和cer证书
  6. 解决装系统选中的磁盘采用的是GPT分区形式
  7. SQL Server 2008 R2,显示SQL语句执行窗口。 编辑前200行,可以执行SQL语句
  8. LeetCode 274
  9. jquery ListBox 左右移动
  10. Need a code of lazy load for div--reference
  11. dojo事件
  12. projecteuler---->problem=9----Special Pythagorean triplet
  13. ios的自动转屏
  14. [开发者账号] ios7苹果开发者账号申请
  15. html 压缩工具 html-minifier
  16. LeetCode算法题-Can Place Flowers(Java实现)
  17. cocos2d-x学习之路(三)——精灵与动作
  18. asp.net c# repeater或gridview导出EXCEL的详细代码。
  19. ActiveX 控件导入程序
  20. 获取APP的启动图 -Launch Image

热门文章

  1. python pdb 基础调试
  2. 工作的时候用到spring返回xml view查到此文章亲测可用
  3. Android Notification通知
  4. 【总结整理】js获取css的属性(内部,外部,内嵌(写在tag中))
  5. SpringMVC接收对象数组参数进行封装
  6. Python 安装 django框架
  7. 你可能不知道的pdf的功能
  8. 解决校园Dr客户端端口占用问题(2)
  9. rest-framework组件 之 认证与权限组件
  10. arp绑定