View on GitHub

eps

embedded PowerShell (templating tool for PowerShell)

Download this project as a .zip file Download this project as a tar.gz file

Build status

EPS

EPS ( Embedded PowerShell ), inspired by ERB, is a templating tool that embeds PowerShell code into a text document. It is conceptually and syntactically similar to ERB for Ruby or Twig for PHP.

EPS can be used to generate any kind of text. The example below illustrates generating plain text, but it could be used to generate HTML as in DS or PowerShell code as in the Forge Module generator.

EPS is available in the PowerShell Gallary. You can install the module with the following command:

Install-Module -Name EPS 

Syntax

EPS allows PowerShell code to be embedded within a pair of <% ... %>, <%= ... %>, or <%# ... %> as well:

All blocks accept multi-line content as long as it is valid PowerShell.

Command Line Usage

Invoke-EpsTemplate [-Template <string>] [-Binding <hashtable>] [-Safe]  [<CommonParameters>]
    
Invoke-EpsTemplate [-Path <string>] [-Binding <hashtable>] [-Safe]  [<CommonParameters>]

Example

In a template file ‘Test.eps’:

Hi <%= $name %>

<%# this is a comment -%>
Please buy me the following items:
<% 1..5 | %{ -%>
  - <%= $_ %> pigs ...
<% } -%>

Dave is a <% if($True) { %>boy<% } else { %>girl<% } %>. 

Thanks,
Dave
<%= (Get-Date -f yyyy-MM-dd) %>

Then render it in on the command line:

Import-Module EPS

$name = "ABC"
Invoke-EpsTemplate -Path Test.eps

Here it is in non-safe mode (render template with values in current run space) To use safe mode: using Invoke-EpsTemplate -Path Test.eps -Safe with binding values

It will produce:

Hi dave

Please buy me the following items:
  - 1 pigs ...
  - 2 pigs ...
  - 3 pigs ...
  - 4 pigs ...
  - 5 pigs ...

Dave is a boy.

Thanks,
Dave
2016-12-07

Or you can use safe mode with data bindings:

Invoke-EpsTemplate -Path Test.eps -Safe -binding @{ name = "dave" }

which will generate the same output.

More examples

You can use multi-line statements in blocks:

$template = @'
<%=
  $name = "dave"
  
  1..5 | %{
    "haha"
  }
%>

Hello, I'm <%= $name %>.
'@

Invoke-EpsTemplate -Template $template

will produce:

haha haha haha haha haha

Hello, I'm dave.

Contribution

Help find more bugs! Or find more usage of this tool… Author’s email: eyaswoo@163.com