Nest is a fast, extensible PHP templating engine that compiles your templates down to native PHP code for maximum performance. It supports:
- Variables:
{$var} - Filters:
{$var|trim|upper} - Raw expressions:
{{ translate('key') }} - Comments:
{# this is a comment #} - Namespaced components/tags:
<nest:avatar user="$user" />,<nest:if test="$cond">…</nest:if> - Custom filters, functions, and components
- File-based caching of compiled templates
Require via Composer:
composer require chrisdeeming/nestThen in your project:
require 'vendor/autoload.php';
use Nest\Nest;
$engine = new Nest(
loader: null, // defaults to ./nest_templates/*.nest.php
cache: null, // defaults to ./nest_cache/
debug: false // disable cache in debug mode
);$html = $engine->renderString(
'<h1>Hello, {$name|upper}!</h1>',
['name' => 'Nest']
);
echo $html; // <h1>Hello, NEST!</h1>Assuming you have templates/email.nest.php:
{# email.nest.php #}
<div>
Hello, {$user.name}!
<p>Your balance is {$user.balance|trim}</p>
</div>Render it:
echo $engine->renderFile('email', [
'user' => ['name'=>'Alice','balance'=>' 123.45 ']
]);
// Outputs:
// <div>
// Hello, Alice!
// <p>Your balance is 123.45</p>
// </div>nest/
├── src/Nest/ # PHP source (PSR-4 namespace `Nest\`)
│ ├── Cache/ # CacheInterface, FileCache
│ ├── Compiler/ # Lexer, Parser, Compiler, AST nodes
│ ├── Component/ # ComponentInterface & built-in components
│ ├── Exception/ # TemplateNotFoundException, etc.
│ ├── Loader/ # LoaderInterface, FileLoader
│ ├── Template/ # StringTemplate, CompiledTemplateBase
│ └── Nest.php # Core Nest class
├── tests/ # Unit & integration tests
├── composer.json
└── README.md
- Core Engine + Loader + Cache + string-based rendering
- Full Lexer & AST parser
- PHP code generator / Compiler
- Namespaced
<nest:…>component system - Default components:
if,elseif,else,foreach,avatar - Filters, functions, and custom extensions
- Security & sandboxing for raw expressions
- Stack support
- Template inheritance
- Includes / partials
- Layout components
- component slots
- unless directive
- isset / empty directive
- switch directive
- forelse or support else on foreach
- for / while
- introduce loop variable (with parent support)
- continue / break for loops
- Raw PHP support
- conditional class/style attributes
- checked/selected/disabled/readonly/required
- access component methods
- Fork the repo
- Create a feature branch (
git checkout -b feature/xyz) - Write tests & code
- Submit a pull request
Please include unit tests for new features.
Nest is open-source under the MIT License.