techworker / nano
简单、小巧且快速的字符串模板功能,从原始 https://github.com/trix/nano 转移而来。
dev-master
1913-11-13 00:00 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is auto-updated.
Last update: 2024-08-25 09:07:12 UTC
README
这是一个代码可读性提升、以占位符为导向、缺陷较少的 sprintf 功能,灵感来源于 https://github.com/trix/nano。当然,是否接受与 PHP 核心功能相比的额外开销取决于您。我使用它来格式化日志消息或异常,这意味着大多数代码部分的速度无关。试试看,它小巧、文档完善且经过测试。
关于
此类试图避免手动字符串连接,通过替换以下代码片段
echo "The Parameter " . $param . " with value " . $value . " in method " . __CLASS__ . ":" . __FUNCTION__ . " was simply wrong.";
echo sprintf("The Parameter %s with value %s in method %s:%s was simply wrong.", $param, $value, __CLASS__, __METHOD__ );
..为..
$message = "The Parameter {param} with value {value} in method {class}:{method} was simply wrong"; echo (new Nano($message))->data([ 'param' => $param, 'value' => $value, 'method' => __METHOD__, 'class' => __CLASS__ ]);
安装
您可以使用多种方式使用 Techworker\nano
类,但首先您必须安装它。最佳方式是通过 composer,将其添加到 require 列表中
{ "require": { "techworker/nano": "dev-master" } }
然后安装包
composer install
之后,您就可以使用 Techworker\Nano
,或者您可以通过下载(请参阅下载链接)并手动安装来手动安装包。
文档
您有大量方式可以使用 Techworker\Nano
类。我们在这里介绍了一些示例,但我希望您去 phpunit\tests 目录看看所有示例。
让我们从替换说明开始
- 每个占位符应位于花括号内,例如
{my_placeholder}
。 - 您可以通过定义以冒号分隔的访问树来访问嵌套数据,例如
{my_root_element:level1_element:level2_element}
。 - 您可以为 printf 和 sprintf 格式化选项分配额外的选项,例如
{price|%.2f} €
。
首先,将以下 use
语句添加到您的代码中,以直接访问 Nano
或使用完整的 \Techworker\Nano
命名空间\类定义
echo Techworker\Nano::tpl("test"); use Techworker\Nano as Nano; echo Nano::tpl("test");
静态调用
最简单且最不侵入的方法
echo Techworker\Nano::tpl("Agent {number}", array("number" => 7)); // outputs: Agent 7
面向对象的用法
简短简单,一些使用示例。
echo (new Techworker\Nano("Agent {number}"))->data(array("number" => 7)); // outputs: Agent 7 echo (new Techworker\Nano("Agent {number|%03d}"))->data(array("number" => 7)); // outputs: Agent 007
对象获取器
try{ throw new \Exception("Exception Message", 110); } catch(\Exception $ex) { echo new Techworker\Nano("Exception {message} and {code} thrown", $ex); } // outputs: Exception Exception Message and 110 thrown
深度结构
echo new Techworker\Nano("Hello {name:firstname} {name:lastname}", [ 'name' => [ 'firstname' => 'Benjamin', 'lastname' => 'Ansbach' ] ]); // outputs: Hello Benjamin Ansbach
简单值
echo (new Techworker\Nano())->template("Hello {name}")->value("name", "Benjamin"); // outputs: Hello Benjamin
默认值
echo (new Techworker\Nano())->template("Hello {name}")->default("Stranger"); // outputs: Hello Stranger
复杂和嵌套!
// Complex Example $empOfTheMonth = new stdClass(); $empOfTheMonth->name = 'Benjamin Ansbach'; $empOfTheMonth->month = 2; $empOfTheMonth->year = 2013; class Income { public function getAmount() { return 0; } } $data = [ 'company' => [ 'name' => 'Techworker', 'employees' => ['Benjamin Ansbach', 'Techworker'], 'income' => new Income(), 'empofmonth' => $empOfTheMonth ] ]; $company = <<<EOT Name: {company:name} Employees: {company:employees:0} and {company:employees:1} Income: {company:income:amount|%.2f}$ Employee of the Month: {company:empofmonth:name} in {company:empofmonth:year}/{company:empofmonth:month|%02d} EOT; echo (new Techworker\Nano($company, $data)); // outputs: // Name: Techworker // Employees: Benjamin Ansbach and Techworker // Income: 0.00$ // Employee of the Month: Benjamin Ansbach in 2013/02
实例重用示例
$data = [ ['firstname' => 'Benjamin', 'lastname' => 'Ansbach'], ['firstname' => 'The', 'lastname' => 'Techworker'] ]; $nano = new Techworker\Nano("{firstname} {lastname}"); foreach($data as $item) { echo $nano->data($item) . "\n"; } // outputs // Benjamin Ansbach // The Techworker