Passing Assetic image path from database - php

I have been stumped for a couple days and I am seeking some direction.
I am attempting to call an image path stored in database to twig file in order to display said image. The twig example below, I am expecting the same image to be displayed twice. When inspecting the rendered html, the variable passes the path from the database, but the first image is not displayed.
From controller:
'logo' => $vendor->getLogovendors()
From database column logoVendors:
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
From twig:
<div class="container">
{{logo | raw}}
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
</div>
I am new to Symfony and its asset management. Any help or prodding in the right direction would be appreciated.

You should normally store only the path to the image in your database!
If logo was the variable you pass to the template holding the image path bundles/loginlogin/img/fs_logo_large.png you could simply include it using twig's asset function like this:
<img src="{{ asset(logo) }}"/>
what you're trying to do ( evaluating a twig function inside a string ) can be solved aswell...but i don't recommend it.
If you want to store the complete code including {{ asset() }} in your database you need to make twig evaluate the code inside the string.
This means twig shall execute the code inside the string instead of just printing it.
This can be achieved using the evaluate filter from this answer.
The final result would then be:
{{ logo |evaluate |raw }}

Related

Laravel jobs and views

I'm facing a problem i don't really understand it. I'm using Laravel 8 jobs to save my blade view as an html page in my public folder. In my main view I have to load another view and pass it a path to the image which stored in the public folder as follows:
#php
$path = $book->image;
//dd($path)
#endphp
#component('report/sales', ['path' => $path])
#endcomponent
the $path variable here contains something like
http://localhost/myapp/src/public/images/books/xyz.jpg In this view if i do dd($path) I get the http://localhost/myapp/src/public/images/books/xyz.jpg as expected. Now in sales view I do this:
<img src= "{{ asset("/" . $path) }}" alt="some text" width="50%">
Now in the produced html file the image is not loaded and src property has the value http://localhost/myapp/src/public The strange thing is that when I don't use jobs the html file has this picture. Another weird thing, if I hardcode the value of the variable $path in my main view as:
#php
$path = "http://localhost/myapp/src/public/images/books/xyz.jpg";
#endphp
#component('report/sales', ['path' => $path])
#endcomponent
And I use it with jobs the html file is produced with the picture and everything is fine. Could somebody please help me to understand? Thanks.

Display Table in Twig from a variable in controller

I am new to symfony.SO please find me a solution.On my controller
$table="<table><tr><th>Test</th></tr><tr><td>Show Test</td></tr></table>"
And I have Passed it to the twig from the controller like
return $this->render('lottery/transactions/trnissuenote/show.html.twig', array(
'table'=>$table
));
And on my twig
<div >
{{table}}
</div>
But i didnt get the table structure .Just echoing it.How will i get the table structure.Help me find a solution.
You need to display it using raw filter. Since by default {{}} escapes html content of a variable.
<div>
{{ table|raw }}
</div>
Read more: https://twig.symfony.com/doc/2.x/filters/raw.html

Img not displaying correctly in Symfony

My twig file goes like this :
Let us try to see an image :
<img src="{{ absolute_url(asset('app/Resources/images/bulb.png')) }}" alt="Symfony!" width="42" height="42"/>
Trying it another way :
<img src="app/Resources/images/bulb.png" alt="Symfony!" width="42" height="42"/>
But when I go to that page in Symfony, I see something like this :
What did I do wrong ?
currently, twig is going to be trying to find your asset in:
/web/app/Resources/images/bulb.png
use instead:
{{ asset('#AppBundle/Resources/public/images/bulb.png', absolute=true) }}
note the use of an additional public folder. If you must store assets in app, then this is sensible.
However, #Ewan Delanoy is correct, you really should be storing all your assets directly in the web folder.
Then you can just call
{{ asset('images/bulb.png', absolute=true) }}

How to feed Vue.js variable into php function?

I have a Laravel 5.1 project that I'm trying to improve and now migrating some features to Vue.js to make it more interactive and stuff.
In my view I have
<img src="{{ file_storage_path($product->path) }}" alt="{{$product->name}}"/>
This is default implementation using Laravel's blade.
Now, when I delegate this to the Vue.js I would have something like this
<img src="{{ file_storage_path(product.path) }}" alt="#{{product.name}}"/>
But this of course fails. So I need both - to invoke a php-function and at the same time to render Vue data.
I tried this
src="{{ file_storage_path( #{{product.path}} ) }}"
But of course this fails too. any suggestions?
Since I couldn't find a way to inline both blade syntax and Vue.js variable rendering I see the solution is to first invoke the php function and then append the Vue.js rendering to it.
So, the workaround would be
<img :src="'{{ file_storage_path('/') }}' + image.path" alt="#{{product.name}}" />
This solution would work only for specific case, it doesn't get answer to my original question, when a php function actually needs to accept a Vue variable. But in this case, I can simply append values.

Is there any easy way to set image asset in symfony 2

I have the html template where i image is referenced as images/logo.gif
Now in Symfony templates i have to use
src="{{ asset(['images/', 'logo.gif']|join) }}"
Is it possible to use something like
src="{{ asset(['images'])}}/logo.gif
so that i need not replace all the image tags in html file. Then i can find and replace easily. Othwise i have to manually change all image occurances
Why don't you just use this?
src="{{ asset('images/logo.gif') }}"
EDIT:
I didn't try it but concatenation should work as well:
{% set imageDir = 'images/' %}
src="{{ asset(imageDir ~ 'logo.gif') }}"
However, it introduces unnecessary complexity. I don't see anything wrong in a hard coded version. You don't change it often.

Categories