charm/tpl
Composer 安装命令:
composer require charm/tpl
包简介
An ultra minimalistic, single file template engine. Uses PHP as the template language, but provides the type of template inheritance you see in Blade or Twig.
README 文档
README
Ultra minimalistic template engine. For those of use who don't like to spend a day setting up a framework and everything else just to make tiny tool or feature. Works great with charm/orm for simple database abstraction and charm/router for routing - but it needs neither.
I don't recommend PHP developers to learn yet another template language. The web is moving toward APIs, so I decided to write the smallest most concise version of a template engine I could - without sacrificing the nice things about blade and twig.
There really should be no limitation regarding how big projects you can use this for though. The source code is quite simple. The challenge was in getting the output buffering right, and get an elegant structure.
See below for template file examples. This is how you would use the template engine:
// TPL will proxy properties and methods from the $user instance in your template.
$template = new TPL('user/profile', $user);
echo $template->render(); // echo, send mail or whatever you want.
If you are really lazy, this also works. I don't recommmend it, and will probably remove the feature because it relies on the __destruct() method.
<?php
require('vendor/autoload.php');
new Charm\TPL('hello-world', ['current_time' => gmdate('Y-m-d H:i:s')]);
Installation & Setup
Install the package:
composer require charm/tpl
Create a folder for template files:
mkdir templates
Make sure the following PHP code runs before you try to render a template:
define APP_ROOT = '/var/www/html'; // Edit as appropriate
That's it! charm/tpl expects template files to have the extension .tpl.php.
A minimal master template file
We'll put this file in templates/skeleton/html5.tpl.php.
<!DOCTYPE html>
<html>
<head>
<!-- $this->html() helps you NOT have to type htmlspecialchars($this->title, ENT_QUOTES) -->
<title><?=$this->html("title"); ?></title>
</head>
<body>
<main>
<?=$this->body; ?>
</main>
<sidebar>
<?=$this->sidebar; ?>
</sidebar>
<footer>
<?php foreach ($this->footerBlocks as $block) { ?>
<?=$block; ?>
<?php } ?>
</body>
</html>
Of course, everything is unrestricted PHP code, so you can do whatever you want.
A minimal child template file
This file could be in templates/index.tpl.php.
<?php $this->extend('skeleton/html5'); ?>
<h1>Body Part</h1>
<p>Whatever you output immediately after extend() will be used as the 'body' variable for the
parent template.</p>
<?php $this->part('sidebar'); ?>
<h2>Sidebar</h2>
<p>This goes into the 'sidebar' variable of the parent.</p>
<?php $this->add('footerBlocks'); ?>
<h3>Footer Block 1</h3>
<p>Using 'add()' makes 'footerBlocks' become an array.</p>
<?php $this->add('footerBlocks'); ?>
<?=$this->include('parts/footer-block', [ 'title' => 'Footer Block 2', 'text' => 'Some Text' ]); ?>
<?php $this->end(); ?>
A minimal reusable template part
This file could be put in `templates/parts/footer-block.tpl.php'.
<div class="footer-block">
<h3><?=$this->html('title'); ?></h3>
<div><?=$this->html('text'); ?></div>
</div>
charm/tpl 适用场景与选型建议
charm/tpl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 charm/tpl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 charm/tpl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-10