php-mohamed-nabil/style
Composer 安装命令:
composer require php-mohamed-nabil/style
包简介
Style is a php template engine for you can use for your mvc projects
README 文档
README
By Mohamed Nabil (https://github.com/PHPMohamedNabil/)!
[Style] is lightweight a tiny PHP Template Engine you can use for small projects or educational purposes.
Feel the power of the template engines of big libraries in your code with simple and flexible usage and little code.
Features
- Simple compiling tags {$variable}, {#constant}, @include(), {%loop $data%}, {%if%}, [ comment ],, {%func echo str_len($string)%}
- Very Easy template Compiling as one class called Style just loads the full template and compiling it.
- [new feature] (Hardcompiling Templates) to send data to other template file so that be injected into other template in every page load.
- Easy to inject a new experissions fell free to add as many as you want.
- Secure when printing variables , as its filtered html content against some xss attacks.
Table of contents
Installation
-
Install composer https://github.com/composer/composer
-
Create a composer.json inside your application folder:
composer require php-mohamed-nabil/style
Usage
Create a Style instance by passing it the folder where your view files are located, and a cache folder. Render a template by calling the render method.
use Style\Style; $style = new Style('template/','template/temp/'); $style->render('page_sections',[]);
Custom-Expressions
You can also add custom expressions using the addTempRole() function:
$style->addTempRole('test','\~ob',function($capt){ return $capt[0].' ppppppppppppppppppoboobobo'; }); $style->render('page_sections',[]);
Which allows you to use the following in your template:
here the ppppppppp : ~ob
Sections
You can also use extend views and using @spread(parent_view_name)
@spread('layout')
using also @sections @addsection to send data from child to parent view
<!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
Hard-compiling-Feature
you can now send data from one view to another one as it will be compiled and hardcoded example :
in the view main you will write the below expression that when view main.stl.php page loaded or compilled
The view test will be injected by random number in every main.stl.php page load within h1 tag that has class title
@hardcompile(test[] within h1:title data:"echo mt_rand(1,1000)")
results in 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>
hard compilling can be (before | after | within) the specified tag in other view to be injected
you can send data to other view like this :
@hardcompile(test['name'=>$name,$title] before h1:title data:"echo mt_rand(1,1000)")
including-view
get other view included in view page
@display('main',['data'=>$data])
foreach-loop
in tempaltes
<div class=""> @foreach($users as $user) {$user->username} @endforeach </div>
Html-Creation
you can now create form with its input data
[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]
will output:
<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>
Printing-Vars
{$var_name}
Terminate-the-code
of view like die you can use @backwithfalse it is just converted to return false and exit from code any code or html after it will not be executed
printing-html-content
without stopping entities you can print html code witout escaping it the main reason of it if you want to show a post content or has a block of html code to be appear and effected by browser you can use {@$post@} as an expample:
<div class="blog-post-content"> {@$posts->post_content@} </div>
Expressions-of-statments:
| Expression | Description |
|---|---|
{$var} |
for printing the variable var with escaping against xss |
{%$var%} |
printing var or any string escaping or filtering it |
{@$var@} |
printing var or any string without escaping it or filtering it |
{%var='name'} |
define a variable inside the view :$var='name' |
{%func echo ucfirst($var)%} |
execute the function or echo it echo word is optional if you want to echo the function |
[comment]ww [/comment] |
any thing in between it will not be compilled |
[php] var_dump($arr); [/php] |
write php code |
{%if $var>0%} |
define if statment |
{%else%},{%elseif%} and {%endif%} |
define else or elseif statment and you can use endif statment to end the statment |
@addsection($name) |
used in layout or parent view to implement section content that will be printed later in child view |
@spread($name) |
extend the parent view in the child view |
@section($name) |
start the section in child view |
@endsection($name) |
end the section in child view |
@foeach |
start the for each loop |
@endforeach |
end the for each loop |
@for() |
start the for loop |
@endfor |
end the for loop |
@while() |
start while statment |
@endwhile |
end while statment |
@switch($var) |
start the switch statment |
@case($name) |
case condition inside switch statment |
@break |
break the statment or the loop |
@continue |
continue the statment or the loop |
@default |
default condition inside switch statment |
@backwithfalse |
it is just converted to return false and exit from code any code or html after it will not be executed |
@hardcompile(view_name[] before|after|within tagname:classname data:"php_code_here") |
hard compiling other view_name and inject data content before or after or within tagname that has a classname this will send data to other view on every exacute of this experission |
Licence
published under the MIT Licence.
php-mohamed-nabil/style 适用场景与选型建议
php-mohamed-nabil/style 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「style」 「template-engine」 「php template engine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-mohamed-nabil/style 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-mohamed-nabil/style 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-mohamed-nabil/style 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Symfony bundle for using the CssToInlineStyles translator by tijsverkoyen
Fixing php code style
Provides ProcessWire integration for various template engines such as Twig.
Enable Laravel Blade Template Engine for Kirby 4 and Kirby 5
The coding standard applying to all Youdot PHP projects, based on Doctrine set of PHPCS rules, with additional checks.
Run all code formatting tools with one command: pretty
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-11