PHPackages                             nabeghe/risma - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nabeghe/risma

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nabeghe/risma
=============

A lightweight, flexible string processing and template engine for PHP with function chaining support.

v1.0.0(2mo ago)261MITPHPPHP &gt;=7.4

Since Feb 3Pushed 1mo agoCompare

[ Source](https://github.com/nabeghe/risma-php)[ Packagist](https://packagist.org/packages/nabeghe/risma)[ Docs](https://github.com/nabeghe/risma-php)[ RSS](/packages/nabeghe-risma/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

⚡ Risma
=======

[](#-risma)

**The Ultimate String Processing &amp; Template Pipeline for Modern PHP**

 [![PHP Version](https://camo.githubusercontent.com/f5550c01f8862fdd40fdce7cdd89e04e46748cb259cfe779dabebbc6b9510020/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d254532253839254135253230372e342d3737374242342e7376673f7374796c653d666c61742d737175617265266c6f676f3d706870)](https://camo.githubusercontent.com/f5550c01f8862fdd40fdce7cdd89e04e46748cb259cfe779dabebbc6b9510020/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d254532253839254135253230372e342d3737374242342e7376673f7374796c653d666c61742d737175617265266c6f676f3d706870) [![License](https://camo.githubusercontent.com/458425f8985b0b0c8a736cffe75e05a098e3d77906acddbcad2bfc54492a4e02/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/458425f8985b0b0c8a736cffe75e05a098e3d77906acddbcad2bfc54492a4e02/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666c61742d737175617265) [![Zero Dependencies](https://camo.githubusercontent.com/2cdf407bf8067aa406511821d3320df139f26fc6e227ecd8c53f370419ea9e31/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446570656e64656e636965732d5a65726f2d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/2cdf407bf8067aa406511821d3320df139f26fc6e227ecd8c53f370419ea9e31/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446570656e64656e636965732d5a65726f2d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265) [![Tests Pest](https://camo.githubusercontent.com/e649164b3fff69d80d933e1f5a6d1db543924cb98110f7f887ac8df7bf0406fb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d506573742d707572706c65)](https://camo.githubusercontent.com/e649164b3fff69d80d933e1f5a6d1db543924cb98110f7f887ac8df7bf0406fb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d506573742d707572706c65)

---

**Risma** is not just another template engine. It's a lightweight, high-performance, **zero-dependency** string processing pipeline for PHP.

It completely transforms how you manipulate strings, handle dynamic templates, and sanitize data by introducing a flexible, deeply recursive, and bulletproof pipe-like syntax. No `eval()`, no spaghetti code; just pure, chained logic.

🤯 Why Risma? (The Problem vs. The Risma Way)
--------------------------------------------

[](#-why-risma-the-problem-vs-the-risma-way)

Ever tried chaining multiple string manipulations in native PHP? It gets ugly, fast.

**❌ Native PHP (Hard to read, inside-out execution):**

```
$output = sprintf("Status: %s", ucfirst(strtolower(trim($user['status'] ?? 'pending'))));
```

**✅ The Risma Way (Clean, pipeline execution):**

```
$output = $risma->render("Status: {status.or('pending').trim.strtolower.ucfirst}", $user);
```

---

✨ Enterprise-Grade Features
---------------------------

[](#-enterprise-grade-features)

- 🔗 **Infinite Function Chaining:** Pipe data through endless functions using simple dot notation.
- 🎯 **Smart Argument Routing (`$`):** Pinpoint exactly where your piped value should land in the next function's arguments.
- 🛡️ **Bulletproof Zero-Eval Lexer:** Advanced lookahead parser flawlessly handles internal unescaped quotes (e.g., `"sal"am"`) and escaped quotes (`"sal\"am"`) without breaking.
- 🪆 **Deep Recursion (Inception):** Nest placeholders inside function arguments infinitely.
- 🔌 **Ultimate Extensibility:** Inject your own custom functions, static classes, or instantiated objects directly into the engine.
- 🛠️ **Rich Built-in Arsenal:** Comes loaded with logic, formatting, and conditional operators out of the box.

---

🚀 Installation
--------------

[](#-installation)

Install via Composer and you are ready to go:

```
composer require nabeghe/risma
```

```
use Nabeghe\Risma\Risma;

// Initialize the engine
$risma = new Risma();
```

---

📖 The Master Guide (A to Z)
---------------------------

[](#-the-master-guide-a-to-z)

### 1. Simple Variable Injection

[](#1-simple-variable-injection)

Pass an associative array of data. Risma finds the placeholders and replaces them.

```
echo $risma->render("Hello {name}!", ['name' => 'Hadi']);
// Output: Hello Hadi!
```

### 2. The Pipeline (Function Chaining)

[](#2-the-pipeline-function-chaining)

Transform data on the fly. By default, the output of the current step becomes the **first argument** of the next function.

```
$text = "Welcome back, {user.trim.strtoupper}!";
echo $risma->render($text, ['user' => '  alice  ']);
// Output: Welcome back, ALICE!
```

### 3. Smart Argument Parsing

[](#3-smart-argument-parsing)

Pass arguments exactly like you would in PHP. Risma's internal lexer is incredibly smart. it handles inner quotes and commas inside strings flawlessly.

```
// Handles internal unescaped quotes effortlessly!
$text = "Message: {@sprintf(\"%s\", \"He said \"Hello\" to me\")}";
echo $risma->render($text, []);
// Output: Message: He said "Hello" to me
```

### 4. Advanced Argument Routing (`$`)

[](#4-advanced-argument-routing-)

Not every PHP function accepts the target string as the first argument (we're looking at you, `str_replace`). Use `$` to tell Risma exactly where to inject the piped value!

```
// str_replace(search, replace, subject) -> Subject is the 3rd argument.
$text = "Formatted: {slug.str_replace('-', ' ', '$')}";
echo $risma->render($text, ['slug' => 'open-source-is-awesome']);
// Output: Formatted: open source is awesome
```

### 5. Direct Execution (`@`)

[](#5-direct-execution-)

Need to execute a function to generate a root value without relying on a variable? Start with `@`.

```
echo $risma->render("Copyright {@date('Y')} - {@rand(1, 100)}", []);
// Output: Copyright 2026 - 42
```

### 6. Deep Nesting &amp; Recursion

[](#6-deep-nesting--recursion)

Risma recursively resolves placeholders from the inside out. You can embed placeholders inside function arguments of other placeholders!

```
$template = '{@sprintf("%s %s", "{@ucfirst("{first}")}", "{@ucfirst("{last}")}")}';
echo $risma->render($template, ['first' => 'hadi', 'last' => 'akbarzadeh']);
// Output: Hadi Akbarzadeh
```

### 7. Escaping Variables (`!`)

[](#7-escaping-variables-)

If you need to render the literal `{braces}` without Risma parsing them, prefix with `!`.

```
echo $risma->render("Use !{variable} to write a variable.", []);
// Output: Use {variable} to write a variable.
```

---

🧰 The Built-in Arsenal (`Functions.php`)
----------------------------------------

[](#-the-built-in-arsenal-functionsphp)

Risma includes a powerful suite of native helpers. You can chain them endlessly to achieve complex logical operations directly inside your string.

### Logic &amp; Conditionals

[](#logic--conditionals)

- **`ok`**: Returns `'1'` if truthy, `'0'` if falsy. *(Great for boolean flags)*
- **`exists`**: Returns `'1'` if not null/empty, otherwise `'0'`.
- **`or('default')`**: Fallback value if the variable is empty.
- **`and('suffix')`**: Appends text *only* if the variable is not empty.
- **`if_empty('yes', 'no')`** / **`if_not_empty('yes', 'no')`**: Conditional text based on emptiness.
- **`if_blank('yes', 'no')`** / **`if_not_blank('yes', 'no')`**: Like empty, but also treats invisible unicode spaces as blank!
- **`if_equals('target', 'yes', 'no')`**: Strict comparison.
- **`if_numeric('yes', 'no')`**: Checks if the piped value is a number.

*(Note: You can use `%s` in the 'yes' or 'no' arguments to inject the original value!)*

### String Manipulation

[](#string-manipulation)

- **`prepend('prefix1', 'prefix2', ...)`**: Adds strings to the beginning.
- **`append('suffix1', 'suffix2', ...)`**: Adds strings to the end.
- **`flatten_lines`**: Squashes multi-line text (`\n`, `\r\n`) into a single, clean space-separated line.
- **`remove_lines`**: Completely strips line breaks.
- **`maybe_plural_s`**: Returns `'s'` if the piped integer is &gt; 1.
- **`line`**: Generates a raw `\n` line break.

---

🤯 Real-World Scenario: Complex Notification System
--------------------------------------------------

[](#-real-world-scenario-complex-notification-system)

Let's combine everything into a single, highly complex template that formats an email notification, handles missing data, flattens line breaks, and evaluates conditionals—all in one string!

```
$template = 'Subject: {@sprintf("New message from %s", "{sender.trim.or(\"Unknown User\")}")} ' .
            '| Body: {message.flatten_lines.if_empty("No content.", ">> %s
