dana / interpolator
简单的字符串模板
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ^6.2
This package is not auto-updated.
Last update: 2021-02-23 07:31:09 UTC
README
Interpolator是一个非常简单的PHP字符串插值库。它使用结合了Ruby、Jinja/Twig和bash特征的语法——包括Jinja/Twig风格的'过滤器'。
使用方法
$interpolator = new Interpolator(); // Basic usage: Both of these print 'Hello, dana!' echo $interpolator->render('Hello, %{0}!', ['dana']), "\n"; echo $interpolator->render('Hello, %{name}!', ['name' => 'dana']), "\n"; // Filter usage: This prints 'Hello, DANA!' (u = upper-case) echo $interpolator->render('Hello, %{0|u}!', ['dana']), "\n"; // Filter usage: This prints 'Hello, "DANA"!' (u = upper-case, j = JSON-encode) echo $interpolator->render('Hello, %{0|uj}!', ['dana']), "\n"; // Filter usage: This prints 'Hello, 38095!' (c = CRC32, d = extract digits) echo $interpolator->render('Hello, %{0|cd}!', ['dana']), "\n";
Interpolator的render()方法是最有用的——它执行实际的插值然后返回结果。render()接受两个参数:要插值的字符串和一个'固定值'数组(可能被注入到字符串中的值)。当遇到占位符(%{foo})时,将在固定值数组的键中查找该占位符名称(foo),并将关联的值插入到占位符原来的位置。
过滤器
每个占位符可以可选地后缀一个竖线(|)和一个或多个单字符过滤器指定符。大多数可用的过滤器用于转义和散列——例如,e过滤器指定符在渲染之前通过escapeshellarg()传递固定值,m指定符使用MD5散列值。还有用于大小写转换(l、u)、空白处理(t、w、W)等的过滤器。过滤器可以串联在一起——例如,lhj将值转换为小写,然后通过htmlspecialchars(),然后通过json_encode()(按此顺序)。
以下是默认过滤器指定符及其行为的列表
a Extract letters [A-Za-z] from the string
b Encode the string using base64
B Encode the string using 'URL-safe' base64
c Hash the string with CRC32
C Hash the string with CRC32b
d Extract digits [0–9] from the string
e Escape the string for use as a shell argument (escapeshellarg())
f Escape the string for use in a URL (rawurlencode()), but preserve slashes
h Escape the string for use in HTML (htmlspecialchars())
H Escape the string for use in HTML (htmlentities())
j Escape the string for use in JSON (json_encode())
l Convert the string to lower-case (mb_strtolower())
L Convert the string to lower-case (strtolower())
m Hash the string with MD5
p Escape the string for use in a PCRE regular-expression pattern (preg_quote())
r Escape the string for use in a URL (rawurlencode())
R Escape the string for use in a URL (urlencode())
s Hash the string with SHA1
S Hash the string with SHA256
t Trim the string of leading/trailing white space (trim())
u Convert the string to upper-case (mb_strtoupper())
U Convert the string to upper-case (strtoupper())
w Collapse consecutive white space in the string into a single space
W Remove all white space from the string
不过,您不必使用默认值——您可以使用setFilter()和setFilters()方法覆盖一些或所有这些
$interpolator = new Interpolator(); // Replace all default filters by our own $interpolator->setFilters(['a' => 'ucfirst']); // This prints 'Foo' echo $interpolator->render('%{0|a}', ['foo']), "\n";
Interpolator还支持自动过滤器——在所有其他过滤器处理完成后自动应用。这特别适用于创建HTML模板,例如。可以使用特殊的-过滤器指定符抑制自动过滤器。
$interpolator = new Interpolator(); // Apply htmlspecialchars() as an auto filter $interpolator->setAutoFilters('h'); // This prints 'FOO&BAR' echo $interpolator->render('%{0|u}', ['foo&bar']), "\n"; // This prints 'FOO&BAR' (auto filters suppressed) echo $interpolator->render('%{0|u-}', ['foo&bar']), "\n";
其他
默认情况下,当Interpolator遇到缺失的固定值或无法转换为字符串的固定值时,它会抛出异常。您可以通过向构造函数传递一个选项来禁用此'严格'行为
// Silently ignore missing/mistyped fixtures $interpolator = new Interpolator(['strict' => false]);
注意,即使禁用了严格模式,非法过滤器指定符的存在仍会引发异常。
待办事项
- 更好的文档
- 更多测试
许可
MIT。