crhayes / scenic-view
快速且简单的视图继承和模板。
dev-master
2013-05-10 15:51 UTC
Requires
- php: >=5.3
This package is not auto-updated.
Last update: 2024-09-23 13:47:17 UTC
README
Scenic View 是一个小型库,提供视图和模板继承。语法直接使用 PHP,但非常简洁和易于表达。
以下示例使用 PHP 短语语法(以提供更干净的视图),如果您可以控制自己的服务器,这是可以的。注意:如果您计划分发代码或没有管理权限访问服务器,则必须使用 <?php ?>。
加载和渲染视图
$view = new Scenic\View('path/to/views'); echo $view->render('index.php', array('title' => 'Welcome', 'name' => 'Chris'));
视图继承和模板
布局
创建一个布局(模板)文件,其他文件将扩展它。 $show() 方法显示由扩展它的子视图定义的内容部分。
<html> <head> <title><?= $title ?></title> </head> <body> <? $show('content') ?> </body> </html>
视图
创建您的视图,它扩展布局并提供内容部分。
<? $extends('template.php') ?> <? $section('content') ?> <h1>Welcome, <?= $name ?></h1> <? $end() ?>
扩展父内容
如果您想在父视图中提供内容,但在子视图中扩展它怎么办?您可以这样做!
在模板中定义一个部分。
<html> <head> <title><?= $title ?></title> </head> <body> <? $section('content') ?> <h1>Welcome, <?= $name ?></h1> <? $end() ?> </body> </html>
在视图中使用 @parent 键来指定父内容应出现的位置。
<? $extends('template.php') ?> <? $section('content') ?> @parent <p>We hope you have a great stay!</p> <? $end() ?>
或者
<? $extends('template.php') ?> <? $section('content') ?> <p>We hope you have a great stay!</p> @parent <? $end() ?>
部分视图
包括部分视图,如头部或尾部,也很简单。
<html> <head> <title><?= $title ?></title> </head> <body> <? $section('content') ?> <h1>Welcome, <?= $name ?></h1> <? $end() ?> <? $include('footer.php') ?> </body> </html>