2014-03-06 33 views
0

圖像如何使用HTML::image()內聯與background-image風格像這樣的HTML標籤:Laravel使用HTML ::背景圖像

此行是爲我main.blade.php文件,並且是主要skeletion我模板。

<div style="height: 45px;background-image:url('../public/images/header_pattern2.png');border-radius: 5px;position:relative"> 

in main.blade.php我的頁面查看正確的模板。但重定向後如login.blade.php圖片不顯示。但切換到索引後,我沒有問題。

在公共目錄

我有imagesjscss文件夾和我的公開是這樣的:

'public' => __DIR__.'/..', 

我可以刪除公共目錄。如何將background-image:url('../public/images/header_pattern2.png');轉換爲刀片HTML::image()標記?

回答

6

你可以試試這個:

<div style="height: 45px;background-image:url('{{ asset('images/header_pattern2.png') }}');border-radius: 5px;position:relative"> 

更新:

其實,HTML::image()產生<img />標籤和你正在使用一個div,如果你想用這個使用HTML::div()那麼您可以創建一個custom macro

更新:我創造了這個custom macro

/** 
* Param $attr : An array of styles 
* Param $html : HTML content for div 
* Returns HTML DIV 
*/ 
HTML::macro('divWithBG', function($attr = array(), $html = ''){ 
    $style = 'style = '; 
    foreach ($attr as $key => $value) { 
     if($key == 'background-image') $style .= $key.':url(\'' . $value .'\');'; 
     else $style .= $key . ':' . $value .';'; 
    } 
return "<div $style >$html</div>"; 
}); 

您可以使用它作爲blade view

{{ HTML::divWithBG(array('background-image' => asset('images/8_big.jpg'), 'height' => '45px', 'border-radius'=>'5px', 'position'=>'relative')) }} 

輸出:

<div style="background-image:url('http://blog.dev/images/8_big.jpg');height:45px;border-radius:5px;position:relative;"></div> 

渲染輸出:

alt text

+0

您定義的宏無法生成正確的樣式。{{HTML :: div(array('background-image'=> asset('images/header_pattern2.png'),'height'=>'45px ','border-radius'=>'5px','position'=>'relative'))}}'generate:'

'html –

+0

@TuxWorld,It works,actually I miss a'else',most probably it是一個拼寫錯誤,但修復了它,但是如果你沒有提供任何「HTML」作爲第二個參數,那麼它可能不會被看到。 –

+0

我不需要'HTML'。 –

0

使用下面的函數來解決它。這是假設圖像存在。

public function inline($path) 
{  
    $filetype = File::type($path); 
    $response = Response(File::get($path), 200); 
    $response->header('Content-Type', $filetype); 
    return $response; 
}