?? 函 數(shù) 式 模 板 引 擎

v0.1.3
版本
2025-02-24
版本更新時(shí)間
3
安裝
1
star
簡(jiǎn)介
雖然PHP 是一種很強(qiáng)大的 Web 開發(fā)語言,但是HTML 和 PHP 代碼參雜一起,不易于書寫和閱讀;
使用 PHP 函數(shù)式實(shí)現(xiàn) HTML 頁面的方式,可以代替?zhèn)鹘y(tǒng)的 view 頁面,而無需手寫 HTML5 代碼;
安裝
composer require kingbes/fun-view
使用
示例一 普通使用
main.php
require "./vendor/autoload.php";
use Kingbes\FunView\Template;
// 內(nèi)容
$tpl = new Template([
// 模板目錄
"view_dir" => __DIR__ . DIRECTORY_SEPARATOR . "view" . DIRECTORY_SEPARATOR
]);
echo $tpl->fetch("index", ["num" => 3]);
模板頁 index.php
use function Kingbes\FunView\{
p,
hsc,
html,
h1,
span,
img,
head,
body,
title,
meta,
div,
style,
script
};
$view = html(
head(
meta(["charset" => "UTF-8"]),
meta([
"name" => "viewport",
"content" => "width=device-width, initial-scale=1.0"
]),
title("這個(gè)是個(gè)標(biāo)題"),
style([
"p" => [
"color" => "blue",
]
])
),
body(
p("hello world"),
h1("下面是循環(huán)<br/>:"),
div(function () use ($num) {
$div = "";
for ($i = 0; $i < $num; $i++) {
$div .= div("循環(huán):$i");
}
return $div;
}),
h1(
"world",
["style" => "color:red;", "id" => "1"],
span(" hello")
),
img(["src" => "https://unpkg.com/outeres/demo/carousel/720x360-1.jpg"]),
div(hsc("<h1>安全輸出</h1>")),
script("alert('hello world')")
)
);
// 顯示模板
echo $view;
示例二 webman使用
配置文件 config/view.php
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.wtbis.cn/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use support\view\Raw;
use support\view\Twig;
use support\view\Blade;
use support\view\ThinkPHP;
// 新增use
use Kingbes\FunView\FunView;
return [
// 配置FunView類
'handler' => FunView::class
];
控制器IndexController.php
<?php
namespace app\controller;
use support\Request;
class IndexController
{
public function view(Request $request)
{
// 常規(guī)使用即可
return view('index/view', ['name' => 'webman']);
}
}
視圖文件view/index/view.php
<?php
use function Kingbes\FunView\{
hsc,
html,
head,
body,
title,
meta,
div
};
$view = html(
head(
meta(["charset" => "UTF-8"]),
meta([
"name" => "viewport",
"content" => "width=device-width, initial-scale=1.0"
]),
title("這個(gè)是個(gè)標(biāo)題")
),
body(
div("wellcome to ", hsc($name)),
)
);
// 顯示
echo $view