该包已被弃用且不再维护。未建议替代包。

简单的变量过滤管道

v1.0.1 2021-03-15 14:02 UTC

README

logo

v

Latest Stable Version Build Status License

简单的变量过滤和格式化管道

要求

  • PHP 7.1+
  • mbstring 扩展

安装

composer require luizbills/v --prefer-dist

用法

使用 v($value, ...$filters) 函数来过滤您的模板值。

<?= 'Name: ' . v( 'luiz', 'upper' ) ?> <!-- Name: LUIZ -->

特性

  • 默认情况下,所有值都会进行转义。
<?php

// escape by default
echo v( '<p>hello</p>' ); // => &lt;p&gt;hello&lt;/p&gt;

// same as
echo v( '<p>hello</p>', 'escape' ); // => &lt;p&gt;hello&lt;/p&gt;

// or you can strip all tags
echo v( '<p>hello</p>', 'strip_tags' ); // => hello

// or strip only "evil" tags: <script>, <style>, <iframe> and <link>
echo v( '<p>hello</p><script>evilFunc();</script>', 'safe_html' ); // => &lt;p&gt;hello&lt;/p&gt;

// and to disable autoescaping, use the `raw` filter
echo v( '<p>hello</p>', 'raw' ); // => <p>hello</p>
  • 过滤器参数有可选的字符串引号。
<?php
// you can do like this
echo v( 1567973782, 'date("d/m/Y")' ); // => 08/09/2019

// or like this (without double-quotes)
echo v( 1567973782, 'date(d/m/Y)' ); // => 08/09/2019

// Note: always use double-quotes if you need whitespaces in your argument,
// otherwise they will be deleted (with `trim`).
<?php
function repeat_callback ( $value, $args ) {
	$times = (int) $args->get( 0 ); // get the first argument
	return str_repeat( $value, $times );
}
v_register_filter( 'repeat', 'repeat_callback' );

function exclaim_callback ( $value, $args ) {
	return $value . '!';
}
v_register_filter( 'exclaim', 'exclaim_callback' );

echo v( 'Ha', 'repeat(5)', 'exclaim' ); // => HaHaHaHaHa!

// you can also overwrite the built-in filters

// default `date` filter
echo v( 1567973782, 'date("Y")' ); // => 2019

// custom `date` filter
function my_date_callback ( $value, $args ) {
	$format = $args->get( 0 );
	return 'date: ' . date( $format, $value );
}
v_register_filter( 'date', 'my_date_callback' );

echo v( 1567973782, 'date("Y")' ); // => date: 2019
  • 或者只需使用任何函数
// inline function
$upper = function ( $value ) {
	return strtoupper( $value );
}
echo v( 'ok', $upper ); // => OK

// or global functions (add an @ before)
echo v( 'ok', '@strtoupper' ); // => OK

// or methods
echo v( 'ok', [ 'MyClass', 'my_method' ] );
  • 上下文设置器 以避免与使用此库的其他应用程序/模块/插件发生冲突。
<?php
// The `v_register_filter` accepts an optional third argument called 'context'.
// Note: the default context is 'root'.

function exclaim_callback_v1 ( $value, $args ) {
	return $value . '!';
}
v_register_filter( 'exclaim', 'exclaim_callback_v1' ); // context = "root"

function exclaim_callback_v2 ( $value, $args ) {
	return $value . '!!!!!';
}
v_register_filter( 'exclaim', 'exclaim_callback_v2', 'v2' ); // context = "v2"

echo v( 'foo', 'exclaim' ); // => foo!

// change the context to "v2"
v_set_context( 'v2' );
echo v( 'foo', 'exclaim' ); // => foo!!!!!

// reset the context
v_reset_context(); // same as: v_set_context( 'root' );
echo v( 'foo', 'exclaim' ); // => foo!

// note: all built-in (or default) filters are available in any context
  • 可扩展。使用 v_load_extension 来覆盖或实现更多 默认过滤器(在任何上下文中都可用)。
<?php
// this function accepts an Array, where each key is a filter
v_load_extension( [
	// `exclaim` is now a default filter
	'exclaim' => function ( $value, $args ) {
		return $value . '!';
	}
] );
  • 轻松调试!
<?php
// use the `log` filter to log the current value with error_log function
echo v( 'hello', 'upper', 'log' );
// => logs: [v log] (string) "HELLO"

// You can also pass a ID to log filter, this help you identify outputs
echo v( 'hello', 'log(before upper)', 'upper' );
// => logs: [v log @ before upper] (string) "hello"

许可证

MIT