本文介绍了Laravel中foreach和forelse之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel的foreachforelse的基本区别是什么? forelse如何工作?

What's the basic difference between Laravel's foreach and forelse? How does forelse work?

我知道foreach复制您给定的数组并对其进行处理,而在forelse中它检查是否存在并在其上循环,如果不存在,则使用您提供的else条件.

I know that foreach duplicates your given array and works on it while in forelse it checks whether it exists and loops on it if it exists but if not it uses your provided else condition.

推荐答案

我相信您的问题的答案是,本质上,ForElse 一个ForEach循环,但对空输入具有额外的处理能力( s.

I believe the answer to your question is that, essentially, ForElse is a ForEach loop, but with extra handling for empty input(s).

来自Blade模板上的Laravel 5文档,该示例说明了两个循环,并且用户列表与输入相同:

From the Laravel 5 docs on Blade Templates, an example illustrating both loops with the same list of Users as Input:

@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse


现在,如果我们将其与Laravel框架的源代码进行比较,以查看循环的编译方式(当网页上的PHP呈现所有Blades构造时,会将所有Blades构造都编译为HTML):


Now if we compare this to the source code for the Laravel Framework to see how the loops are compiled (all Blades constructs are compiled into HTML when being rendered by PHP on the web page):

   /**
     * Compile the for-each statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileForeach($expression)
    {
        preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);

        $iteratee = trim($matches[1]);

        $iteration = trim($matches[2]);

        $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";

        $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';

        return "<?php {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>";
    }


   /**
     * Compile the for-else statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileForelse($expression)
    {
        $empty = '$__empty_'.++$this->forElseCounter;

        preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);

        $iteratee = trim($matches[1]);

        $iteration = trim($matches[2]);

        $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";

        $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';

        return "<?php {$empty} = true; {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} {$empty} = false; ?>";
    }


本质上,ForElse循环具有内置代码并针对空输入进行编译检查,在这种情况下,它可以有选择地为空输入输出备用显示模板.


Essentially, the ForElse loop has a built-in code and compile check for an empty input, in which case it optionally outputs an alternate display template for the empty input.

我想您可以在可能使用ForElse循环的每种情况下都使用ForEach循环,并添加包含的内容和清空ForElse可能为您捕获的支票.

I would imagine you can use a ForEach loop in every case that you might use a ForElse loop, adding that you include and empty checks that ForElse might catch for you.

参考链接:

  1. 刀片模板
  2. 刀片视图编译函数
  1. Blade Templates
  2. Blade View Compile Functions

这篇关于Laravel中foreach和forelse之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 02:27