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