php-mohamed-nabil / style
Style 是一个用于 MVC 项目的 PHP 模板引擎
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-30 02:00:07 UTC
README
由 Mohamed Nabil (https://github.com/PHPMohamedNabil/) 提供!
[Style] 是一个轻量级的 PHP 模板引擎,适用于小型项目或教育目的。
在您的代码中感受到大型库模板引擎的力量,使用简单灵活,代码量少。
特性
- 简单的编译标签 {$variable},{#constant},@include(),{%loop $data%},{%if%},[ 注释 ],{%func echo str_len($string)%}
- 非常易于模板编译,只需一个名为 Style 的类即可加载并编译完整的模板。
- [新功能](硬编译模板)将数据发送到其他模板文件,以便在每次页面加载时注入到其他模板中。
- 轻松注入新表达式,您可以添加尽可能多的表达式。
- 在打印变量时安全,因为它过滤了 HTML 内容以防止一些 XSS 攻击。
目录
安装
-
安装 composer https://github.com/composer/composer
-
在您的应用程序文件夹内创建一个 composer.json
composer require php-mohamed-nabil/style
使用
通过传递包含视图文件的文件夹和一个缓存文件夹来创建 Style 实例。通过调用 render 方法来渲染模板。
use Style\Style; $style = new Style('template/','template/temp/'); $style->render('page_sections',[]);
自定义表达式
您还可以使用 addTempRole()
函数添加自定义表达式
$style->addTempRole('test','\~ob',function($capt){ return $capt[0].' ppppppppppppppppppoboobobo'; }); $style->render('page_sections',[]);
这允许您在模板中使用以下内容
here the ppppppppp : ~ob
部分
您还可以使用扩展视图和 @spread(parent_view_name)
@spread('layout')
使用 @sections 和 @addsection 从子视图发送数据到父视图
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>layout page</title> </head> <body> <p class="yield"> <!-- this will yield data and print it here form child view --> @addsection('content') </p> </html>
@spread('layout')
<!-- add data to the main view and render show it -->
@section('content')
My first paragraph in parent view
@endsection
硬编译功能
现在您可以像这样从一个视图发送数据到另一个视图,因为它将被编译并硬编码
在视图 main 中,您将编写以下表达式,在 main.stl.php 页面加载或编译时
视图 test 将在 main.stl.php 页面加载时注入随机数字,在具有类 title 的 h1 标签内
@hardcompile(test[] within h1:title data:"echo mt_rand(1,1000)")
结果在 test.stl.php 中
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Test</title> </head> <body> <h1 class="title"> 681 <!-- this a random number hardcoded by main view --> </h1>
硬编译可以(在 | 之后 | 之内)指定的标签中注入其他视图
您可以像这样向其他视图发送数据
@hardcompile(test['name'=>$name,$title] before h1:title data:"echo mt_rand(1,1000)")
包含视图
在视图页面中包含其他视图
@display('main',['data'=>$data])
foreach 循环
在模板中
<div class=""> @foreach($users as $user) {$user->username} @endforeach </div>
HTML 创建
您现在可以创建带有其输入数据的表单
[php] print \Style\Style::form('/',[ 'method'=>'post', 'enctype'=>'multipart/form-data', 'id'=>'first-form' ])->formInput('username',['class'=>'form-control','type'=>'text'])->formInput('password',['class'=>'form-control','type'=>'password'])->formInput('file',['class'=>'form-input-file','type'=>'file'])->renderForm(); [/php]
将输出
<form action="/" method="post" enctype="multipart/form-data" id="first-form"> <input name="username" class="form-control" type="text"> <input name="password" class="form-control" type="password"> <input name="file" class="form-input-file" type="file"> </form>
打印变量
{$var_name}
终止代码
视图,例如 die,您可以使用 @backwithfalse,它只是转换为返回 false 并退出代码,其后的任何代码或 HTML 都将不会执行
打印 HTML 内容
不停止实体,您可以直接打印 HTML 代码,而无需转义它,主要原因是如果您想显示帖子内容或具有要显示并受浏览器影响的 HTML 块,则可以使用 {@$post@} 作为示例
<div class="blog-post-content"> {@$posts->post_content@} </div>
表达式语句
许可
在 MIT 许可下发布。