underscore_PHP Underscore入门

underscore

If you’ve ever used the Backbone framework for JavaScript, you’ll already be familiar with Underscore. Indeed, it’s become incredibly useful for JavaScript developers in general. But did you know that it’s been ported to PHP?

如果您曾经将Backbone框架用于JavaScript,那么您将已经熟悉Underscore。 实际上,对于JavaScript开发人员而言,它变得异常有用。 但是您知道它已经移植到PHP了吗?

In this article I’ll take a look at Underscore, what it can do, and provide some examples of where it might be useful.

在本文中,我将介绍Underscore的功能,并提供一些可能有用的示例。

什么是下划线? (What is Underscore?)

Underscore describes itself as a “utility belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects. It’s the tie to go along with jQuery’s tux, and Backbone.js’s suspenders.”

Underscore将自己描述为“ JavaScript的实用程序带库,它提供了Prototype.js(或Ruby)中期望的许多功能编程支持,但没有扩展任何内置JavaScript对象。 这是与jQuery的晚礼服和Backbone.js的吊带一起使用的纽带。”

Most notably, Underscore provides a bunch of utilities for working with collections and arrays, some for working with objects, basic templating functionality and a number of other useful functions.

最值得注意的是,Underscore提供了一堆用于处理集合和数组的实用程序,一些用于处理对象,基本模板功能以及许多其他有用的功能。

The functions which operate on collections and arrays can be particularly useful when dealing with JSON, which makes it great for handling responses from web services.

处理集合和数组的函数在处理JSON时特别有用,这使其非常适合处理来自Web服务的响应。

安装/下载 (Installation / Download)

The easiest way to download the library is using Composer:

下载库的最简单方法是使用Composer

{
    "require": {
        "underscore/underscore.php": "dev-master"
    }   
}

Alternatively, you can download it manually, or clone it from Github – you need only require one file, underscore.php.

另外,您可以手动下载它,也可以从Github克隆它 -您只require一个文件underscore.php

PHP下划线的语法 (Syntax of PHP Underscore)

In the original JavaScript library, all Underscore’s functions are prefixed with an underscore and a dot; e.g. _.each, _.map, _.reduce. In PHP, the underscore is generally reserved as an alias of gettext() (for translating strings), so instead it uses a double-underscore.

在原始JavaScript库中,所有Underscore函数都以一个下划线和一个点作为前缀。 例如_.each_.reduce . _.map_.reduce . _.reduce 。 在PHP中,下划线通常保留为gettext()的别名(用于翻译字符串),因此,它使用双下划线。

All the functions that make up the library are available as static methods of a class called __ – i.e., a double-underscore.

组成库的所有函数都可以作为称为__的类的静态方法使用-即双下划线。

So, if we wish to map the JavaScript functions to their PHP equivalent:

因此,如果我们希望将JavaScript函数映射到它们PHP等效项:

JavaScript          PHP
_.each              __::each
_.map               __::map
_.reduce            __::reduce

…and so on.

…等等。

You can also use it in an object-oriented fashion;

您也可以以面向对象的方式使用它。

__(array(1, 2, 3))->map(function($n) { return $n * 2; });

Is equivalent to:

等效于:

__::map(array(1, 2, 3), function($n) { return $n * 2; });

For the purposes of this article, I’m going to use the static method style.

出于本文的目的,我将使用静态方法样式。

处理集合和数组 (Working with Collections and Arrays)

(Each)

You can iterate through an array, applying a function to each item using __::each.

您可以遍历数组,使用__::each将函数应用于每个项目。

For example:

例如:

$items = array(1, 2, 3, 4, 5);
__::each($items, function($item) { print $item; });

Produces:

产生:

12345

Here’s a (slightly) more useful example:

这是一个(稍微)有用的示例:

