定制 bynhan/sharp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bynhan/sharp

Composer 安装命令:

composer require bynhan/sharp

包简介

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

README 文档

README

Sharp

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

Latest Version PHP 8.1+ MIT License Tests

Documentation · Changelog · Contributing · MIT License · Sponsors

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

composer require bynhan/sharp

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')
  <h1>Hello, {{ $user->name }}!</h1>

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

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

Features at a glance

Feature Description
{{ $expr }} HTML-escaped output
{!! $expr !!} Raw unescaped output
<!-- comment --> 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)
<ComponentTag :prop="$val" /> File-backed or class-backed components
#props([name: type, name?: type]) Typed prop declarations with runtime validation and PHPDoc generation
Named slots <slot name="header">…</slot>
$sharp->directive('name', fn) Compile-time custom directives
$sharp->namespace('ns', $path) Namespace loader ('ns::view')
Dependency graph Auto-recompile when any dependency changes
Sandbox mode Block dangerous PHP functions at compile time

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:

<!-- rendered output -->
<div data-sharp-src="/abs/path/templates/home.sp:8" class="user-card">

Sharp also writes .sharp/ast/<hash>.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

Guide Description
Getting Started Installation, config, first render
Template Syntax Echo, comments, #if, #foreach, $loop, #while, #for, #switch, includes, #php, #dump
Layouts #extends, #section, #yield, #parent, #push, #prepend, #stack
Components Props, slots, class-backed components
Custom Directives Compile-time #directives
Loaders File, namespace, memory, custom loaders
Cache & Dependency Graph How caching and invalidation work
Security Sandbox mode and trust boundaries
API Reference Full method and exception reference
Architecture Internals for contributors
Changelog Version history

Contributing

Contributions are welcome. Please read 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

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

No sponsors yet — become the first!

License

Sharp is open-sourced software licensed under the MIT license.

bynhan/sharp 适用场景与选型建议

bynhan/sharp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「template」 「view」 「engine」 「ast」 「compiler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bynhan/sharp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bynhan/sharp 我们能提供哪些服务?
定制开发 / 二次开发

基于 bynhan/sharp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-28