wudimei / template
Wudimei/template 是一个类似于 blade 的 PHP 模板引擎
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-29 06:02:48 UTC
README
Wudimei/template 是一个类似于 blade 的 PHP 模板引擎,它们并不相同。
许可证
本软件根据 MIT 许可证(MIT)进行分发。请阅读 LICENSE 以了解软件可用性和分发信息。
安装
composer require wudimei/template:dev-main
使用
examples/init.php
<?php /* ini_set("display_errors",true); error_reporting(E_ALL|E_ERROR); */ use Wudimei\Template\Engine; //require_once __DIR__ . '/../src/Template/Engine.php'; require_once __DIR__ . '/vendor/autoload.php'; $config =[ 'paths' => [ __DIR__.'/view' ], 'compiled' => __DIR__.'/viewc', //view's file extension, html 'ext' => 'html', //if true,recompile anyhow 'force_compile' => true, //if view is modified,recompile again. 'compile_check' => true, //write "don't edit this content" in compiled file 'write_do_not_edit_comment' => false, //multiple white characters to one blank char 'reduce_white_chars' => false, ]; $template =new Engine($config);
examples/hello.php
<?php require_once __DIR__ . '/init.php'; $vars =[]; $vars['name'] ='Yang Qing-rong'; echo $template->fetch('demo.hello',$vars); ?>
examples/view/demo/hello.html
hello,{{$name}}!
浏览器输出
hello,Yang Qing-rong!
关键词
模板的关键词是: _、V、M
$_ 是模板内容变量。
$V 保留您分配的变量。
M 是主节名称。
@
两个 @@ 代表 @ 本身。
输入
Email: yangqingrong@@wudimei.com
输出
Email: yangqingrong@wudimei.com
examples/ifelse.php
<?php require_once __DIR__ . '/init.php'; $vars =[]; $vars['score'] = 85; echo $template->fetch('demo.ifelse',$vars); ?>
examples/view/demo/ifelse.html
@if( 90 <= $score && $score <=100) A @elseif( 80 <= $score && $score <90) B @elseif( 70 <= $score && $score <80) C @elseif( 60 <= $score && $score <70) D @else E @endif
浏览器输出
B
examples/foreach.php
<?php require_once __DIR__ . '/init.php'; $data =[]; for( $i =1; $i<3;$i++){ $item =new stdClass(); $item->id=$i; $item->name ='Yang Qing-rong'.$i; $data[] =$item; } echo $template->fetch('demo.foreach',compact('data')); ?>
遍历
@foreach 与 PHP 中的 foreach 类似,如果 $data 为空,则转到 @foreachelse 块。
examples/view/demo/foreach.html
<table border="1"> @foreach($data as $row) @if($row->id > 0) <tr> <td>{{$row->id }}</td> <td>{!!$row->name!!}</td> </tr> @endif @foreachelse Sorry,no data. @endforeach </table>
浏览器输出
<table border="1"> <tr> <td>1</td> <td>Yang Qing-rong1</td> </tr> <tr> <td>2</td> <td>Yang Qing-rong2</td> </tr> </table>
注释
{{-- comment here,won't be shown --}}
@php @endphp
在 @php 和 @endphp 之间的代码将被转换为 PHP 标签 <?php 和 ?>。如果您想显示一个变量,请将变量附加到 $_,内容变量。
@php $ad ="Wudimei Template Engine is free of charge."; $_ .= $ad; //output to template @endphp
保留
在 @keep 和 @endkeep 之间的代码保持不变。
@keep @foreach($data as $row) @if($row->id > 0) {{$row->id }} @endif @endforeach @endkeep
@include
@include(const string viewName,array $variables)
通过 viewName 包含一个视图,并将视图变量传递给它。
examples/view/components/nav.html
<nav style="background-color:#E8E8E8;"> {{$date}} {{$title}} </nav>
在另一个文件中,让我们包含 components.nav,并将一个数组传递给第二个参数。
@include('components.nav',['date' => '2020-10-07','title'=>$title])
扩展
@extends(const string parentViewName )
@extends 类似于 OOP 的扩展。
超视图或父视图如下所示
examples/view/layout/default.html
<!DOCTYPE html> <html> <head> @section('head') @endsection </head> <body> @section('content') @endsection </body> </html>
examples/view/demo/extends.html
现在,我们创建一个子视图,以增强它。
@extends('layout.default') @section('head') @endsection @section('content') @include('components.nav',['date' => '2020-10-07','title'=>$title]) <h1> {{$title}} </h1> @endsection
yield
文件名: examples/layout/yield.html
@yield('section_name' ,'default value')
@yield 与 @section 类似,但 @yield 是单身,没有 @endyield。
<!DOCTYPE html> <html> <head> @section('head') @endsection </head> <body> @yield('content' ,$title) </body> </html>
文件名: examples/demo/yield.html
@parent 获取父节或 yield 的内容,并在此处渲染。
@extends('layout.yield') @section('head') @endsection @section('content') @parent() <h1> {{$title}} </h1> @endsection
页面缓存
public Template::cache( $cacheName,$seconds,$func )
如果页面缓存存在且未过期,则返回缓存内容。
否则,调用您提供的 $func,将结果存储在缓存文件中,最后返回缓存内容。
<?php require_once __DIR__ . '/init.php'; $cid =1; $page =2; $cacheName= 'article_'.$cid.'_'.$page; echo $template->cache( $cacheName , 5,function() use($template,$cid,$page){ $name ='Yang Qing-rong'; $name .= ' , cid: '.$cid . ' , '.$page .' '; $name .= date('Y-m-d H:i:s'); return $template->fetch('demo.hello',compact('name')); }); ?>
自定义
<?php require_once __DIR__ . '/init.php'; function op_loop( $args ){ list( $data,$item ) =preg_split('#\s*,\s*#',$args); $code = ' foreach( ' .$data .' as '.$item .'){ '; return $code; } function op_endloop( $args ){ return '}'; } function op_sayHello( $args ) { $code = ' $arr = ['.$args.']; '; $code .= ' $__TPL .= "hello,".$arr[0]."!"; '; return $code; } $template->addOp(['loop','endloop']); $template->addOp('sayHello'); $students =[ ['name'=>'yqr','id'=>1], ['name'=>'yqr2','id'=>2]]; echo $template->fetch('demo.customize',compact('students')); ?>
examples/view/demo/customize.html
上面定义了 OP @loop、@endloop 和 @sayHello。
@loop( $students , $stu ) @if( $stu['id'] > 0) {{$stu['id']}} {{$stu['name']}} <br /> @endif @endloop @sayHello('Wudimei Template Engine!')
捐赠
如果您愿意,请随意捐赠一小笔钱给我,以帮助这个项目,包括未来的改进和错误修复。
提示:本项目 免费!捐赠 不是必需的!
微信:wudimei_com
PayPal: yangqingrong@gmail.com
提示:本项目 免费!捐赠 不是必需的!