$student_records = array(
    array(
        'name'    =>  'Joe Bloggs',
        'id'      =>  1,
        'grade'   =>  72,
        'class'   =>  'A',
    ),
    array(
        'name'    =>  'Jack Brown',
        'id'      =>  2,
        'grade'   =>  67,
        'class'   =>  'B',
    ),
    array(
        'name'    =>  'Jill Beaumont',
        'id'      =>  3,
        'grade'   =>  81,
        'class'   =>  'B',
    ),
);

__::each($student_records, function($record) { 
    print $record['name'] . ' ' . $record['grade'] . '<br />'; 
});

This produces:

这将产生:

Joe Bloggs A
Jack Brown B
Jill Beaumont B

Later, we’ll look at some better ways of doing something like this, when we delve into templating.

稍后,当我们深入研究模板时,我们将研究一些类似的更好方法。

采摘 (Pluck)

If you have a multi-dimensional array and you wish to “pluck” certain values out and flatten them into a single-dimensional array, you can use __::pluck.

如果您有多维数组,并且希望“抽取”某些值并将其展平为一维数组,则可以使用__::pluck

The Facebook API provides a real-world example of when this might be useful. When you request a list of a Facebook user’s friends, the result (when json_decode‘d into a multi-dimensional array) looks like this:

Facebook API提供了一个实际的示例,说明何时可能有用。 当您请求一个Facebook用户的朋友列表时,结果(当json_decode放入多维数组时)如下所示:

$response = array(
    'data'  =>  array(
        array(
            'name'  =>  'Joe Bloggs',
            'id'        =>   123456789,
        ),
        array(
            'name'  =>  'Jack Brown',
            'id'        =>  987654321,
        ),
    )
...

If we want to extract the Facebook user ID’s into a single-dimensional array, we can do this:

如果我们要将Facebook用户ID提取到一维数组中,则可以执行以下操作:

$ids = __::pluck($response['data'], 'id');
// array(123456789, 98765432)

最小和最大 (Minimum and Maximum)

Based on our sample student records from earlier, we could find the student with the highest grade using __::max:

根据之前的样本学生记录,我们可以使用__::max找到成绩最高的学生:

__::max($student_records, function($student) { return $student['grade']; });
// returns array('name' => 'Jill Beaumont', 'id' => 3, 'grade' => 81, 'class' => 'B')

Or the lowest, by using __::min:

或最低的,通过使用__::min

__::min($student_records, function($student) { return $student['grade']; });
// returns array('name' => 'Jack Brown', 'id' => 2, 'grade' => 67, 'class' => 'B')

As you can see, rather than simply return the highest or lowest grade, these functions return the corresponding item from the array – i.e., the student record.

如您所见,这些函数不仅返回最高或最低成绩,而且还返回数组中的相应项目,即学生记录。

过滤并拒绝 (Filter and Reject)

The filter method runs a truth test on a collection or array, and returns only those items for which it passes.

filter方法对集合或数组运行真相测试,并仅返回其通过的那些项。

As an example, let’s go back to our student records from earlier. Suppose the pass-mark is a grade of 70 or above. We can use __::filter to apply a simple function, so that we can find out which of the students have passed:

例如,让我们回到之前的学生记录。 假设及格分数为70或更高。 我们可以使用__::filter来应用一个简单的函数,以便我们找出哪些学生通过了考试:

$passed = __::filter($student_records, function($student) { return $student['grade'] >= 70; });

The reject function is simply the opposite of filter; it excludes the items that pass the truth test.

reject函数与filter相反; 它排除通过真相测试的项目。

In other words, the following two functions produce the same result:

换句话说,以下两个函数产生相同的结果:

__::filter($student_records, function($student) { return $student['grade'] >= 70; });

__::reject($student_records, function($student) { return $student['grade'] < 70; });

排序方式 (sortBy)

The sortBy function orders an array – in ascending order – using an iterator function. Here’s a simple example:

sortBy函数使用迭代器函数以升序对数组进行排序。 这是一个简单的例子:

$scores = array(476, 323, 1010, 567, 723, 1009, 600);
$sorted = __::sortBy($scores, function($score) { return $score; });

To sort in descending order, simply negate the value. So to get a list of records, starting with the student who scored highest:

要按降序排序,只需将值取反即可。 因此,要获取记录列表,首先是得分最高的学生:

$ordered = __::sortBy($student_records, function($student) { return -$student['grade']; });

通过...分组 (groupBy)

Suppose we want to re-organise our array of students according to the class they are in.

假设我们想根据他们所在的班级来重新组织学生。

This is where groupBy comes in. We can do this:

这是groupBy进入的地方。我们可以这样做:

var_dump( __::groupBy($student_records, 'class') );

Which will produce the following output:

这将产生以下输出:

array(2) {
["A"]=>
array(1) {
    [0]=>
    array(4) {
    ["name"]=>
    string(10) "Joe Bloggs"
    ["id"]=>
    int(1)
    ["grade"]=>
    int(72)
    ["class"]=>
    string(1) "A"
    }
}
["B"]=>
array(2) {
    [0]=>
    array(4) {
    ["name"]=>
    string(10) "Jack Brown"
    ["id"]=>
    int(2)
    ["grade"]=>
    int(67)
    ["class"]=>
    string(1) "B"
    }
    [1]=>
    array(4) {
    ["name"]=>
    string(13) "Jill Beaumont"
    ["id"]=>
    int(3)
    ["grade"]=>
    int(81)
    ["class"]=>
    string(1) "B"
    }
}
}

减少 (Reduce)

The reduce function is used to reduce a collection into a single value.

reduce函数用于将集合简化为单个值。

For example, to get the sum of a single-dimensional array:

例如,要获取一维数组的总和:

__::reduce(array(1, 2, 3), function($first, $second) { return $first + $second; }, 0); // 6

If we combine reduce with pluck on our student records, we can find out the average grade:

如果我们在学生记录上结合reducepluck ,我们可以找到平均成绩:

$average = round( ( __::reduce(__::pluck($student_records, 'grade'), function($first, $second) { return $first + $second; }, 0) / count($student_records) ), 2);

What we’re doing here is extracting the grades as a single dimensional array using pluck, reducing them to a single value using a simple additive iterator function, dividing it by the number of records, then rounding it to two digits.

我们在这里所做的是使用pluck将等级提取为一维数组,使用简单的累加迭代器函数将等级降低为单个值,将其除以记录数,然后将其舍入为两位数。

(Find)

The find function iterates through an array, executing a function against each item until the function returns true – in other words, it returns the first matching “record”.

find函数遍历数组,对每个项目执行一个函数,直到该函数返回true为止-换句话说,它返回第一个匹配的“记录”。

For example, to find the first student with a grade below 70, you could do this:

例如,要找到第一个成绩低于70的学生,您可以这样做:

__::find($student_records, function($student) { return $student['grade'] < 70; })

This should produce the following, if you var_dump the results:

如果您使用var_dump结果,则应生成以下内容:

array(4) {
["name"]=>
string(10) "Jack Brown"
["id"]=>
int(2)
["grade"]=>
int(67)
["class"]=>
string(1) "B"
}

Suppose we want to find a student record by ID. We can do this:

假设我们想通过ID查找学生记录。 我们可以完成这个:

function findById($records, $id) {
return __::find($records, function($record) use ($id) { return ($record['id'] == $id); });
}

If you run:

如果您运行:

var_dump(findById($student_records, 2));

You should get this:

您应该得到以下信息:

array(4) {
    ["name"]=>
    string(10) "Jack Brown"
    ["id"]=>
    int(2)
    ["grade"]=>
    int(67)
    ["class"]=>
    string(1) "B"
}

Note the inclusion of the use keyword, so that $id is in scope.

请注意包含了use关键字,因此$id在范围内。

模板化 (Templating)

One area where Backbone uses Underscore extensively is through its simple templating functionality.

Backbone广泛使用Underscore的一个领域是其简单的模板功能。

It’s generally much cleaner than, say, string concatenation, and you can combine it with other Underscore functions such as __::each to make it even more powerful.

通常,它比字符串连接要干净得多,您可以将其与其他Underscore函数(例如__::each以使其更加强大。

Within a template string, you can echo values like this:

在模板字符串中,您可以像这样回显值:

<%= $student['name'] %>

You can execute code using this syntax:

您可以使用以下语法执行代码:

<% __::each($records, student) { %> … <% }) %>

There are two common methods for templating. One is to define it as a string, using the syntax above to inject values or code, and run it through the __::template() function:

有两种常见的模板制作方法。 一种方法是使用上面的语法将其定义为字符串,以注入值或代码,然后通过__::template()函数运行它:

$welcome = 'Hello <%= $name %>, welcome back!';
print __::template($welcome, array('name' => 'Jack'));

Alternatively, you can “compile” a template by defining a variable and assigning the result of the __::template function, with a template defined as a single, string argument. The following is equivalent to the previous example:

另外,您可以通过定义变量并分配__::template函数的结果以及定义为单个字符串参数的模板来“编译”模板。 以下等效于先前的示例:

$compiled = __::template('Hello <%= $name %>, welcome back!');
print $compiled(array('name'=>'Jack'));

// Hello Jack, welcome back!

Here’s how you might create a simple template to output an unordered list, by combining __::template with __::each:

通过结合__::template__::each ,可以创建一个简单的模板以输出无序列表的方法如下:

$ul = __::template('<ul><% __::each($items, function($item)  { %><li><%= $item %></li><% }); %></ul>');

print $ul(array('items' => array('one', 'two', 'three')));

Let’s build a compiled template which takes a set of student records, and creates an unordered list of their names:

让我们构建一个编译模板,该模板接受一组学生记录,并创建其名称的无序列表:

$list_students = __::template('<ul><% __::each($records,  function($student) { %><li><%= $student["name"] %></li><% }); %></ul>');

Then, to render it:

然后,渲染它:

print $list_students(array('records' => $student_records));

You should get the following output:

您应该获得以下输出:

<ul>
    <li>Joe Bloggs</li>
    <li>Jack Brown</li>
    <li>Jill Beaumont</li>
</ul>

Or a template to produce a table of students and their grades:

或用于生成学生及其成绩表的模板:

$grades_table = __::template('<table><thead><tr><td>Student</td><td>Grade</td></tr></thead><tbody><% __::each($records, function($student) { %><tr><td><%= $student["name"] %></td><td><%= $student["grade"] %>%</td></tr><% }); %></tbody></table>');

print $grades_table(array('records' => $student_records));

You can of course pass multiple arguments in, so we could add a header to the table, for example:

您当然可以传入多个参数,所以我们可以在表中添加标题,例如:

$grades_table = __::template('<h4><%= $title %></h4><table><thead><tr><td>Student</td><td>Grade</td></tr></thead><tbody><% __::each($records, function($student) { %><tr><td><%= $student["name"] %></td><td><%= $student["grade"] %>%</td></tr><% }); %></tbody></table>');

print $grades_table(array('title' => $title, 'records' => $student_records));

扩展下划线 (Extending Underscore)

You can even create your own functions using mixins.

您甚至可以使用mixins创建自己的函数。

__::mixin(array(
'capitalize'=> function($string) { return ucwords($string); },
'shout'      => function($string) { return strtoupper($string); }
));
__::capitalize('joe bloggs'); // 'Joe Bloggs'
__::shout('Joe bloggs');       // 'JOE BLOGGS'

摘要 (Summary)

In this article, I’ve introduced you to the PHP port of the popular “utility belt” library, Underscore. I’ve gone through some of the available features; however there’s much more to explore. Have a browse through the documentation and play around!

在本文中,我向您介绍了流行的“实用程序带”库UnderscorePHP端口。 我已经介绍了一些可用的功能; 但是,还有更多值得探索的地方。 浏览文档并玩转!

翻译自: https://www.sitepoint.com/getting-started-php-underscore/

underscore

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值