【问题标题】:PHP code inside a Laravel 5 blade templateLaravel 5 刀片模板中的 PHP 代码
【发布时间】:2015-06-27 10:53:15
【问题描述】:

我必须在 Laravel 5 刀片模板中放置一些 PHP 代码。如下所示

@foreach ($farmer->tasks as $task)
    @if ($task->pivot->due_at) < date(now))
        $style = 'alert alert-danger';
    @elseif ($task->pivot->due_at) > date(now))
        $style = 'alert alert-success';
    @else
        $style = '';
    @endif
@endforeach

将 PHP 代码放入 Laravel 5 刀片模板的实际过程是什么?

【问题讨论】:

    标签: php laravel-5 laravel-blade


    【解决方案1】:

    根据documentation,在 Laravel 5.2 及更高版本中,您可以使用以下代码:

    @php
    {{-- PHP code here --}}
    @endphp
    

    或者,您可以按照 here 的描述扩展 Blade 模板引擎。

    如果上述解决方案都不适合,您将被by Armenby Gonzalo 给出的答案所困扰。

    【讨论】:

    • 其实很好奇@php&lt;?php之间的区别……我的意思是添加这些指令可能是有原因的。
    • @Tanckom 据我了解,编写@php&lt;?php 之间没有区别。我曾与喜欢写&lt;?php 而不是@php 的人一起工作,他们背后的主要原因是他们更习惯使用它,因为他们已经在 Raw PHP 中使用了一段时间。但通常在代码审查期间,我通常最终将它们改回@php,因为这是我更喜欢编写它的方式。在Laravel's documentation 中是否要求写@php。我猜是因为要保持 Blade 语法的一致性。
    • @I.Antonov 使用@php&lt;?php 之一应该在贵公司的风格指南中定义。根据最后接触文件的人来切换样式并不是代码审查的本意。
    • @Peilonrayz 是的,我试图让他们这样做。但最初他们并不感兴趣,因为他们“没有时间”。相反,我们被分配在审查期间更改它并推动决赛。只是分享一个场景。而且我不再在那里工作了。
    【解决方案2】:

    只需打开和关闭 PHP 标签:

    <?php $style = '...'; ?>
    

    【讨论】:

    • 似乎是 Laravel 4 的唯一解决方案,因为不支持 @php 指令。
    【解决方案3】:

    在现代 Laravel (6/7) 中你应该这样做:

    @php
       yourphpcode();
    @endphp
    

    【讨论】:

      【解决方案4】:

      Laravel 食谱提出了一种简单但有效的方法,无需包含 PHP 标签:

      {{--*/ $var = 'test' /*--}}
      

      {{-- --}} 用作刀片式评论 / 和 / 恢复评论产生的效果

      <?php $var = 'test' ?>
      

      问题在于它比包含 PHP 标记要长 :-(

      【讨论】:

      • 我刚刚失去了 1 小时的时间,想知道一个变量是如何获得它的值的,当它被注释掉时......谢谢。
      【解决方案5】:

      以下新的 NewBladeCompiler 将使用 @{ }} 来接受所有 PHP 代码,如变量分配、类声明等。

      例如@{ $variable = 0; }} 将被编译为&lt;?php $variable=0; ?&gt;

      <?php
      
          use Illuminate\View\Compilers\BladeCompiler;
      
          class NewBladeCompiler extends BladeCompiler
          {
      
              /**
               * Get the echo methods in the proper order for compilation.
               *
               * @return array
               */
              function getEchoMethods()
              {
                  $methods = [
                      'compileRawEchos'     => strlen(stripcslashes($this->rawTags[0])),
                      'compileEscapedEchos' => strlen(stripcslashes($this->escapedTags[0])),
                      'compileRegularEchos' => strlen(stripcslashes($this->contentTags[0])),
                      'compilePhpEchos'     => strlen(stripcslashes("@{"))
                  ];
      
                  uksort($methods, function ($method1, $method2) use ($methods) {
      
                      // Ensure the longest tags are processed first
                      if($methods[$method1] > $methods[$method2])
                      {
                          return -1;
                      }
                      if($methods[$method1] < $methods[$method2])
                      {
                          return 1;
                      }
      
                      // Otherwise give preference to raw tags (assuming they've overridden)
                      if($method1 === 'compilePhpEchos')
                      {
                          return -1;
                      }
                      if($method2 === 'compilePhpEchos')
                      {
                          return 1;
                      }
                      if($method1 === 'compileRawEchos')
                      {
                          return -1;
                      }
                      if($method2 === 'compileRawEchos')
                      {
                          return 1;
                      }
                      if($method1 === 'compileEscapedEchos')
                      {
                          return -1;
                      }
                      if($method2 === 'compileEscapedEchos')
                      {
                          return 1;
                      }
                  });
      
                  return $methods;
              }
      
              function compilePhpEchos( $value )
              {
                  $pattern  = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', "@{", "}}");
                  $callback = function ($matches) {
                      $whitespace = empty($matches[3]) ? '' : $matches[3] . $matches[3];
                      return $matches[1] ? substr($matches[0], 1) : '<?php ' . $matches[2] . ' ?>' . $whitespace;
                  };
                  return preg_replace_callback($pattern, $callback, $value);
              }
      
          }
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2015-08-04
        • 2017-04-03
        • 2018-05-31
        • 2015-04-16
        • 2020-05-23
        • 2016-12-18
        • 1970-01-01
        • 2020-06-22
        • 2013-04-29
        相关资源
        最近更新 更多