elliephp/zap
最新稳定版本:1.0.0
Composer 安装命令:
composer require elliephp/zap
包简介
A minimal, fast routing component for ElliePHP API framework based on FastRoute and PSR-7/PSR-15 standards
README 文档
README
A compiler-based templating engine for microframeworks.
- Fast: templates compile to cached PHP (no runtime parsing; OPcache-friendly).
- Safe by default:
{ $expr }is escaped, raw output is explicit via{!! $expr !!}. - Blade-like ergonomics:
@if,@foreach, file-based components, layouts withchildren.
Install
composer require elliephp/zap
Quickstart
Folder layout
your-app/
views/
home.view
components/
Layout.php
Layout.view
PostCard.php
PostCard.view
cache/
Bootstrap
use ElliePHP\Components\View\Engine; $engine = Engine::bootstrap([ 'views_path' => __DIR__ . '/views', 'components_path' => __DIR__ . '/components', 'cache_path' => __DIR__ . '/cache', 'debug' => true, 'escaper' => static fn($v) => htmlspecialchars((string)$v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), ]);
Render a view
views/home.view:
<Layout title="Home"> <h1>Hello { $user->name }</h1> @if ($user) <PostCard post={$post} /> @else <GuestBanner /> @endif @foreach ($posts as $post) <PostCard post={$post} /> @endforeach </Layout>
$html = $engine->render('home', [ 'user' => $user, 'posts' => $posts, 'post' => $post, ]);
Template cheatsheet
Output (escaped vs raw)
{ $expr }escapes via your configuredescaper{!! $expr !!}outputs raw HTML
Control flow
@if (...)/@elseif (...)/@else/@endif@foreach (...)/@endforeach@break,@continue
Components
- Lowercase tags are HTML (
<div>,<a>,<img />) - PascalCase tags are components (
<Layout>,<PostCard />) - Props:
post={$post}(expression),title="Home"(string),disabled(boolean)
Common patterns
Layouts + children
Children are passed as a prop named children (already-rendered HTML).
components/Layout.view:
<div class="layout"> <header>{ $title }</header> <main>{!! $children !!}</main> </div>
Component class + template
components/PostCard.php:
use ElliePHP\Components\View\Component\Component; final class PostCard extends Component { public function __construct(public mixed $post) {} }
components/PostCard.view:
<article> <h2>{ $post->title }</h2> </article>
Gotchas (read this once)
- Void HTML tags: write them as self-closing
/>(example:<img ... />) - JS/CSS with
{}: inline JS/CSS containing lots of{}can be mis-tokenized; prefer external assets for complex scripts/styles - Attribute expressions: don’t write
src="{ $url }"(that’s a literal string). Usesrc={$url}.
Examples
- CLI example:
php examples/basic/run.php
- Browser example:
php -S 127.0.0.1:8000 -t examples/browser/public
Nerd docs
If you want the compiler pipeline, AST nodes, caching contracts, source maps, and extension points, see docs/TECHNICAL.md.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-20