2015-06-27 29 views
12

Tôi phải đặt một số mã PHP bên trong Laravel 5 Blade Template. Giống như bên dướiMã PHP bên trong Laravel 5 Blade Template

@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 

Quy trình thực tế để đặt mã PHP bên trong Laravel 5 Blade Template ??

Trả lời

8

thẻ php Chỉ cần mở và gần gũi: <?php $style = '...'; ?>

0

Các NewBladeCompiler mới sau đây sẽ sử dụng @{ }} để chấp nhận tất cả các mã php như ấn định biến, tuyên bố lớp vv ví dụ @{ $variable = 0; }} sẽ được biên dịch để <?php $variable=0; ?>

<?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); 
    } 

} 

?> 
2

Laravel công thức nấu ăn cho thấy một cách đơn giản nhưng hiệu quả để làm điều đó mà không bao gồm các thẻ php

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

{{- -}} hoạt động như một bình luận lưỡi /và/ trở lại trạng ảnh hưởng của lời nhận xét kết quả trên

<?php $var = 'test' ?> 

vấn đề là dài hơn bao gồm tags php :-(

24

Theo documentation, trong Laravel 5.2 và mới hơn, bạn có thể sử dụng đoạn mã sau:

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

Ngoài ra, bạn có thể mở rộng cơ Blade khuôn mẫu vì nó được mô tả here.

Nếu không có giải pháp nào ở trên phù hợp, bạn bị kẹt với câu trả lời được đưa ra bởi @Armen và @Gonzalo

Các vấn đề liên quan