All Projects → straightdave → Eps

straightdave / Eps

Licence: other
A templating engine for PowerShell

Programming Languages

powershell
5483 projects

Projects that are alternatives of or similar to Eps

Tangular
A simple JavaScript template engine like Angular.js for websites or node.js
Stars: ✭ 58 (-46.3%)
Mutual labels:  template-engine
Hydrogen
🎈 Hydrogen. Voted (by me) the world's lightest static-site generator built with TypeScript ❤ It uses 🔥 lit-html inspired templating for super duper performant template generation.
Stars: ✭ 80 (-25.93%)
Mutual labels:  template-engine
Emerald
HTML5 templating engine for Nimrod
Stars: ✭ 91 (-15.74%)
Mutual labels:  template-engine
Yii2 Smarty
Yii 2 Smarty Extension.
Stars: ✭ 67 (-37.96%)
Mutual labels:  template-engine
Handlebars.java
Logic-less and semantic Mustache templates with Java
Stars: ✭ 1,204 (+1014.81%)
Mutual labels:  template-engine
Jingoo
OCaml template engine almost compatible with jinja2
Stars: ✭ 86 (-20.37%)
Mutual labels:  template-engine
Jb
A simple and fast JSON API template engine for Ruby on Rails
Stars: ✭ 1,075 (+895.37%)
Mutual labels:  template-engine
Geco
Simple code generator based on a console project, running on .Net core and using C# interpolated strings
Stars: ✭ 97 (-10.19%)
Mutual labels:  template-engine
Tuxedo
Tuxedo is a template language for Swift.
Stars: ✭ 80 (-25.93%)
Mutual labels:  template-engine
Cheatsheet Maker
Cheetsheet (cheat sheet or quick reference) generator. Use it for guides, instructions or study. Made in Python 3
Stars: ✭ 91 (-15.74%)
Mutual labels:  template-engine
Solid
Liquid template engine in Elixir
Stars: ✭ 68 (-37.04%)
Mutual labels:  template-engine
Jiko
Full Featured Template Engine for JavaScript
Stars: ✭ 72 (-33.33%)
Mutual labels:  template-engine
Metalsmith React Templates
A metalsmith plugin to render files using React / Preact / JSX based templates.
Stars: ✭ 90 (-16.67%)
Mutual labels:  template-engine
Awesome Twig
A curated list of amazingly awesome Twig extensions, snippets and tutorials
Stars: ✭ 63 (-41.67%)
Mutual labels:  template-engine
Materialtabstemplate
MaterialTabsTemplate is solely created to reduce the burden of writing same boiler plate codes for Tab creation in Android.
Stars: ✭ 92 (-14.81%)
Mutual labels:  template-engine
Liquor
Liquor is a safe sandboxing compiling template language for Ruby
Stars: ✭ 57 (-47.22%)
Mutual labels:  template-engine
Html
A Virtual DOM based templating-engine for PHP
Stars: ✭ 86 (-20.37%)
Mutual labels:  template-engine
Art Template
High performance JavaScript templating engine
Stars: ✭ 9,554 (+8746.3%)
Mutual labels:  template-engine
Api2html
Using the data from your API, generate the HTML on the fly! Server-side rendering of the mustache templates
Stars: ✭ 97 (-10.19%)
Mutual labels:  template-engine
Gdb
Generic Data Binder (GDB) for jQuery is a framework agnostic and extremely easy to use 2 way data binder. GDB binds views and models in realtime with live two-way binding and no hefty framework necessary.
Stars: ✭ 90 (-16.67%)
Mutual labels:  template-engine

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:

  • Code in <% CODE %> blocks are executed but no value is inserted.
    • If started with <%- : the preceding indentation is trimmed.
    • If terminated with -%> : the following line break is trimmed.
  • Code in <%= EXPRESSION %> blocks insert the value of EXPRESSION.
    • If terminated with -%> : the following line break is trimmed.
  • Text in <%# ... %> blocks are treated as comments and are removed from the output.
    • If terminated with -%> : the following line break is trimmed.
  • <%% and %%> : are replaced respectively by <% and %> in the output.

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>]
  • use -Template to render the template in the corresponding string. than a file
  • use -Path to render the template in the corresponding file.
  • -Safe renders the template in isolated mode (in another thread/powershell instance) to avoid variable pollution (variable that are already in the current scope).
  • if -Safe is provided, you must bind your values using -Binding option with a Hashtable containing key/value pairs.

Example

A very simple example of EPS would be :

$name = "Dave"

Invoke-EpsTemplate -Template 'Hello <%= $name %>!'

This script produces the following result:

Hello Dave!

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 (render the template in an isolated scope) execute: 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

Multi-line code or expression blocks

You can use multi-line statements in blocks:

$name = "Dave"

