PHP code example of kingbes / fun-view

1. Go to this page and download the library: Download kingbes/fun-view library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kingbes / fun-view example snippets


$config = [
    // 模板目录
    'view_dir'      =>  './views/',
    // 模板布局
    'layout_name'   =>  'layout', //布局模板入口文件名称
    'layout_item'   =>  '__CONTENT__', //布局模板内容变量标识
];

$tpl = new \Kingbes\FunView\Template($config);



// app/index/view/layout.php
use function Kingbes\FunView\{
    html,
    head,
    body,
    title,
    meta,
    style,
    hsc
};

$view = html(
    head(
        meta(["charset" => "UTF-8"]),
        meta([
            "name" => "viewport",
            "content" => "width=device-width, initial-scale=1.0"
        ]),
        title(hsc($title ?? "")),
        style([
            "p" => [
                "color" => "blue",
            ]
        ])
    ),
    body($__CONTENT__)
);

echo $view;



// app/index/view/user/add.php
use function Kingbes\FunView\{
    p,
    h1,
    div,
};

$view = div(
    p("hello world"),
    h1("hello world"));

echo $view;




use Kingbes\FunView\Template;

// 内容
$tpl = new Template([
    // 模板目录
    "view_dir" =>  __DIR__ . DIRECTORY_SEPARATOR . "view" . DIRECTORY_SEPARATOR
]);

echo $tpl->fetch("index", ["num" => 3]);



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("这个是个标题"),
        style([
            "p" => [
                "color" => "blue",
            ]
        ])
    ),
    body(
        p("hello world"),
        h1("下面是循环<br/>:"),
        div(function () use ($num) {
            $div = "";
            for ($i = 0; $i < $num; $i++) {
                $div .= div("循环:$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;

use function Kingbes\FunView\hsc;

/**
 * 安全输出,htmlspecialchars用法 function
 *
 * @param integer|string $text
 * @return string
 */
function hsc(int|string $text): string


use Kingbes\FunView\Tags;

function myview(mixed ...$args): string
{
    return Tags::__callStatic("myview", $args);
}
// <myview></myview> 标签


function diyview($str):string
{
    return "<div id='diyview'>".$str."</div>";
}