簡體   English   中英

如何在laravel中將普通視圖寫入blade.php文件?

[英]how to write normal view to blade.php file in laravel?

我是 Laravel 的新手,我想寫一些 php 行,例如:

<?php
    $uri_segment = "";
    $uri_segment1 = Request::segment(1);
    $uri_segment2 = Request::segment(2);

    $inventory_array = array('premium', 'surplus', 'purchase');
    $material_array = array('brand', 'style', 'pricegroup', 'grade', 'size');
    $product_array = array('adhesive', 'silicone', 'caulk', 'adhesivebrand');
    $suppliers_array = array('dropshipper', 'price');
?>

在blade.php文件中,所以請幫助我,我寫的是:

{{  $uri_segment = ""}}
{{ $uri_segment1 = Request::segment(1) }}
{{ $uri_segment2 = Request::segment(2) }}
{{ $inventory_array = ['premium', 'surplus', 'purchase'] }}
{{ $material_array = ['brand', 'style', 'pricegroup', 'grade', 'size'] }}
{{ $product_array = ['adhesive', 'silicone', 'caulk', 'adhesivebrand'] }}
{{ $suppliers_array = ['dropshipper', 'price'] }}
{{ $system_array = ['webstores', 'utilities'] }}

但它給出了錯誤在此處輸入圖片說明

請指導我,謝謝!

在您的視圖中使用@php指令:

在某些情況下,將 PHP 代碼嵌入到視圖中很有用。 您可以使用 Blade @php 指令在模板中執行一段普通的 PHP:

@php
    $uri_segment = "";
    $uri_segment1 = Request::segment(1);
    $uri_segment2 = Request::segment(2);

    $inventory_array = array('premium', 'surplus', 'purchase');
    $material_array = array('brand', 'style', 'pricegroup', 'grade', 'size');
    $product_array = array('adhesive', 'silicone', 'caulk', 'adhesivebrand');
    $suppliers_array = array('dropshipper', 'price');
@endphp

您顯示的代碼將起作用:

<?php
    $uri_segment = "";
    $uri_segment1 = Request::segment(1);
    $uri_segment2 = Request::segment(2);

    $inventory_array = array('premium', 'surplus', 'purchase');
    $material_array = array('brand', 'style', 'pricegroup', 'grade', 'size');
    $product_array = array('adhesive', 'silicone', 'caulk', 'adhesivebrand');
    $suppliers_array = array('dropshipper', 'price');
?>

但是,您不應在 Blade 視圖中執行此操作。 您應該在控制器或服務類中移動邏輯。

{{ $inventory_array = ['premium', 'surplus', 'purchase'] }}不起作用,因為它與這樣做相同:

echo $inventory_array = ['premium', 'surplus', 'purchase'];

在某些情況下,將 PHP 代碼嵌入到視圖中很有用。 您可以使用 Blade @php指令在模板中執行一段普通的 PHP:

 @php // code here .. @endphp

雖然 Blade 提供了此功能,但頻繁使用它可能表明您的模板中嵌入了過多的邏輯。 https://laravel.com/docs/5.5/blade#php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM