lkt/templates

LKT PHP 模板

2.0.1 2022-12-22 15:33 UTC

This package is auto-updated.

Last update: 2024-09-24 13:35:29 UTC


README

简单。快速。LKT 模板!

你是否厌倦了复杂的 PHP 模板引擎?

你想要像在常规 PHP 中一样编写代码吗?

那么这个库就是为你准备的!

安装

composer require lkt/templates

用法

布局文件

LKT 模板与 PHTML/PHP 文件一起工作。

在一个 PHTML 文件中,你可以做任何你用 PHP 可以做的事情,只需记住冒号语法。

例如,看这个 layout.phtml 文件

<div><?php echo $title; ?></div>

<?php foreach ($data as $datum): ?>
    <div><?php echo $datum; ?></div>
<?php endforeach; ?>

<?php if(isset($favouriteDrink)): ?>
    <div>Favourite drink: <?php echo $favouriteDrink; ?></div>
<? endif; ?>

你可能想知道数据从哪里来?接下来会说明

模板实例:访问布局

这就是如何访问 layout.phtml

use Lkt\Templates\Template;

// Create a template instance:
$template = Template::file('layout.phtml'); // You can set a relative path, but absolute path it's allowed too

// Feed the data
$template->setData([
    'title' => 'Hello from LKT Templates!',
    'data' => [1,2]
]);

// If you want to, you can add more data with:
$template->set('favouriteDrink', 'Apple juice'); // PEGI 3+ example

// Parse the layout
$html = $template->parse(); // String returned

// And now you can just print the html or do whatever you want to do with that string
echo $html;

// Alternatively, you can just print the template, and it will be automatically parsed:
echo $template; // equal to echo $template->parse();

生成的 HTML

本 README 中使用的布局,以及 PHP 示例中的数据将打印出类似以下内容

<div>Hello from LKT Templates!</div>
<div>1</div>
<div>2</div>
<div>Favourite drink: Apple juice</div>

附加说明

使用常量路径访问模板文件

$template = Template::file(__DIR__ . '/path/to/layout.phtml');