Laravel使用HTML :: image作为背景图像

问题描述 投票:1回答:3

如何使用HTML::image()内嵌样式与background-image,如这个HTML标签:

这行是我的main.blade.php文件,这是我的模板的主要角色。

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

在main.blade.php我的页面查看正确的模板。但在重定向之后,如login.blade.php图像不显示。但切换到索引后我没有问题。

在公共目录中我有imagesjscss文件夹,我的公众是这样的:

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

我可以删除公共目录。如何将background-image:url('../public/images/header_pattern2.png');转换为刀片HTML::image()标签?

php laravel laravel-4
3个回答
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>

渲染输出:


0
投票

使用以下功能解决它。这是假设图像存在。

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

0
投票

我试过这个并且它有效

 background: url('{{asset('uploads/avatars/'.$employee->avatar)}}')
© www.soinside.com 2019 - 2024. All rights reserved.