PHPackages                             dalpras/smart-template - 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. dalpras/smart-template

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

dalpras/smart-template
======================

Smart template engine for key-value substitutions

v3.6(1mo ago)24proprietaryPHPPHP &gt;=8.3

Since Oct 9Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/dalpras/smart-template)[ Packagist](https://packagist.org/packages/dalpras/smart-template)[ RSS](/packages/dalpras-smart-template/feed)WikiDiscussions main Synced 1mo ago

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

Smart Template
==============

[](#smart-template)

A small PHP template engine for structured rendering with native PHP arrays, lazy compilation, and named placeholder substitution.

`smart-template` is designed for projects that want to keep rendering logic in PHP without introducing a separate template language. Templates are regular PHP files that return arrays of strings and callbacks. When accessed, string leaves are lazily compiled into render closures and nested arrays are wrapped into `RenderCollection` objects.

What it is good at
------------------

[](#what-it-is-good-at)

- Small, reusable HTML fragments and components
- PHP-native template composition
- Dynamic rendering with named placeholders
- Rendering from filesystem templates or in-memory templates
- Fine-grained control over escaping and HTML attributes

What it is not
--------------

[](#what-it-is-not)

This package is **not** a full view framework. It does not try to replace systems built around inheritance, blocks, macros, or expression languages. It works best when you want explicit PHP control over markup and composition.

---

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

[](#installation)

```
composer require dalpras/smart-template
```

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

[](#requirements)

- PHP 8.3 or newer

---

Mental model
------------

[](#mental-model)

A template file returns an array.

Each array entry can be:

- a **string** with placeholders such as `{title}` or `{rows}`
- a **closure**
- a **nested array** of more templates

When a string leaf is accessed, the engine compiles it lazily into a closure. When you call that closure with an associative array, placeholders are replaced by key.

Example:

```
$template['card']([
    '{title}' => 'Hello',
    '{body}'  => 'Welcome',
]);
```

If a placeholder value is itself a closure, the engine resolves it before substitution.

---

Quick start
===========

[](#quick-start)

1) Create a template file
-------------------------

[](#1-create-a-template-file)

`templates/card.php`

```
