PHPackages                             matronator/parsem - 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. [Templating &amp; Views](/categories/templating)
4. /
5. matronator/parsem

ActiveLibrary[Templating &amp; Views](/categories/templating)

matronator/parsem
=================

Parse file templates.

v3.3.3(4mo ago)41.1k↑300%[1 issues](https://github.com/matronator/parsem/issues)2MITHTMLPHP &gt;=8.1CI passing

Since Sep 1Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/matronator/parsem)[ Packagist](https://packagist.org/packages/matronator/parsem)[ GitHub Sponsors](https://github.com/sponsors/matronator)[ Fund](https://ko-fi.com/matronator)[ RSS](/packages/matronator-parsem/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (43)Used By (2)

Pars'Em Template Engine
=======================

[](#parsem-template-engine)

[![Pars'Em logo](.github/parsem-logo.png)](.github/parsem-logo.png)

Simple lightweight templating engine made in PHP.

Enhance your files with variables, conditional blocks and PHP functions as filters. Create re-usable templates by adding variable ``'s anywhere in your file and have the content change dynamically based on the arguments you provide.

- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
    - [Templates Syntax Highlighting for VS Code](#templates-syntax-highlighting-for-vs-code)
- [Usage](#usage)
    - [Template syntax](#template-syntax)
        - [Comments](#comments)
        - [Conditions](#conditions)
        - [Variables](#variables)
            - [Default values](#default-values)
        - [Filters](#filters)
    - [Built-in filters](#built-in-filters)
    - [Use in code](#use-in-code)
        - [Parse string or file](#parse-string-or-file)
        - [Methods](#methods)
            - [`Parser::parseString`](#parserparsestring)
            - [`Parser::parse`](#parserparse)

Features
--------

[](#features)

- Parse string templates to string
    - Replace variable placeholders with provided arguments
    - Apply filter functions to variables
        - Use [built-in filters](#built-in-filters) or provide custom functions
    - Use `` blocks to conditionally parse the template
        - Use `` blocks to provide an alternative content if the condition is not met
    - Include `` in the template files that are removed after patsing
- Parse template files to string
    - Parse the entire file as a string
- Provide a custom regex pattern to parse functions to use a custom syntax
- Get all variables from a template

Requirements
------------

[](#requirements)

- PHP &gt;= 8.2
- Composer

Installation
------------

[](#installation)

Install with Composer:

```
composer require matronator/parsem

```

And then just add the dependency to your PHP file/s:

```
use Matronator\Parsem\Parser;
```

### Templates Syntax Highlighting for VS Code

[](#templates-syntax-highlighting-for-vs-code)

To get syntax highlighting for template files (highlight `` and `` even inside strings), you can download the [MTRGen Templates Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=matronator.mtrgen-yaml-templates) extension for VS Code.

Usage
-----

[](#usage)

### Template syntax

[](#template-syntax)

#### Comments

[](#comments)

You can use comments in your templates by using the following syntax:

```

```

Comments are ignored by the parser and will be removed from the parsed output.

#### Conditions

[](#conditions)

You can use conditions in your templates by using the `` and `` tags. The condition must be a valid PHP expression that will be evaluated and if it returns `true`, the content between the tags will be included in the final output.

You can also use the `` tag to provide an alternative content if the condition is not met.

To use a variable provided in the arguments array in a condition, you must use the `$` sign before the variable name, like this: ``. The `$` sign is used to differentiate between the template variable and a keyword such as `true` or `null`.

##### Example:

[](#example)

```
some:
  key

  with value

  without value

```

If you provide an argument `['variable' => 'value']`, the final output will be this:

```
some:
  key
  with value
```

And if you provide an argument `['variable' => 'other value']`, the final output will be this:

```
some:
  key
  without value
```

#### Variables

[](#variables)

Variables are wrapped in `` with optional space on either side (both `` and `` are valid) and the name must be an alphanumeric string with optional underscore/s (this regex `[a-zA-Z0-9_]+?`).

##### Default values

[](#default-values)

Variables can optionally have a default value that will be used if no argument is provided for that variable during parsing. You can specify a default value like this: ``

If you're going to use filters, the default value comes before the filter, ie.: ``

If default value is empty (ie. ``), it will be treated as null.

#### Filters

[](#filters)

You can optionally provide filter to a variable by placing the pipe symbol `|` right after the variable name and the filter right after that (no space around the `|` pipe symbol), like this: ``.

The filter can be any PHP function with the variable used as the function's argument.

##### Example:

[](#example-1)

> If we have `` in the template and we provide an argument `['foo' => 'hello world']`, the final (parsed) output will be this: `HELLO WORLD`.

Filters can also have additional arguments apart from the variable itself. To pass additional arguments to a filter, write it like this: ``. Each argument after the colon is separated by a comma and can have any scalar type as a value.

The first argument will always the variable on which we're declaring the filter, with any other arguments passed after that.

##### Example:

[](#example-2)

> If we have `` and provide an argument `['foo' => 'abcdef']`, the filter will get called like this using the arguments provided: `substr('abcdef', 1, 3)`. And the final parsed output will thus be this: `bcd`.

*So far you can specify only one filter per variable declaration, but that will probably change in the future.*

### Built-in filters

[](#built-in-filters)

There are a few built-in filters that you can use:

`upper` - Converts the variable to uppercase

`lower` - Converts the variable to lowercase

`upperFirst` - Converts the first character of the variable to uppercase

`lowerFirst` - Converts the first character of the variable to lowercase

`first` - Returns the first character of the variable

`last` - Returns the last character of the variable

`camelCase` - Converts the variable to camelCase

`snakeCase` - Converts the variable to snake\_case

`kebabCase` - Converts the variable to kebab-case

`pascalCase` - Converts the variable to PascalCase

`titleCase` - Converts the variable to Title Case

`length` - Returns the length of the variable

`reverse` - Reverses the variable

`random` - Returns a random character from the variable

`truncate` - Truncates the variable to the specified length

### Use in code

[](#use-in-code)

#### Parse string or file

[](#parse-string-or-file)

There are two main functions that will be of most interest to you: `parseString` and `parse`. Both are static functions and are used like this:

```
use Matronator\Parsem\Parser;

// parseString()
echo Parser::parseString('some .', ['text' => 'value']);
// Output: some value.

// parse()
$arguments = [
    'variableName' => 'value',
    'key' => 'other value',
];
$parsedFile = Parser::parse('filename.yaml', $arguments);
echo $parsedFile;
// Output: Will print the parsed contents of the file as string.
```

#### Methods

[](#methods)

##### `Parser::parseString`

[](#parserparsestring)

```
/**
 * Parses a string, replacing all template variables with the corresponding values passed in `$arguments`.
 * @return mixed The parsed string or the original `$string` value if it's not string
 * @param mixed $string String to parse. If not provided with a string, the function will return this value
 * @param array $arguments Array of arguments to find and replace while parsing `['key' => 'value']`
 * @param bool $strict [optional] If set to `true`, the function will throw an exception if a variable is not found in the `$arguments` array. If set to `false` null will be used.
 * @param string|null $pattern [optional] You can provide custom regex with two matching groups (for the variable name and for the filter) to use custom template syntax instead of the default one ``
 * @throws RuntimeException If a variable is not found in the `$arguments` array and `$strict` is set to `true`
 */
Parser::parseString(mixed $string, array $arguments = [], bool $strict = true, ?string $pattern = null): mixed
```

##### `Parser::parse`

[](#parserparse)

```
/**
 * @param string $filename Path to the file to parse
 * @see Parser::parseString() for rest of the parameter descriptions
 */
Parser::parse(string $filename, array $arguments = [], bool $strict = true, ?string $pattern = null): string
```

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance76

Regular maintenance activity

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 82.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~29 days

Recently: every ~40 days

Total

42

Last Release

133d ago

Major Versions

v1.0.7 → v2.0.02022-10-12

v1.x-dev → v2.0.22023-05-09

v2.0.12 → v3.0.02023-05-12

PHP version history (2 changes)1.0.0PHP &gt;=7.4

v2.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/c9d3a221275cd0bd50dbe32abc3b193851cc693bc9893c249c3e374f7c2dc439?d=identicon)[matronator](/maintainers/matronator)

---

Top Contributors

[![matronator](https://avatars.githubusercontent.com/u/5470780?v=4)](https://github.com/matronator "matronator (95 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (18 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

mtrgenparseparserparsing-engineparsing-libraryphptemplate-enginetemplate-languagetemplate-parsertemplatingtemplating-enginejsonparseryamlneontemplatestemplate enginetemplate parser

### Embed Badge

![Health badge](/badges/matronator-parsem/health.svg)

```
[![Health](https://phpackages.com/badges/matronator-parsem/health.svg)](https://phpackages.com/packages/matronator-parsem)
```

###  Alternatives

[webuni/front-matter

Front matter parser and dumper for PHP

41264.9k11](/packages/webuni-front-matter)[picocms/pico

Pico is a flat file CMS, this means there is no administration backend and database to deal with. You simply create .md files in the "content" folder and that becomes a page.

3.9k37.8k11](/packages/picocms-pico)[foil/foil

PHP template engine for native PHP templates

170111.2k7](/packages/foil-foil)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
