今天讨论的是如何在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

输出为

[PowerShell]template engine的更多相关文章

  1. The template engine

    Play has an efficient templating system which allows to dynamically generate HTML, XML, JSON or any ...

  2. 如何选择Javascript模板引擎(javascript template engine)?

    译者 jjfat 日期:2012-9-17  来源: GBin1.com 随着前端开发的密集度越来越高,Ajax和JSON的使用越来越频繁,大家肯定免不了在前台开发中大量的使用标签,常见到的例子如下: ...

  3. JFinal Template Engine 使用

    官方文档:JFinal Template Engine 文档

  4. 【Reship】use of tangible T4 template engine

    1.first of all 之前在 “使用T4模板生成代码 – 初探” 文章简单的使用了T4模板的生成功能,但对于一个模板生成多个实例文件,如何实现这个方式呢?无意发现一个解决方案 “Multipl ...

  5. 最近兰州的js风格写个插件和一个template engine

    /* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...

  6. Js template engine

    P http://www.jquery4u.com/javascript/10-javascript-jquery-templates-engines/ http://www.creativebloq ...

  7. Juicer——a fast template engine

    https://blog.csdn.net/yutao_struggle/article/details/79201688 当前最新版本: 0.6.8-stable Juicer 是一个高效.轻量的前 ...

  8. 500 Lines or Less: A Template Engine(模板引擎)

    介绍: 多数项目都是包含很多的逻辑处理,只有少部分的文字文本处理.编程语言非常擅长这类项目.但是有一些项目只包含了少量的逻辑处理,大量的文本数据处理.对于这些任务,我们期望有一个工具能够很好的处理这些 ...

  9. [Pug] Template Engine -- Jade/ Pug

    Looking at the follow code: .wrapper - const upName = name && name.toUpperCase(); h2 | Hello ...

随机推荐

  1. openGL一些概念02

    着色器程序 着色器程序对象(Shader Program Object)是多个着色器合并之后并最终链接完成的版本. 如果要使用刚才编译的着色器我们必须把他们链接为一个着色器程序对象,然后在渲染对象的时 ...

  2. DAY17-Django之model增删改

    添加表记录 普通字段 #方式1 publish_obj=Publish(name="人民出版社",city="北京",email="renMin@16 ...

  3. <c:set var="ctx" value="${pageContext.request.contextPath}" />的学习

    ${pageContext.request.contextPath},是获取当前根目录 set var="ctx",是给这个路径定义了一个变量,用的时候可以通过EL表达式获取:${ ...

  4. NLP整体流程的代码

    import nltk import numpy as np import re from nltk.corpus import stopwords # 1 分词1 text = "Sent ...

  5. Swing界面组件的通用属性

    ----------------siwuxie095                             Swing 界面组件(控件)的通用属性:         (1)enabled:启用/禁用 ...

  6. C++——explicit

    explicit构造函数是用来防止隐式转换的.请看下面的代码: class Test1 { public: Test1(int n) { num=n; }//普通构造函数 private: int n ...

  7. ReactNative http网络通讯

    安装 fetch npm install whatwg-fetch --save 1.fetch的Get方式通讯 async sendGet(){ let response = await fetch ...

  8. eclipse中安装git插件

    1 安装及配置git插件,问度娘即可 点击前往 2 eclipse 中怎么同步到 本地git仓库 和 码云远程仓库 点击前往

  9. 前端学习笔记2017.6.21-引入JS文件的方法

    通过网络引入JS文件 <script src="https://www.xxx.com/aaa.js"></script> 如果是引入本地JS文件 < ...

  10. 数字图像处理实验(6):PROJECT 04-02,Fourier Spectrum and Average Value 标签: 图像处理MATLABfft 2017-05-07 23:1

    实验要求: Objective: To observe the Fourier spectrum by FFT and the average value of an image. Main requ ...