2015-08-22 31 views
5

, ben çok temel bir bıçak yönergesini oluşturmaya çalışıyorum:Özel blade yönergesiyle argümanlar nasıl geçilir? Laravel 5.1 olarak

Blade::directive('hello', function ($planet) { 
    return 'Hello ' . $planet; 
}); 

Yazarken:

@hello('world') 

Bu döndürür:

Hello ('world') 

istediğim:

Hello world 

Evet Bu parantezleri manuel olarak kaldırabilirim ama Laravel'deki bir hata mı? (world) yerine world'u $planet değeri olarak nasıl edinebilirim.

cevap

3

bu deneyin:

Blade::directive('hello', function ($planet) { 
    return "<?php echo 'Hello' . $planet ?>"; 
}); 

@hello('world'); // => Hello world 

Ayrıca bu durumda size dizi dönüş değeri nasıl işleneceğini tanımlamak zorunda, dize yerine bir dizi geçebilir, burada bir örnek:

Blade::directive('hello', function ($value) { 
    if (is_array($value)) { 
     return "<?php echo 'Hello' . $value[0] . ' and ' . $value[1] ?>"; 
    } 

    if (is_string($planets)) { 
     return "<?php echo 'Hello' . $value ?>"; 
    } 

    // fallback when value is not recognised 
    return "<?php echo 'Hello' ?>"; 
}); 

@hello('world'); // => Hello world 
@hello(['world', 'mars']); // => Hello world mars 
+0

Ama neden bu işe yaramıyor? –

+0

@HomoSapien - bu yöntem php kodunu döndürür, ancak yönteminiz – user4621032

+0

dizgisini döndürür Gerçekten mi? ........ –

İlgili konular