🧩

Poshstache

A PowerShell implementation of Mustache templates — render dynamic content from simple templates using hashtables and objects.

35 Stars
⬇️ 50,000+ Downloads
🏷️ v1.2.0 Version
📜 MIT License
Install: Install-Module -Name Poshstache
⬇️ PowerShell Gallery View on GitHub

Documentation

Overview

Poshstache is a PowerShell module that implements the Mustache logic-less template specification. It lets you render dynamic text content from simple .mustache (or any text) templates using PowerShell hashtables or objects.

Use Poshstache anywhere you need dynamic text generation: configuration files, HTML emails, deployment scripts, documentation, and more.


Installation

Install from the PowerShell Gallery:

# Install for current user
Install-Module -Name Poshstache -Scope CurrentUser

# Install system-wide (requires elevation)
Install-Module -Name Poshstache

# Import after installation
Import-Module Poshstache

Quick Start

Basic Template Rendering

Import-Module Poshstache

# Define a template string
$template = "Hello, {{name}}! You have {{count}} new messages."

# Provide data as a hashtable
$data = @{
    name  = "Alice"
    count = 5
}

# Render the template
$result = ConvertTo-PoshstacheTemplate -InputString $template -ParametersObject ($data | ConvertTo-Json)
# Output: "Hello, Alice! You have 5 new messages."
Write-Host $result

Rendering from a File

# template.mustache
# Dear {{recipient}},
# Your order #{{order_id}} has been {{status}}.

$data = @{
    recipient = "Bob Smith"
    order_id  = "ORD-9821"
    status    = "shipped"
} | ConvertTo-Json

$result = ConvertTo-PoshstacheTemplate -InputFile "template.mustache" -ParametersObject $data
Write-Output $result

Commands

ConvertTo-PoshstacheTemplate

Renders a Mustache template with the provided parameters.

ParameterTypeDescription
-InputStringstringTemplate content as a string
-InputFilestringPath to a template file
-ParametersObjectstringJSON string with template variables

Template Syntax

SyntaxDescription
{{variable}}Renders the value of variable (HTML-escaped)
{{{variable}}}Renders the value unescaped
{{#section}}...{{/section}}Conditional/loop block
{{^section}}...{{/section}}Inverted conditional (renders when falsy)
{{! comment }}Comment (not rendered)
{{> partial}}Partial template

Advanced Examples

Lists and Loops

$template = @"
Shopping list:
{{#items}}
- {{name}} ({{quantity}})
{{/items}}
"@

$data = @{
    items = @(
        @{ name = "Apples"; quantity = "6" }
        @{ name = "Bread";  quantity = "1 loaf" }
        @{ name = "Milk";   quantity = "2 liters" }
    )
} | ConvertTo-Json -Depth 5

$result = ConvertTo-PoshstacheTemplate -InputString $template -ParametersObject $data

Config File Generation

# appsettings.mustache
# {
#   "ConnectionString": "{{db_server}}/{{db_name}}",
#   "Environment": "{{env}}",
#   "LogLevel": "{{log_level}}"
# }

$config = @{
    db_server = "prod-sql.company.com"
    db_name   = "AppDatabase"
    env       = "Production"
    log_level = "Warning"
} | ConvertTo-Json

ConvertTo-PoshstacheTemplate -InputFile "appsettings.mustache" -ParametersObject $config |
    Out-File "appsettings.json" -Encoding utf8

Contributing

Contributions are welcome! See the GitHub repository for details.

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

License

Poshstache is released under the MIT License.

Related Blog Posts