PHPackages                             lorenzomilesi/templates - 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. lorenzomilesi/templates

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

lorenzomilesi/templates
=======================

A simple template manager to replace placeholders inside strings

v0.2.0(4y ago)02MITPHPPHP &gt;=8.0

Since Oct 30Pushed 4y ago1 watchersCompare

[ Source](https://github.com/lorenzo-milesi/templates)[ Packagist](https://packagist.org/packages/lorenzomilesi/templates)[ RSS](/packages/lorenzomilesi-templates/feed)WikiDiscussions develop Synced 4d ago

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

Templates
=========

[](#templates)

A simple template manager to replace placeholders inside strings.

Usage
-----

[](#usage)

### Creating a Placeholder

[](#creating-a-placeholder)

First, you may need to create a Placeholder, it gives you a `$tag` property and a `value()` method to set up.

The `$tag` is the placeholder that will be replaced by the result of the `value()` method.

```
// MyCustomPlaceholder.php

use LorenzoMilesi\Templates\Placeholder;

class MyCustomPlaceholder extends Placeholder
{
    protected static string $tag = '[custom]';

    public static function value() : string
    {
        return 'yey';
    }
}
```

### Using Placeholders in Templates

[](#using-placeholders-in-templates)

Then, anywhere should you need it, you may create a Template instance that will be responsible for transforming a given content by replacing every defined placeholders by their values.

```
use LorenzoMilesi\Templates\Template;

// Load the template with given content and placeholders to replace
$template = Template::load('placeholders will be replaced, [custom]')
        ->addPlaceholder(MyCustomPlacehoder::up());

// This will return : "placeholders will be replaced, yey"
$template->__toString();
```

### PlaceholderFactory

[](#placeholderfactory)

Sometimes you do not want to create a custom placeholder in a class file, consequently, we created a PlaceholderFactory to setup anonymous placeholder classes on the fly:

```
use LorenzoMilesi\Templates\Template;
use LorenzoMilesi\Templates\PlaceholderFactory;

$datePlaceholder = PlaceholderFactory::build('[date]', \Carbon\Carbon::today()->format('Y-m-d'));

Template::load('Today date is [date]')
    ->addPlaceholder($datePlaceholder);
```

So far, so good, you may add several placeholders and build successful template's replacement systems:

```
use LorenzoMilesi\Templates\Template;
use LorenzoMilesi\Templates\PlaceholderFactory;

$datePlaceholder = PlaceholderFactory::build('[date]', \Carbon\Carbon::today()->format('Y-m-d'));

$userPlaceholder = PlaceholderFactory::build('[username]', auth()->user()->getName());

$teamPlaceholder = PlaceholderFactory::build('[teamname]', auth()->user()->team()->getName);

Template::load('Hello [username], we\'re on [date] and you joined [teamname] :)')
    ->addPlaceholder($datePlaceholder)
    ->addPlaceholder($userPlaceholder)
    ->addPlaceholder($teamPlaceholder);
```

### Some other things

[](#some-other-things)

You're free to define any format for placeholders:

```
use LorenzoMilesi\Templates\Template;
use LorenzoMilesi\Templates\PlaceholderFactory;

$datePlaceholder = PlaceholderFactory::build('{date}', \Carbon\Carbon::today()->format('Y-m-d'));

Template::load('this will replace {date} but not [date]')
    ->addPlaceholder($datePlaceholder);
```

When you define placeholders on a Template, order matters:

```
use LorenzoMilesi\Templates\Template;
use LorenzoMilesi\Templates\PlaceholderFactory;

$dateOne = PlaceholderFactory::build('{date}', \Carbon\Carbon::today()->format('Y-m-d'));

$dateTwo = PlaceholderFactory::build('{date}', \Carbon\Carbon::yesterday()->format('Y-m-d'));

/**
 * Given that $dateOne and $dateTwo replace the same tag,
 * Only $dateOne will work here, since it will replace
 * the tag before $dayTwo.
 */
Template::load('This is {date}')
    ->addPlaceholder($dateOne)
    ->addPlaceholder($dateTwo);
```

Lastly you may create placeholder switches:

```
use LorenzoMilesi\Templates\Template;
use LorenzoMilesi\Templates\PlaceholderFactory;

$condition = rand(0, 1);

$conditionalTag = PlaceholderFactory::build('[condition]', $condition ? '[today]' : '[yesterday]');

$todayPlaceholder = PlaceholderFactory::build('[today]', \Carbon\Carbon::today()->format('Y-m-d'));

$yesterdayPlaceholder = PlaceholderFactory::build('[yesterday]', \Carbon\Carbon::yesterday()->format('Y-m-d'));

/**
 * This will use today or yesterday tag given a condition
 */
Template::load('This is [condition]')
    ->addPlaceholder($conditionalTag)
    ->addPlaceholder($todayPlaceholder)
    ->addPlaceholder($yesterdayPlaceholder);
```

Licence
-------

[](#licence)

This package is delivered to you for free under the MIT Licence.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

1657d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f6ed273e080825bb5da0acb1e29f109bba07fe832534c6156f50f8418d70875?d=identicon)[LorenzoMilesi](/maintainers/LorenzoMilesi)

---

Top Contributors

[![lorenzo-milesi](https://avatars.githubusercontent.com/u/47899622?v=4)](https://github.com/lorenzo-milesi "lorenzo-milesi (4 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lorenzomilesi-templates/health.svg)

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M2.6k](/packages/craftcms-cms)[tightenco/jigsaw

Simple static sites with Laravel's Blade.

2.2k438.5k29](/packages/tightenco-jigsaw)[illuminate/view

The Illuminate View package.

13144.9M1.7k](/packages/illuminate-view)[rareloop/lumberjack-core

A powerful MVC framework for the modern WordPress developer. Write better, more expressive and easier to maintain code

42155.0k19](/packages/rareloop-lumberjack-core)[alajusticia/laravel-logins

Session management in Laravel apps, user notifications on new access, support for multiple separate remember tokens, IP geolocation, User-Agent parser

2011.0k](/packages/alajusticia-laravel-logins)

PHPackages © 2026

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