Invoke-EpsTemplate -Template @'
Hello <%= $name %>!
Today is <%= Get-Date -UFormat %x %>.
'@

will produce:

Hello Dave!
Today is 11/12/17.

Iterating and joining the results

Sometimes we would like to iterate over a collection, generate some text for each element and finally join the generated blocks together with a separator.

Inside expression blocks

In an expression block we can use the following idiomatic PowerShell snippet:

Invoke-EpsTemplate -Template @'
<%= ("Id", "Name", "Description" | ForEach-Object { "[String]`$$_" }) -Join ",`n" -%>
'@

which would generate the following result:

[String]$Id,
[String]$Name,
[String]$Description

With EPS templating elements

However, due to EPS internal workings, the following code would not work:

Invoke-EpsTemplate -Template @'
<% ("Id", "Name", "Description" | ForEach-Object { -%>
[String]$<%= $_ -%>
<% }) -join ",`n" -%>
'@

The -join operator is ignored by EPS:

[String]$Id[String]$Name[String]$Description

This is expected behavior because -join is applied to the result of ForEach-Object which is defined inside a CODE block and should not produce any output.

EPS provides an internal Each function whose behavior is similar to ForEach-Object but achieves the desired result inside a template :

Each [-Process] <scriptblock> [-InputObject <Object[]>] [-Begin <scriptblock>] [-End <scriptblock>] [-Join <string>]

Each can only be used in PS v3 or above.

This snippet of EPS would generate the desired result (notice that -Join is a parameter of Each and is not applied to its result value as would be the case with the -join operator):

Invoke-EpsTemplate -Template @'
<% "Id", "Name", "Description" | Each { -%>
[String]$<%= $_ -%>
<% } -Join ",`n" -%>
'@

and generate:

[String]$Id,
[String]$Name,
[String]$Description

In some cases it can be useful to also generate a prefix and suffix to the iterated part:

Invoke-EpsTemplate -Template @'
<% "Id", "Name", "Description" | Each { -%>
[String]$<%= $_ -%>
<% } -Begin { %>[NSSession]$Session<% } -End { %>[String]$LogLevel<% } -Join ",`n" -%>
'@

will generate:

[NSSession]$Session,
[String]$Id,
[String]$Name,
[String]$Description,
[String]$LogLevel

Notice that when using -Begin and/or -End with -Join all blocks are joined together.

If you want to prefix and suffix, without joining the prefix and suffix, use the following pattern:

Invoke-EpsTemplate -Template @'
Param(
<% "Id", "Name", "Description" | Each { -%>
    [String]$<%= $_ -%>
<% } -Join ",`n" %>
)
'@

Which will generate:

Param(
    [String]$Id,
    [String]$Name,
    [String]$Description
)

Iterating with an index

In some cases it is useful to iterate over a collection while maintaining an index of the current item. The Each function exposes a $index variable to the script blocks it executes. The $index variable starts at 0 for the first element.

Invoke-EpsTemplate -Template @'
<% "Dave", "Bob", "Alice" | Each { -%>
<%= $Index + 1 %>. <%= $_ %>
<% } -%>
'@

would generate the following listing:

1. Dave
2. Bob
3. Alice

Handling default values

Using default values if the provided variable is $Null or empty (as returned by the [string]::IsNullOrEmpty function) is a very common pattern which can be done easily with a template like:

$config = [PSCustomObject]@{
  Host = "localhost"
  #Port = "8080" # this would be an optional configuration value
}
Invoke-EpsTemplate -Template @'
<%= $config.Host 
%>:<%= 
  if ([string]::IsNullOrEmpty($config.Port)) { "80" } else { $config.Port }
%>
'@

EPS provides a Get-OrElse function that allows for a shorter version:

$config = [PSCustomObject]@{
  Host = "localhost"
  #Port = "8080" # this would be an optional configuration value
}
Invoke-EpsTemplate -Template @'
<%= $config.Host %>:<%= Get-OrElse $config.Port "80" %>
<%= $config.Host %>:<%=  $config.Port | Get-OrElse -Default "80" %>
'@

Helper functions

If there's a more complicated piece of powershell code that should be reused across multiple files, it can be encapsulated in a helper function, i.e.:

$helpers = @{
   NumberedList = { 
     param($arr)
     $i = 1
     $arr | foreach-object { "$i. $_"; $i++ } | out-string 
  }
}

$helpers should be passed to Invoke-Eps:

Invoke-EpsTemplate -Path Test.eps -helpers $helpers

Now, NumberedList can be used in the template.

<%= NumberedList "Dave", "Bob", "Alice" %>

would generate the following listing:

1. Dave
2. Bob
3. Alice

Contribution

Help find more bugs! Or find more usage of this tool... Author's email: [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].