kuofp/yatp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kuofp/yatp

Composer 安装命令:

composer require kuofp/yatp

包简介

A logic-less PHP template engine

关键字:

README 文档

README

Yet another TemplatePower

A logic-less PHP template engine

中文說明

Format

Blocks

#valid
<!-- @block_name -->
	...
<!-- @block_name -->

#valid (with zero or many spaces)
<!--@block_name-->
	...
<!-- @block_name     -->

#invalid (must use pair blocks)
<!-- @block_name -->

Marks

{mark_name}
  • A valid block_name/mark_name: [A-Za-z0-9_-]+
  • A mark_name should contain at least one alphabet (update since V1.1)

Get Started

Install

  • Via composer
$ composer require kuofp/yatp
  • Download directly
// Require it with the correct path
require_once 'yatp.php';

Methods

__construct(string $html_or_filepath)

// Initialize
$tpl = new Yatp('<strong>{str}</strong>');

// You can use a file (if exists).
$tpl = new Yatp('view.tpl');

render([ bool $print = true ])

// Print to screen by default
$tpl = new Yatp('<strong>Hello World!</strong>');
$tpl->render();

// Output:
// <strong>Hello World!</strong>


// Get result without a print
$html = $tpl->render(false);
echo $html;

// Output:
// <strong>Hello World!</strong>


// PHP code is allowed
$tpl = new Yatp('<?php echo "Hello World!"?>');
$tpl->render();

// Output:
// Hello World!

block(string $target)

// Support dot operation
$tpl = new Yatp('
	<!-- @a -->
		<!-- @c -->a.c<!-- @c -->
	<!-- @a -->
	<!-- @b -->
		<!-- @c -->b.c<!-- @c -->
	<!-- @b -->
');
$tpl->block('a.c')->render();

// Output:
// a.c


// Search the most likely one
$tpl = new Yatp('
	<!-- @a -->a
		<!-- @b -->b
			<!-- @c -->c
				<!-- @d -->d
				<!-- @d -->
			<!-- @c -->
		<!-- @b -->
	<!-- @a -->
');
// Equivalent
// $tpl->block('a.b.c.d')->render();
// $tpl->block('c.d')->render();
$tpl->block('a.d')->render();

// Output:
// d


// First is adopted when block is redefined
$tpl = new Yatp('
	<!-- @a -->1<!-- @a -->
	<!-- @a -->2<!-- @a -->
	<!-- @a -->3<!-- @a -->
');
$tpl->block('a')->render();

// Output:
// 1

assign(array $params)

// Assign to a mark
$tpl = new Yatp('<strong>{str}</strong>');
$tpl->assign([
	'str' => 'Hello World!'
])->render();

// Output:
// <strong>Hello World!</strong>


// Assign several times
$tpl = new Yatp('<strong>{str}</strong>');

// Equivalent
// $tpl->assign([
//    'str' => 'Hi!'
// ])->assign([
//    'str' => 'Hi!'
// ])->render();

$tpl->assign([
	'str' => ['Hi!', 'Hi!']
])->render();

// Output:
// <strong>Hi!Hi!</strong>


// Assign a variable to block in the same way
$tpl = new Yatp('<strong><!-- @str --><!-- @str --></strong>');
$tpl->assign([
	'str' => 'Hello World!'
])->render();

// Output:
// <strong>Hello World!</strong>


// You can assign with another block
$tpl = new Yatp('<strong>{mark}</strong>');
$msg = new Yatp('<!-- @block -->Hello World!<!-- @block -->');
$tpl->assign([
	'mark' => $msg->block('block')
])->render();

// Output:
// <strong>Hello World!</strong>


// Support dot operation, too
$tpl = new Yatp('
	<!-- @a -->
		<!-- @c -->a.c<!-- @c -->
	<!-- @a -->
	<!-- @b -->
		<!-- @c -->b.c<!-- @c -->
	<!-- @b -->
');
$tpl->assign([
	'a.c' => 'replaced'
])->render();

// Output:
// replaced b.c

nest(array $params)

$tpl = new Yatp('
	<ul>
	<!-- @li -->
		<li>{title}</li>
	<!-- @li -->
	</ul>
');

$data = [
	['title' => 'Lesson1'],
	['title' => 'Lesson2'],
];

// Equivalent
// $tpl->assign([
//     'li' => [
//         $tpl->block('li')->assign($data[0]),
//         $tpl->block('li')->assign($data[1]),
//     ]
// ])->render();

$tpl->assign([
	'li' => $tpl->block('li')->nest($data)
])->render();

// Output:
// <ul>
//     <li>Lesson1: Hello World!</li>
//     <li>Lesson2: Functions</li>
// </ul>

debug( void )

$tpl = new Yatp();

$tpl->block('a_missing_block')->assign([
	'a_missing_mark' => '',
	'#wrong style' => ''
])->debug();

// Output:
// Array
// (
//     [0] => Debug info:
//     [1] => block "a_missing_block" is not found
//     [2] => block or mark "a_missing_mark" is not found
//     [3] => block or mark "#wrong style" is invalid
// )

Example

view.html

<h1>{title}<h1>
<ul>
<!-- @li -->
	<li>{title}: {text}</li>
<!-- @li -->
</ul>

php code

$tpl = new Yatp('view.html');

$data = [
	[
		'title' => 'Lesson1',
		'text'  => 'Hello World!'
	],
	[
		'title' => 'Lesson2',
		'text'  => 'Functions'
	],
];

$tpl->assign([
	'title' => 'Syllabus',
	'li'    => $tpl->block('li')->nest($data)
])->render();

Output:

<h1>Syllabus<h1>
<ul>
	<li>Lesson1: Hello World!</li>
	<li>Lesson2: Functions</li>
</ul>

kuofp/yatp 适用场景与选型建议

kuofp/yatp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 252 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「templating」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kuofp/yatp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kuofp/yatp 我们能提供哪些服务?
定制开发 / 二次开发

基于 kuofp/yatp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 252
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-23