PHPackages                             bynhan/sharp - 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. bynhan/sharp

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

bynhan/sharp
============

A fast AST-based PHP view engine with #-directives, compile-time caching, layout inheritance, and component support.

v0.0.4(1mo ago)04↑2900%MITPHPPHP ^8.1

Since Mar 28Pushed 1mo agoCompare

[ Source](https://github.com/tuinhanne/sharp-view-engine)[ Packagist](https://packagist.org/packages/bynhan/sharp)[ Docs](https://github.com/bynhan/sharp)[ RSS](/packages/bynhan-sharp/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

Sharp
=====

[](#sharp)

A fast, AST-based PHP view engine with `#`-directive syntax

 [![Latest Version](https://camo.githubusercontent.com/09fa0a894ae7440b1a78b60cf2164ac44ca25e204b4b9a87f698b6eba639b76a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62796e68616e2f7368617270)](https://packagist.org/packages/bynhan/sharp) [![PHP 8.1+](https://camo.githubusercontent.com/83dd395020c37276225039739320f6c8e7e99963ab21ee3d09282cb48dad2a60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c7565)](https://packagist.org/packages/bynhan/sharp) [![MIT License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE) [![Tests](https://camo.githubusercontent.com/33d331233b0503374dce34031b69ead51967d82a3f44f0447e8628c7e5cbc798/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d31303925323070617373696e672d627269676874677265656e)](https://github.com/bynhan/sharp/actions)

 [Documentation](docs/getting-started.md) · [Changelog](docs/changelog.md) · [Contributing](CONTRIBUTING.md) · [MIT License](LICENSE) · [Sponsors](#sponsors)

---

Why Sharp?
----------

[](#why-sharp)

- **`#directive` syntax** — clean, readable templates without a framework dependency
- **Full AST pipeline** — Lexer → Parser → Validator → Optimizer → Codegen → FileCache; no regex string replacement
- **Zero runtime overhead for directives** — compiled and inlined at build time
- **Plain PHP expressions** — no dot-notation translator, no magic; `{{ $user->name }}` is exactly what it says
- **Standalone** — PHP 8.1+, zero runtime dependencies, drop into any project

---

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

[](#installation)

```
composer require bynhan/sharp
```

---

Quick start
-----------

[](#quick-start)

```
use Sharp\Sharp;

$sharp = new Sharp();                   // reads/creates sharp.config.json at getcwd()
echo $sharp->render('home', ['user' => $user]);
```

**`templates/home.sp`**

```
#extends('layouts.main')

#section('content')
  Hello, {{ $user->name }}!

  #foreach($user->posts as $post)
    {{ $loop->iteration }}. {{ $post->title }}
  #endforeach
#endsection
```

Sharp auto-creates `sharp.config.json` on first run. No manual setup required.

---

Features at a glance
--------------------

[](#features-at-a-glance)

FeatureDescription`{{ $expr }}`HTML-escaped output`{!! $expr !!}`Raw unescaped output``Stripped at compile time`#if` / `#elseif` / `#else` / `#endif`Conditionals`#foreach` / `#endforeach`Loops with `$loop` variable (10 properties)`#while` / `#endwhile`While loops`#for` / `#endfor`C-style for loops`#switch` / `#case` / `#default` / `#endswitch`Switch statements`#extends` / `#section` / `#yield` / `#parent`Layout inheritance`#push` / `#prepend` / `#stack`Accumulated content stacks (CSS/JS injection)`#include('view', $data)`Partial includes with optional extra data`#includeIf`Include only if the view file exists`#includeWhen` / `#includeUnless`Conditionally include a partial`#includeFirst`Include the first resolvable view from a list`#set($var = expr)`Assign a variable inline`#break` / `#continue`Loop and switch control`#php` / `#endphp`Raw PHP blocks (sandbox-blocked)`#dump` / `#dd`Debug helpers (sandbox-blocked)``File-backed or class-backed components`#props([name: type, name?: type])`Typed prop declarations with runtime validation and PHPDoc generationNamed slots`…``$sharp->directive('name', fn)`Compile-time custom directives`$sharp->namespace('ns', $path)`Namespace loader (`'ns::view'`)Dependency graphAuto-recompile when any dependency changesSandbox modeBlock dangerous PHP functions at compile time---

Dev Mode &amp; Debugging
------------------------

[](#dev-mode--debugging)

Sharp includes a dev mode that injects source annotations into rendered HTML, enabling Chrome DevTools to trace any element back to its `.sp` template source.

**Enable via `sharp.config.json`** (recommended for local development):

```
{
  "viewPath": "templates/",
  "sandbox": true,
  "devMode": true
}
```

**Enable via code** (takes priority over config):

```
$sharp = new Sharp();
$sharp->setProduction(false); // dev mode — injects annotations (default)
$sharp->setProduction(true);  // production mode — no annotations
```

When dev mode is on, every HTML element in the rendered output will carry a `data-sharp-src` attribute:

```

```

Sharp also writes `.sharp/ast/.ast` source-map files that the VSCode extension uses for Xdebug breakpoint translation.

**Jump to template source from Chrome:**

Add this bookmarklet to your browser to jump from a hovered element directly to its `.sp` source in VSCode:

```
javascript:(function(){
  var el = document.querySelector(':hover');
  while(el && !el.dataset.sharpSrc) el = el.parentElement;
  if(el) location.href = 'vscode://file/' + el.dataset.sharpSrc;
})();
```

> Always set `"devMode": false` or call `$sharp->setProduction(true)` before deploying — dev mode exposes template file paths in public HTML.

---

Documentation
-------------

[](#documentation)

GuideDescription[Getting Started](docs/getting-started.md)Installation, config, first render[Template Syntax](docs/syntax.md)Echo, comments, `#if`, `#foreach`, `$loop`, `#while`, `#for`, `#switch`, includes, `#php`, `#dump`[Layouts](docs/layouts.md)`#extends`, `#section`, `#yield`, `#parent`, `#push`, `#prepend`, `#stack`[Components](docs/components.md)Props, slots, class-backed components[Custom Directives](docs/directives.md)Compile-time `#directives`[Loaders](docs/loaders.md)File, namespace, memory, custom loaders[Cache &amp; Dependency Graph](docs/cache.md)How caching and invalidation work[Security](docs/security.md)Sandbox mode and trust boundaries[API Reference](docs/api.md)Full method and exception reference[Architecture](docs/architecture.md)Internals for contributors[Changelog](docs/changelog.md)Version history---

Contributing
------------

[](#contributing)

Contributions are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions, coding standards, and the PR checklist.

```
git clone https://github.com/bynhan/sharp.git && cd sharp
composer install
./vendor/bin/phpunit --testdox
```

---

Sponsors
--------

[](#sponsors)

Sharp is free, open-source software. If it saves you time, consider sponsoring its development.

*No sponsors yet — [become the first!](https://github.com/sponsors/bynhan)*

---

License
-------

[](#license)

Sharp is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance91

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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

4

Last Release

46d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/927877cd465dba79544fe63d02aa12180ed9d778ff4b4c340121ed1c0a32e368?d=identicon)[tuinhanne](/maintainers/tuinhanne)

---

Top Contributors

[![tuinhanne](https://avatars.githubusercontent.com/u/66679347?v=4)](https://github.com/tuinhanne "tuinhanne (9 commits)")

---

Tags

phptemplateviewcompilerastengineblade-like

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bynhan-sharp/health.svg)

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

###  Alternatives

[phug/phug

Pug (ex-Jade) facade engine for PHP, HTML template engine structured by indentation

67292.2k13](/packages/phug-phug)

PHPackages © 2026

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