lighthouse/view 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lighthouse/view

最新稳定版本:v0.1.0

Composer 安装命令:

composer require lighthouse/view

包简介

PHP Template Engine for the Lighthouse framework

README 文档

README

A simple, powerful PHP template engine for the Lighthouse framework.

Installation

composer require lighthouse/view

Requirements

  • PHP 8.2 or higher

Features

  • PHP-based templates (no new syntax to learn)
  • Layouts with sections
  • Partials/includes
  • Automatic HTML escaping
  • Global shared data
  • Dot notation for view paths

Quick Start

Basic Rendering

use Lighthouse\View\View;

$view = new View('/path/to/views');

// Render a view
echo $view->render('welcome', ['name' => 'John']);

views/welcome.php:

<h1>Hello, <?= $view->e($name) ?>!</h1>

Escaping

Always escape user data to prevent XSS attacks:

// In templates, use $view->e() or $view->escape()
<p><?= $view->e($userInput) ?></p>

// For JSON (e.g., in JavaScript)
<script>
    var data = <?= $view->json($data) ?>;
</script>

Layouts

Create a layout template:

views/layouts/main.php:

<!DOCTYPE html>
<html>
<head>
    <title><?= $view->yield('title', 'My App') ?></title>
</head>
<body>
    <header>
        <?= $view->yield('header') ?>
    </header>
    
    <main>
        <?= $view->yield('content') ?>
    </main>
    
    <footer>© 2025</footer>
</body>
</html>

Extend the layout in your view:

views/users/index.php:

<?php $view->extends('layouts.main'); ?>

<?php $view->section('title'); ?>
Users List
<?php $view->endSection(); ?>

<?php $view->section('content'); ?>
<h1>Users</h1>
<ul>
    <?php foreach ($users as $user): ?>
        <li><?= $view->e($user['name']) ?></li>
    <?php endforeach; ?>
</ul>
<?php $view->endSection(); ?>

Partials

Include reusable components:

views/partials/alert.php:

<div class="alert alert-<?= $view->e($type) ?>">
    <?= $view->e($message) ?>
</div>

Usage:

<?php $view->include('partials.alert', ['type' => 'success', 'message' => 'Saved!']); ?>

Or capture the output:

$html = $view->partial('partials.alert', ['type' => 'error', 'message' => 'Failed']);

Shared Data

Share data across all views:

// Share single value
$view->share('appName', 'My Application');

// Share multiple values
$view->share([
    'appName' => 'My Application',
    'version' => '1.0.0',
    'user' => $currentUser,
]);

Access in any template:

<title><?= $view->e($appName) ?></title>

Dot Notation

View paths use dot notation:

$view->render('users.index');      // views/users/index.php
$view->render('admin.users.edit'); // views/admin/users/edit.php
$view->partial('partials.nav');    // views/partials/nav.php

Check View Exists

if ($view->exists('emails.welcome')) {
    echo $view->render('emails.welcome', $data);
}

API Reference

View

Method Description
render(string $view, array $data) Render a view template
partial(string $partial, array $data) Render and return a partial
include(string $partial, array $data) Render and echo a partial
extends(string $layout) Set layout for current view
section(string $name) Start capturing a section
endSection() End section capture
yield(string $name, string $default) Output section content
hasSection(string $name) Check if section exists
share(string|array $key, mixed $value) Share data globally
getShared(?string $key) Get shared data
escape(string $value) Escape HTML entities
e(string $value) Alias for escape()
json(mixed $value) Safe JSON encoding
exists(string $view) Check if view file exists
getBasePath() Get views base path
setBasePath(string $path) Set views base path

Template Variables

Inside templates, these variables are available:

  • $view - The View instance (for helpers)
  • Any data passed to render() or partial()
  • Any globally shared data

Testing

composer test

License

MIT License. See LICENSE for details.

Part of the Lighthouse Framework

This package is part of the Lighthouse Framework, an educational PHP framework designed to teach how modern frameworks work internally.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固