anunes/an-template
Composer 安装命令:
composer require anunes/an-template
包简介
A lightweight, modern PHP template engine with intuitive syntax supporting directives, inheritance, sections, and components
README 文档
README
A lightweight, modern PHP template engine with intuitive syntax supporting directives, inheritance, sections, and components.
Features
- Blade-like syntax - Familiar directives and templating
- Template inheritance - Extend layouts and yield sections
- Component system - Reusable UI components with slots
- Caching - Compiled template caching for performance
- Error handling - Detailed error reporting with context
- PSR-4 autoloading - Modern PHP standards compliance
- No dependencies - Pure PHP implementation
Installation
Install via Composer:
composer require anunes/an-template
Quick Start
Basic Setup
use AnTemplate\AnTemplate; // Initialize the template engine $templates = new AnTemplate( templatesPath: __DIR__ . '/templates', cachePath: __DIR__ . '/cache', cacheEnabled: true ); // Render a template echo $templates->render('welcome', ['name' => 'World']);
Using Helper Functions
// Set up global instance for helper functions viewer_setup(__DIR__ . '/templates', __DIR__ . '/cache'); // Use Laravel-style view helper view('welcome', ['name' => 'World']); // Outputs directly // Or get content as string $content = viewer_content('welcome', ['name' => 'World']);
Template Syntax
Variables
{{-- Escaped output --}}
{{ $name }}
{{ $user->email ?? 'No email' }}
{{-- Raw output --}}
{!! $htmlContent !!}
{{-- Escaped literals --}}
@{{ This will output: {{ $literal }} }}
Conditionals
@if($user->isActive()) <p>User is active</p> @elseif($user->isPending()) <p>User is pending</p> @else <p>User is inactive</p> @endif @unless($user->isBanned()) <p>Welcome back!</p> @endunless @isset($user->profile) <p>Profile: {{ $user->profile->name }}</p> @endisset @empty($posts) <p>No posts found</p> @endempty
Loops
@foreach($users as $user) <p>{{ $user->name }}</p> @endforeach @forelse($posts as $post) <article>{{ $post->title }}</article> @empty <p>No posts available</p> @endforelse @for($i = 0; $i < 10; $i++) <p>Item {{ $i }}</p> @endfor @while($condition) <p>Processing...</p> @endwhile
Template Inheritance
Layout (layouts/app.view.php):
<!DOCTYPE html> <html> <head> <title>@yield('title', 'Default Title')</title> @stack('styles') </head> <body> <header> @yield('header') </header> <main> @yield('content') </main> @stack('scripts') </body> </html>
Page template:
@extends('layouts.app') @section('title', 'Welcome Page') @section('content') <h1>Welcome to our site!</h1> <p>Hello {{ $name }}!</p> @endsection @push('styles') <link rel="stylesheet" href="/css/welcome.css"> @endpush
Components
Component (components/alert.view.php):
<div class="alert alert-{{ $type ?? 'info' }}">
@if(isset($title))
<h4>{{ $title }}</h4>
@endif
<div class="alert-body">
{{ $slot }}
</div>
</div>
Using components:
<x-alert type="success" title="Success!"> Your profile has been updated. </x-alert> {{-- Self-closing --}} <x-alert type="info" /> {{-- With named slots --}} <x-card> @slot('header') <h3>Card Title</h3> @endslot This is the main card content. @endcard
Includes
@include('partials.header') @include('partials.nav', ['active' => 'home'])
Raw PHP
@php
$processed = array_map('strtoupper', $items);
$total = count($processed);
@endphp
<p>Processing {{ $total }} items</p>
Stacks
@push('scripts') <script src="/js/analytics.js"></script> @endpush @push('scripts') <script src="/js/app.js"></script> @endpush {{-- In layout --}} @stack('scripts')
Directory Structure
your-project/
├── templates/
│ ├── layouts/
│ │ └── app.view.php
│ ├── components/
│ │ ├── alert.view.php
│ │ └── button.view.php
│ ├── partials/
│ │ ├── header.view.php
│ │ └── footer.view.php
│ └── pages/
│ ├── home.view.php
│ └── about.view.php
└── cache/ (auto-created)
Advanced Usage
Custom Global Data
// Add data available to all templates $templates->share('app_name', 'My Application'); $templates->share('current_user', $user); // Or add multiple at once $templates->shareMany([ 'app_name' => 'My Application', 'version' => '1.0.0' ]);
Cache Management
// Clear all cached templates $templates->clearCache(); // Get cache statistics $stats = $templates->getCacheStats(); // Returns: ['enabled' => true, 'files' => 15, 'size' => 45231, 'size_formatted' => '44.17 KB']
Debug Mode
// Enable debug mode for verbose error reporting $templates->setDebugMode(true); // Get compilation information $debug = $templates->getDebugInfo('welcome'); // Get compilation log $log = $templates->getCompilationLog();
Template Validation
// Validate template syntax $result = $templates->validateTemplate('welcome'); if (!$result['valid']) { foreach ($result['errors'] as $error) { echo "Error: {$error}\n"; } }
Error Handling
AnTemplate provides detailed error reporting:
use AnTemplate\AnTemplateException; try { echo $templates->render('nonexistent'); } catch (AnTemplateException $e) { echo "Template Error: " . $e->getDetailedMessage(); echo "Template: " . $e->getTemplateName(); echo "File: " . $e->getTemplateFile(); echo "Line: " . $e->getTemplateLine(); }
Performance
- Template compilation caching - Templates are compiled once and cached
- Optimized parsing - Efficient regex-based compilation
- Minimal overhead - No external dependencies
- Memory efficient - Smart buffering and cleanup
Requirements
- PHP 8.0 or higher
- No external dependencies
License
MIT License. See LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
anunes/an-template 适用场景与选型建议
anunes/an-template 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「templating」 「php」 「template」 「components」 「inheritance」 「template-engine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anunes/an-template 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anunes/an-template 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anunes/an-template 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
Simple ASCII output of array data
E2E Studios PHP Framework adaptation of leafsphp/blade package
Melis Platform Templating Plugin Creator
Serpent is a lightweight and compiling templating engine for PHP. It was designed to seamlessly integrate into existing MVC frameworks. It uses PHP itself as its template language, so you do not need to learn a new markup language. On the other side you get many improvements compared to pure PHP.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-27