目录 wudimei-php是一个MIT许可的自由php框架。由杨庆荣开发维护,wudimei.com , QQ290359552

视图

视图的配置文件在config\view.php,我把这个配置文件内容拷出来作了注释。

<?php
return [
        'path' => __DIR__ . '/../resources/view' , //视图所在的根目录
        'compiled'  => __DIR__ . '/../storage/views_c' , //视图编译成php后存储的根目录
        'forceCompile' => false, //强制编译,不使用已经编译过的文件。默认false,只要视图文件最后修改时间比编译后的新,就重新编译。
        'skipCommentTags' => true //跳过代码前后的<!--和-->标记。
];

在使用视图时,一般要在有命名空间的文件顶部加入use View;语句,例如:

<?php
namespace App\Frontend\Controllers;
use View;
//...
class UserController{
}

循环输出:

<?php
$vars = [
    'students' => [
        ['name' => 'jim','age'=>14  ],
        ['name' => 'lily','age'=>15 ],
        ['name' => 'lucy','age'=>13 ]
   ]
];
echo View::make("default.index.for",$vars );
 ?>

此处的"default.index.for"会被转换为视图配置中的path./default/index/for.view.phtml。 也就是

__DIR__ . '/../resources/view' . '/default/index/for.view.phtml';

/default/index/for.view.phtml的代码是这样的(为简化,我删掉了一些html标记)。

@for( $i=0; $i< count( $students );$i++)
    {{$students[$i]['name']}} <br />
    {{$students[$i]['age']}} <br />
@endfor

@for( $i=0; $i< count( $students );$i++)
    @php($stu = $students[$i];)
    {{$stu['name']}} <br />
    {{$stu['age']}} <br />
@endfor

如果$students是对象数组,则可以这样写:

@for( $i=0; $i< count( $students );$i++)
    {{$students[$i]->name}} <br />
    {{$students[$i]->age}} <br />
@endfor

这里的foreach和php大致相同,不过我加了@foreachelse

@foreach(   $students_empty_val as $stu)
     {{$stu['name']}} ,
     {{$stu['age']}}  <br />
@foreachelse
 no data
@endforeach

当然,没有@foreachelse也可以工作。

@foreach(   $students  as $stu)
     {{$stu['name']}} ,
     {{$stu['age']}}  <br />
@endforeach

如果是对象数组,和php语法一样:

@foreach(   $students  as $stu)
     {{$stu->name}} ,
     {{$stu->age}}  <br />
@endforeach

判断语句,表达式语法参考php。

@if( $role == "admin" )
  hello,admin! <br />
@elseif( $role == 'root' )
  hello,root!<br />
@else
  hello,user!<br />
@endif

当循环

@while( $index < 10 )
  {{$index}} <br />
  @{$index++}
@endwhile

注意 @{$index++}最终会被理解为<?php $index++; ?>,并且不输出$index的值。@{ }是php代码标记。 赋值和代码:

${
$sum = $num1+$num2+$num3;
$data = DB::table('blog')->all();
}

@foreach( $data as $item )
  {{$item->name}} <br />
@endforeach

输出变量:

{{$var}} 这是经过html字符转义后输出,是为了防跨站代码。
{!$var!} 这是不经过html字符转义后输出。
{{@$errors['email']}} 在{{和}}之间的变量表达式前加符号@可以关闭错误报告。

@end关键字可以代替@endif,@endfor,@endwhile,目的是减少键入。当然,阅读起来比较吃力。

@if( $role == 'admin' )
    hello,admin
@else
    hello,guest
@end

@for( $i=0; $i< 5;$i++ )
    {{$i}},
@end

@{$i=0}
@while($i<8)
    {{$i++}} ,
@end

the tag \@end does not support \@foreach and \@section
<br />
@section('footer')
    created by wudimei.com
@endsection

转义字符要在前面加上\

\{{$name}}
\{{$name}}
admin\@wudimei.com

包含文件@include(视图名称)

@include('default.index.inc_head')

它会把__DIR__ . '/../resources/view' . '/default/index/inc_head.view.phtml'这个文件包含进来。

呼叫函数:

<?php
function hello($name){
    return 'hello,' . $name . '!';
}

在视图中

@hello("Yang Qing-rong") <br />

便会输出

hello,Yang Qing-rong! <br />

这里在视图中也可以定义一个函数:

@{

   $myTitle = "title";

   function add( $a , $b ){
     return $a+$b;
   }
}

{{$myTitle}}
{{  add(100,200)  }}

模版继承,先定义一个母模板,位置在:/default/layout/main.view.phtml

<!DOCTYPE html>
<html>
<head>
<title>@section('title','WudimeiPHP')</title>
</head>
<body>

<header>this is header</header>
@section('content')
default content
@endsection

<footer>this is footer </footer>
</body>
</html>

定义一个子模板扩展它。

@extends('default.layout.main')
@section('title','WudimeiPHP View')

@section ('content')

    @substr($name,0,4) ,你好!

@endsection

@extends('default.layout.main')表示继承上述模版。 @section有两种方式:

第一种是只有一个参数的,它需要@endsection与它配对,之间的内容就是它的默认输出。 第二种方式是把默认输出放到第二个参数中。

@section最终会被编译成模版的一个函数。我利用php类继承和方法重写的机制使@section可以重写。

如果要引用父@section的输出,可以使用@parent关键字。

@section ('location')
     new location
     @parent
@endsection

你完全可以在一个section中定义一个子section

@section ('content')
    @section('location')
        location
    @endsection
  hello,world
@endsection

如果你想在双参数的section中调用代码,您可以在函数前面加一个@,并且把代码放在双引号中。

@section('title',"@trans('setting.setting')" )

如果你觉得不想显示@if之类的关键字,你可以用html注释标记把它隐藏起来。

<!--@if( $role == 'admin')-->
hello,{{$role}}
<!-- @else -->
hello,guest
<!-- @endif -->

<!-- @foreach($years as $year) -->
<!-- {{$year}} --><br />
<!-- @foreachelse -->
sorry,years arrary is empty
<!-- @endforeach -->

@if( count( $years) == 4 )
4 years
@else
not 4 years
@endif

<!-- @section('body') -->
this is the body
<!-- @endsection -->
<!-- this is html comment,was regnized by Wudimei::Parser2 -->
<!--[if lt IE 9]>

      <script src="/{{$role}}/assets/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="/assets/libs/respond/1.4.2/respond.min.js"></script>
<![endif]-->

当然,最好把config/view.php中的'skipCommentTags' => true保持为true。这样@if之类前后的注释标记就会被自动去除。