Osuji Kingsley
Osuji Kingsley

Reputation: 371

Displaying background image dynamically from database in twig

I am using laravel and trying to make it possible for users to change the background image of their page using an update form. They upload a pic and the pic is displayed using twig. My code below for getting the user image used for the background pic

$this['userprofile'] = User::with('background_pic')->where('id', $slug)->first();

The background pic is to be rendered in a div using the code below

<div align="center" style="height: 30px; background-image: url('{{ userprofile.avater.path }}');"></d>

the image uploads fine to the backend but not sure how to display it in twig. using the above background-image: url('{{ userprofile.avater.path}}') does not work

Upvotes: 0

Views: 1487

Answers (1)

Nicolai Fr&#246;hlich
Nicolai Fr&#246;hlich

Reputation: 52513

In order to render the background-image url from a variable you need to use the asset function.

This twig function resolves the path of your image to a publicly accessible URL.

Example usage:

<div style="background-image: url('{{ asset(userprofile.avater.path) }}');"></d>

Upvotes: 1

Related Questions