naingaunglwin-dev / view
简单的PHP视图库
1.0.0
2024-05-03 14:03 UTC
Requires
- php: ^8.0
This package is auto-updated.
Last update: 2024-09-17 13:52:40 UTC
README
简单的PHP视图库
贡献
- 这是一个开源库,欢迎贡献。
- 如果您有任何建议、错误报告或功能请求,请在项目仓库中创建问题或提交拉取请求。
要求
- PHP版本需要8.0或更高
安装与设置
- 您可以直接从仓库下载代码使用,或者使用composer下载。
使用Composer下载
- 如果您没有composer,请先安装composer。
- 在项目根目录创建文件
composer.json
。 - 将以下内容添加到
composer.json
{ "require": { "naingaunglwin-dev/view": "^1.0" } }
- 在项目根目录的终端中运行以下命令
composer install
- 或者直接在终端中运行
composer require naingaunglwin-dev/view
。
用法
- 在您的PHP文件中,
<?php require_once "vendor/autoload.php"; use NAL\View\View; $view = new View(__DIR__ . '/views'); // You can pass view file without extension if it is php file $view->render('index', ['status' => 'success']); // You can also render other file, // You can retrieve the view without rendering, $indexView = $view->render('index.html', [], true); // You can also render multi views $view->render(['index.html, test']);
章节用法
-
创建
one.php
和two.php
-
one.php
<?php //one.php echo 'this is one.php'; $this->displaySection('content');
- two.php
<?php //two.php $this->extends('one'); // view file name that need to extend $this->section('content'); // Section start with `content` name echo '<br>This is section content from two.php'; $this->endSection('content'); // End the section `content`
- index.php
<?php require_once "vendor/autoload.php"; use NAL\View\View; $view = new View(__DIR__ . '/views'); $view->render('two');
- 输出将会是
this is one.php This is section content from two.php