在树枝中动态显示来自数据库的背景图像
Displaying background image dynamically from database in twig
我正在使用 laravel 并尝试使用户能够使用更新表单更改其页面的背景图像。他们上传了一张图片,图片使用树枝显示。我下面的代码用于获取用于背景图片的用户图像
$this['userprofile'] = User::with('background_pic')->where('id', $slug)->first();
背景图片将使用下面的代码 div 渲染
<div align="center" style="height: 30px; background-image: url('{{ userprofile.avater.path }}');"></d>
图像可以很好地上传到后端,但不确定如何在 twig 中显示它。使用上面的 background-image: url('{{ userprofile.avater.path}}')
不起作用
为了从变量中呈现 background-image
url,您需要使用 asset
函数。
此 twig 函数将您的图像路径解析为可公开访问的路径 URL。
用法示例:
<div style="background-image: url('{{ asset(userprofile.avater.path) }}');"></d>
我正在使用 laravel 并尝试使用户能够使用更新表单更改其页面的背景图像。他们上传了一张图片,图片使用树枝显示。我下面的代码用于获取用于背景图片的用户图像
$this['userprofile'] = User::with('background_pic')->where('id', $slug)->first();
背景图片将使用下面的代码 div 渲染
<div align="center" style="height: 30px; background-image: url('{{ userprofile.avater.path }}');"></d>
图像可以很好地上传到后端,但不确定如何在 twig 中显示它。使用上面的 background-image: url('{{ userprofile.avater.path}}')
不起作用
为了从变量中呈现 background-image
url,您需要使用 asset
函数。
此 twig 函数将您的图像路径解析为可公开访问的路径 URL。
用法示例:
<div style="background-image: url('{{ asset(userprofile.avater.path) }}');"></d>