The project is in a healthy, maintained state
A lightweight Mustache-style template engine supporting variable interpolation, sections, inverted sections, and nested scopes with safe rendering that never raises on missing variables.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-template

Gem Version CI License

Logic-less Mustache-style template engine with safe rendering for Ruby.

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem 'philiprehberger-template'

Or install directly:

gem install philiprehberger-template

Usage

require 'philiprehberger/template'

# Simple variable interpolation
tpl = Philiprehberger::Template.new('Hello, {{name}}!')
tpl.render(name: 'World')
# => "Hello, World!"

# Load from file
tpl = Philiprehberger::Template.from_file('greeting.mustache')
tpl.render(name: 'World')

# Sections (truthy/falsy)
tpl = Philiprehberger::Template.new('{{#show}}visible{{/show}}')
tpl.render(show: true)   # => "visible"
tpl.render(show: false)  # => ""

# Array iteration
tpl = Philiprehberger::Template.new('{{#items}}* {{name}}\n{{/items}}')
tpl.render(items: [{ name: 'Alice' }, { name: 'Bob' }])
# => "* Alice\n* Bob\n"

# Inverted sections
tpl = Philiprehberger::Template.new('{{^items}}No items found.{{/items}}')
tpl.render(items: [])
# => "No items found."

# Nested scopes (child inherits parent variables)
tpl = Philiprehberger::Template.new('{{#user}}{{greeting}}, {{name}}{{/user}}')
tpl.render(greeting: 'Hi', user: { name: 'Alice' })
# => "Hi, Alice"

Supported Syntax

Tag Description
{{var}} Variable interpolation (missing variables render as empty string)
{{#section}}...{{/section}} Section block (renders if truthy; iterates if array)
{{^section}}...{{/section}} Inverted section (renders if falsy or empty)

API

Philiprehberger::Template.new(source)

Compiles a template string into a renderable template.

Philiprehberger::Template.from_file(path)

Reads a file and compiles its contents as a template.

#render(variables = {})

Renders the template with the given variable hash. Accepts both symbol and string keys. Missing variables produce an empty string.

Development

bundle install
bundle exec rspec
bundle exec rubocop

License

MIT