[PowerShell]template engine
2015-08-24 15:24
621 查看
今天讨论的是如何在Powershell里实现一个简单的Template Engine的功能。
假设模板文件的内容如下:template.tt
模板引擎的函数定义在Invoke-Template.ps1
调用的Powershell script是
输出为
假设模板文件的内容如下: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
输出为
相关文章推荐
- windows下git bash存储alias
- IBM developer Bash系列
- shell 使用字典
- Shell脚本查看linux系统性能瓶颈
- awk与shell之间的变量传递方法
- Linux目录结构、bash的基础特性、I/O重定向及管道
- 博客迁移:Shell脚本批量文件处理
- Bash shortcuts
- redhat系统使用xshell连接后无法弹出界面
- BASH find substring in string
- shell里的各种括号
- linux:bash基础
- 希尔排序(shell)
- BASH第七课第二题
- BASH第七课第一题
- 解决adb shell input text 中文输入,unicode转utf-8
- 查看linux中shell的几种方法汇总
- linux shell 正则表达式(BREs,EREs,PREs)差异比较
- linux shell 正则表达式(BREs,EREs,PREs)差异比较
- 有关PowerShell脚本你必须知道的十个基本概念