Skip to content

A lightweight PHP template engine crafted for clarity, speed, and extendability.

License

Notifications You must be signed in to change notification settings

chrisdeeming/nest

Repository files navigation

Nest Template Engine

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

Installation

Require via Composer:

composer require chrisdeeming/nest

Then 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
);

Quick Start

1. Render a string

$html = $engine->renderString(
    '<h1>Hello, {$name|upper}!</h1>',
    ['name' => 'Nest']
);

echo $html; // <h1>Hello, NEST!</h1>

2. Render a file

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>

Project Structure

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

Roadmap

  • 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

Contributing

  1. Fork the repo
  2. Create a feature branch (git checkout -b feature/xyz)
  3. Write tests & code
  4. Submit a pull request

Please include unit tests for new features.


License

Nest is open-source under the MIT License.

About

A lightweight PHP template engine crafted for clarity, speed, and extendability.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published