zkwbbr / view
此包的最新版本(v1.2.1)没有可用的许可信息。
使用纯PHP在布局文件中显示模板文件
v1.2.1
2022-07-20 17:31 UTC
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^8
README
使用纯PHP在布局文件中显示模板文件
安装
使用composer以zkwbbr/view
的方式安装
示例用法
创建一个模板文件夹(例如,myTemplates/
)
在您的模板文件夹内,创建一个布局文件(例如,myLayout.php
)并放置以下内容。
<html> <head> <title><?=$title></title> </head> <body> <?=$templateContent?> </body> </html>
在您的模板文件夹内,创建一个视图文件(例如,myView.php
)并放置以下内容。
<h1><?=$heading?></h1> <?=$body?>
在您的PHP代码(例如,控制器)中放置以下内容。
<?php use Zkwbbr\View; $data = [ 'title' => 'My Title', 'heading' => 'My Heading', 'body' => 'My Body' ]; $view = (new View\View) ->setData($data) ->setLayoutFile('myLayout') ->setTemplateVar('templateContent') ->setTemplateDir(__DIR__ . '/myTemplates/') ->setTemplate('myView') ->setBacktraceIndex(0) // # of nested calls relative to render(); for auto-detecting template file (try 0 first then increment until you find it) ->setStripStringFromTemplateFile('foo') // optional, remove string from template file (e.g., if your controller is the basis for auto template detection e.g., UserControllerIndex, and your actual template file is UserIndex, use 'Controller' as value here) ->render(); // you can also use generatedView() to return the generated view instead of outputting it
预期输出
<html> <head> <title>My Title</title> </head> <body> <h1>My Heading</h1> My Body </body> </html>